pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1585354 - /pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter/LZWFilter.java
Date Sun, 06 Apr 2014 22:00:06 GMT
Author: tilman
Date: Sun Apr  6 22:00:05 2014
New Revision: 1585354

URL: http://svn.apache.org/r1585354
Log:
PDFBOX-1147: Code table in LZW filter must be local to the method because there is only one
filter instance

Modified:
    pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter/LZWFilter.java

Modified: pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter/LZWFilter.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter/LZWFilter.java?rev=1585354&r1=1585353&r2=1585354&view=diff
==============================================================================
--- pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter/LZWFilter.java (original)
+++ pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter/LZWFilter.java Sun Apr
 6 22:00:05 2014
@@ -51,10 +51,8 @@ public class LZWFilter implements Filter
      */
     public static final long EOD = 257;
 
-    /**
-     * The LZW code table.
-     */
-    private ArrayList<byte[]> codeTable = null;
+    //BEWARE: codeTable must be local to each method, because there is only
+    // one instance of each filter
 
     /**
      * {@inheritDoc}
@@ -62,7 +60,7 @@ public class LZWFilter implements Filter
     public void decode(InputStream compressedData, OutputStream result, COSDictionary options,
int filterIndex)
             throws IOException
     {
-        codeTable = null;
+        ArrayList<byte[]> codeTable = null;
         int chunk = 9;
         MemoryCacheImageInputStream in = new MemoryCacheImageInputStream(compressedData);
         long nextCommand = 0;
@@ -75,7 +73,7 @@ public class LZWFilter implements Filter
                 if (nextCommand == CLEAR_TABLE)
                 {
                     chunk = 9;
-                    initCodeTable();
+                    codeTable = createCodeTable();
                     prevCommand = -1;
                 }
                 else
@@ -124,7 +122,7 @@ public class LZWFilter implements Filter
     public void encode(InputStream rawData, OutputStream result, COSDictionary options, int
filterIndex)
             throws IOException
     {
-        initCodeTable();
+        ArrayList<byte[]> codeTable = createCodeTable();
         int chunk = 9;
 
         byte[] inputPattern = null;
@@ -164,7 +162,7 @@ public class LZWFilter implements Filter
                         // code table is full
                         out.writeBits(CLEAR_TABLE, chunk);
                         chunk = 9;
-                        initCodeTable();
+                        codeTable = createCodeTable();
                     }
 
                     inputPattern = new byte[]
@@ -195,7 +193,6 @@ public class LZWFilter implements Filter
         out.writeBits(EOD, chunk);
         out.writeBits(0, 7); // pad with 0
         out.flush(); // must do or file will be empty :-(
-        codeTable.clear();
     }
 
     /**
@@ -241,9 +238,9 @@ public class LZWFilter implements Filter
      * Init the code table with 1 byte entries and the EOD and CLEAR_TABLE
      * markers.
      */
-    private void initCodeTable()
+    private ArrayList<byte[]> createCodeTable()
     {
-        codeTable = new ArrayList<byte[]>(4096);
+        ArrayList<byte[]> codeTable = new ArrayList<byte[]>(4096);
         for (int i = 0; i < 256; ++i)
         {
             codeTable.add(new byte[]
@@ -253,6 +250,7 @@ public class LZWFilter implements Filter
         }
         codeTable.add(null); // 256 EOD
         codeTable.add(null); // 257 CLEAR_TABLE
+        return codeTable;
     }
 
     /**



Mime
View raw message