Apache Lucene open-source search software
LUCENE-1326: be sure to call Inflator/Deflator.end when working with zlib for compressed fields, to avoid memory leaks
Michael McCandless40997321953a
diff --git a/src/java/org/apache/lucene/index/FieldsReader.java b/src/java/org/apache/lucene/index/FieldsReader.javaindex f9b98f32d63..61b7820c393 100644--- a/src/java/org/apache/lucene/index/FieldsReader.java+++ b/src/java/org/apache/lucene/index/FieldsReader.java@@ -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 dataByteArrayOutputStream 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 datareturn bos.toByteArray();diff --git a/src/java/org/apache/lucene/index/FieldsWriter.java b/src/java/org/apache/lucene/index/FieldsWriter.javaindex e6dff0fc1b1..cdb89b47111 100644--- a/src/java/org/apache/lucene/index/FieldsWriter.java+++ b/src/java/org/apache/lucene/index/FieldsWriter.java@@ -225,14 +225,6 @@ final class FieldsWriterprivate 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 datareturn bos.toByteArray();