LUCENE-1326: be sure to call Inflator/Deflator.end when working with zlib for compressed fields, to avoid memory leaks

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@673972 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2008-07-04 09:20:41 +00:00
parent bca43ea3ea
commit 4099732195
2 changed files with 39 additions and 32 deletions

View File

@ -550,28 +550,31 @@ final class FieldsReader {
private final byte[] uncompress(final byte[] input)
throws CorruptIndexException, IOException {
Inflater decompressor = new Inflater();
decompressor.setInput(input);
// Create an expandable byte array to hold the decompressed data
ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
// Decompress the data
byte[] buf = new byte[1024];
while (!decompressor.finished()) {
try {
int count = decompressor.inflate(buf);
bos.write(buf, 0, count);
}
catch (DataFormatException e) {
// this will happen if the field is not compressed
CorruptIndexException newException = new CorruptIndexException("field data are in wrong format: " + e.toString());
newException.initCause(e);
throw newException;
Inflater decompressor = new Inflater();
try {
decompressor.setInput(input);
// Decompress the data
byte[] buf = new byte[1024];
while (!decompressor.finished()) {
try {
int count = decompressor.inflate(buf);
bos.write(buf, 0, count);
}
catch (DataFormatException e) {
// this will happen if the field is not compressed
CorruptIndexException newException = new CorruptIndexException("field data are in wrong format: " + e.toString());
newException.initCause(e);
throw newException;
}
}
} finally {
decompressor.end();
}
decompressor.end();
// Get the decompressed data
return bos.toByteArray();

View File

@ -225,14 +225,6 @@ final class FieldsWriter
private final byte[] compress (byte[] input) {
// Create the compressor with highest level of compression
Deflater compressor = new Deflater();
compressor.setLevel(Deflater.BEST_COMPRESSION);
// Give the compressor the data to compress
compressor.setInput(input);
compressor.finish();
/*
* Create an expandable byte array to hold the compressed data.
* You cannot use an array that's the same size as the orginal because
@ -241,14 +233,26 @@ final class FieldsWriter
*/
ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
// Compress the data
byte[] buf = new byte[1024];
while (!compressor.finished()) {
int count = compressor.deflate(buf);
bos.write(buf, 0, count);
// Create the compressor with highest level of compression
Deflater compressor = new Deflater();
try {
compressor.setLevel(Deflater.BEST_COMPRESSION);
// Give the compressor the data to compress
compressor.setInput(input);
compressor.finish();
// Compress the data
byte[] buf = new byte[1024];
while (!compressor.finished()) {
int count = compressor.deflate(buf);
bos.write(buf, 0, count);
}
} finally {
compressor.end();
}
compressor.end();
// Get the compressed data
return bos.toByteArray();