You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by bo...@apache.org on 2015/08/11 20:18:34 UTC

svn commit: r1695345 - in /commons/proper/compress/trunk/src/main/java/org/apache/commons/compress: archivers/zip/UnshrinkingInputStream.java compressors/lzw/LZWInputStream.java compressors/z/ZCompressorInputStream.java

Author: bodewig
Date: Tue Aug 11 18:18:34 2015
New Revision: 1695345

URL: http://svn.apache.org/r1695345
Log:
COMPRESS-300 remove protected fields from LZWInputStream

Modified:
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/UnshrinkingInputStream.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/lzw/LZWInputStream.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/z/ZCompressorInputStream.java

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/UnshrinkingInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/UnshrinkingInputStream.java?rev=1695345&r1=1695344&r2=1695345&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/UnshrinkingInputStream.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/UnshrinkingInputStream.java Tue Aug 11 18:18:34 2015
@@ -36,20 +36,22 @@ class UnshrinkingInputStream extends LZW
     
     public UnshrinkingInputStream(InputStream inputStream) throws IOException {
         super(inputStream, ByteOrder.LITTLE_ENDIAN);
-        setClearCode(codeSize);
+        setClearCode(DEFAULT_CODE_SIZE);
         initializeTables(MAX_CODE_SIZE);
-        isUsed = new boolean[prefixes.length];
+        isUsed = new boolean[getPrefixesLength()];
         for (int i = 0; i < (1 << 8); i++) {
             isUsed[i] = true;
         }
-        tableSize = clearCode + 1;
+        setTableSize(getClearCode() + 1);
     }
 
     @Override
     protected int addEntry(int previousCode, byte character) throws IOException {
+        int tableSize = getTableSize();
         while ((tableSize < MAX_TABLE_SIZE) && isUsed[tableSize]) {
             tableSize++;
         }
+        setTableSize(tableSize);
         int idx = addEntry(previousCode, character, MAX_TABLE_SIZE);
         if (idx >= 0) {
             isUsed[idx] = true;
@@ -60,14 +62,14 @@ class UnshrinkingInputStream extends LZW
     private void partialClear() {
         final boolean[] isParent = new boolean[MAX_TABLE_SIZE];
         for (int i = 0; i < isUsed.length; i++) {
-            if (isUsed[i] && prefixes[i] != -1) {
-                isParent[prefixes[i]] = true;
+            if (isUsed[i] && getPrefix(i) != UNUSED_PREFIX) {
+                isParent[getPrefix(i)] = true;
             }
         }
-        for (int i = clearCode + 1; i < isParent.length; i++) {
+        for (int i = getClearCode() + 1; i < isParent.length; i++) {
             if (!isParent[i]) {
                 isUsed[i] = false;
-                prefixes[i] = -1;
+                setPrefix(i, UNUSED_PREFIX);
             }
         }
     }
@@ -89,19 +91,19 @@ class UnshrinkingInputStream extends LZW
         final int code = readNextCode();
         if (code < 0) {
             return -1;
-        } else if (code == clearCode) {
+        } else if (code == getClearCode()) {
             final int subCode = readNextCode();
             if (subCode < 0) {
                 throw new IOException("Unexpected EOF;");
             } else if (subCode == 1) {
-                if (codeSize < MAX_CODE_SIZE) {
-                    codeSize++;
+                if (getCodeSize() < MAX_CODE_SIZE) {
+                    incrementCodeSize();
                 } else {
                     throw new IOException("Attempt to increase code size beyond maximum");
                 }
             } else if (subCode == 2) {
                 partialClear();
-                tableSize = clearCode + 1;
+                setTableSize(getClearCode() + 1);
             } else {
                 throw new IOException("Invalid clear code subcode " + subCode);
             }

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/lzw/LZWInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/lzw/LZWInputStream.java?rev=1695345&r1=1695344&r2=1695345&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/lzw/LZWInputStream.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/lzw/LZWInputStream.java Tue Aug 11 18:18:34 2015
@@ -34,16 +34,19 @@ import org.apache.commons.compress.utils
  * @since 1.10
  */
 public abstract class LZWInputStream extends CompressorInputStream {
+    protected static final int DEFAULT_CODE_SIZE = 9;
+    protected static final int UNUSED_PREFIX = -1;
+
     private final byte[] oneByte = new byte[1];
 
     protected final BitInputStream in;
-    protected int clearCode = -1;
-    protected int codeSize = 9;
-    protected byte previousCodeFirstChar;
-    protected int previousCode = -1;
-    protected int tableSize = 0;
-    protected int[] prefixes;
-    protected byte[] characters;
+    private int clearCode = -1;
+    private int codeSize = DEFAULT_CODE_SIZE;
+    private byte previousCodeFirstChar;
+    private int previousCode = UNUSED_PREFIX;
+    private int tableSize;
+    private int[] prefixes;
+    private byte[] characters;
     private byte[] outputStack;
     private int outputStackLocation;
 
@@ -178,4 +181,49 @@ public abstract class LZWInputStream ext
         }
         return 0;
     }
+
+    protected int getCodeSize() {
+        return codeSize;
+    }
+
+    protected void resetCodeSize() {
+        setCodeSize(DEFAULT_CODE_SIZE);
+    }
+
+    protected void setCodeSize(int cs) {
+        this.codeSize = cs;
+    }
+
+    protected void incrementCodeSize() {
+        codeSize++;
+    }
+
+    protected void resetPreviousCode() {
+        this.previousCode = -1;
+    }
+
+    protected int getPrefix(int offset) {
+        return prefixes[offset];
+    }
+
+    protected void setPrefix(int offset, int value) {
+        prefixes[offset] = value;
+    }
+
+    protected int getPrefixesLength() {
+        return prefixes.length;
+    }
+
+    protected int getClearCode() {
+        return clearCode;
+    }
+
+    protected int getTableSize() {
+        return tableSize;
+    }
+
+    protected void setTableSize(int newSize) {
+        tableSize = newSize;
+    }
+
 }

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/z/ZCompressorInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/z/ZCompressorInputStream.java?rev=1695345&r1=1695344&r2=1695345&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/z/ZCompressorInputStream.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/z/ZCompressorInputStream.java Tue Aug 11 18:18:34 2015
@@ -49,17 +49,14 @@ public class ZCompressorInputStream exte
         blockMode = (thirdByte & BLOCK_MODE_MASK) != 0;
         maxCodeSize = thirdByte & MAX_CODE_SIZE_MASK;
         if (blockMode) {
-            setClearCode(codeSize);
+            setClearCode(DEFAULT_CODE_SIZE);
         }
         initializeTables(maxCodeSize);
         clearEntries();
     }
     
     private void clearEntries() {
-        tableSize = 1 << 8;
-        if (blockMode) {
-            tableSize++;
-        }
+        setTableSize((1 << 8) + (blockMode ? 1 : 0));
     }
 
     /**
@@ -100,11 +97,11 @@ public class ZCompressorInputStream exte
      */
     @Override
     protected int addEntry(int previousCode, byte character) throws IOException {
-        final int maxTableSize = 1 << codeSize;
+        final int maxTableSize = 1 << getCodeSize();
         int r = addEntry(previousCode, character, maxTableSize);
-        if (tableSize == maxTableSize && codeSize < maxCodeSize) {
+        if (getTableSize() == maxTableSize && getCodeSize() < maxCodeSize) {
             reAlignReading();
-            codeSize++;
+            incrementCodeSize();
         }
         return r;
     }
@@ -132,19 +129,19 @@ public class ZCompressorInputStream exte
         final int code = readNextCode();
         if (code < 0) {
             return -1;
-        } else if (blockMode && code == clearCode) {
+        } else if (blockMode && code == getClearCode()) {
             clearEntries();
             reAlignReading();
-            codeSize = 9;
-            previousCode = -1;
+            resetCodeSize();
+            resetPreviousCode();
             return 0;
         } else {
             boolean addedUnfinishedEntry = false;
-            if (code == tableSize) {
+            if (code == getTableSize()) {
                 addRepeatOfPreviousCode();
                 addedUnfinishedEntry = true;
-            } else if (code > tableSize) {
-                throw new IOException(String.format("Invalid %d bit code 0x%x", codeSize, code));
+            } else if (code > getTableSize()) {
+                throw new IOException(String.format("Invalid %d bit code 0x%x", getCodeSize(), code));
             }
             return expandCodeToOutputStack(code, addedUnfinishedEntry);
         }