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) 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);
// Decompress the data Inflater decompressor = new Inflater();
byte[] buf = new byte[1024];
while (!decompressor.finished()) { try {
try { decompressor.setInput(input);
int count = decompressor.inflate(buf);
bos.write(buf, 0, count); // Decompress the data
} byte[] buf = new byte[1024];
catch (DataFormatException e) { while (!decompressor.finished()) {
// this will happen if the field is not compressed try {
CorruptIndexException newException = new CorruptIndexException("field data are in wrong format: " + e.toString()); int count = decompressor.inflate(buf);
newException.initCause(e); bos.write(buf, 0, count);
throw newException; }
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 // 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,14 +233,26 @@ final class FieldsWriter
*/ */
ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length); ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
// Compress the data // Create the compressor with highest level of compression
byte[] buf = new byte[1024]; Deflater compressor = new Deflater();
while (!compressor.finished()) {
int count = compressor.deflate(buf); try {
bos.write(buf, 0, count); 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 // Get the compressed data
return bos.toByteArray(); return bos.toByteArray();