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
src/java/org/apache/lucene/index

View File

@ -550,12 +550,14 @@ final class FieldsReader {
private final byte[] uncompress(final byte[] input) private final byte[] uncompress(final byte[] input)
throws CorruptIndexException, IOException { throws CorruptIndexException, IOException {
Inflater decompressor = new Inflater();
decompressor.setInput(input);
// Create an expandable byte array to hold the decompressed data // Create an expandable byte array to hold the decompressed data
ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length); ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
Inflater decompressor = new Inflater();
try {
decompressor.setInput(input);
// Decompress the data // Decompress the data
byte[] buf = new byte[1024]; byte[] buf = new byte[1024];
while (!decompressor.finished()) { while (!decompressor.finished()) {
@ -570,8 +572,9 @@ final class FieldsReader {
throw newException; throw newException;
} }
} }
} finally {
decompressor.end(); decompressor.end();
}
// Get the decompressed data // Get the decompressed data
return bos.toByteArray(); return bos.toByteArray();

View File

@ -225,14 +225,6 @@ final class FieldsWriter
private final byte[] compress (byte[] input) { 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. * Create an expandable byte array to hold the compressed data.
* You cannot use an array that's the same size as the orginal because * You cannot use an array that's the same size as the orginal because
@ -241,6 +233,16 @@ final class FieldsWriter
*/ */
ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length); ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
// 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 // Compress the data
byte[] buf = new byte[1024]; byte[] buf = new byte[1024];
while (!compressor.finished()) { while (!compressor.finished()) {
@ -248,7 +250,9 @@ final class FieldsWriter
bos.write(buf, 0, count); bos.write(buf, 0, count);
} }
} finally {
compressor.end(); compressor.end();
}
// Get the compressed data // Get the compressed data
return bos.toByteArray(); return bos.toByteArray();