lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mikemcc...@apache.org
Subject svn commit: r673972 - in /lucene/java/trunk/src/java/org/apache/lucene/index: FieldsReader.java FieldsWriter.java
Date Fri, 04 Jul 2008 09:20:41 GMT
Author: mikemccand
Date: Fri Jul  4 02:20:41 2008
New Revision: 673972

URL: http://svn.apache.org/viewvc?rev=673972&view=rev
Log:
LUCENE-1326: be sure to call Inflator/Deflator.end when working with zlib for compressed fields,
to avoid memory leaks

Modified:
    lucene/java/trunk/src/java/org/apache/lucene/index/FieldsReader.java
    lucene/java/trunk/src/java/org/apache/lucene/index/FieldsWriter.java

Modified: lucene/java/trunk/src/java/org/apache/lucene/index/FieldsReader.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/index/FieldsReader.java?rev=673972&r1=673971&r2=673972&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/index/FieldsReader.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/index/FieldsReader.java Fri Jul  4 02:20:41
2008
@@ -550,28 +550,31 @@
   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();

Modified: lucene/java/trunk/src/java/org/apache/lucene/index/FieldsWriter.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/index/FieldsWriter.java?rev=673972&r1=673971&r2=673972&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/index/FieldsWriter.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/index/FieldsWriter.java Fri Jul  4 02:20:41
2008
@@ -225,14 +225,6 @@
 
     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 @@
        */
       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();



Mime
View raw message