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 2013/12/15 17:23:49 UTC

svn commit: r1551028 - /commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/z/AbstractLZWInputStream.java

Author: bodewig
Date: Sun Dec 15 16:23:48 2013
New Revision: 1551028

URL: http://svn.apache.org/r1551028
Log:
reduce duplication in read method

Modified:
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/z/AbstractLZWInputStream.java

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/z/AbstractLZWInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/z/AbstractLZWInputStream.java?rev=1551028&r1=1551027&r2=1551028&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/z/AbstractLZWInputStream.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/z/AbstractLZWInputStream.java Sun Dec 15 16:23:48 2013
@@ -64,35 +64,17 @@ public abstract class AbstractLZWInputSt
     
     @Override
     public int read(byte[] b, int off, int len) throws IOException {
-        int bytesRead = 0;
-        int remainingInStack = outputStack.length - outputStackLocation;
-        if (remainingInStack > 0) {
-            int maxLength = Math.min(remainingInStack, len);
-            System.arraycopy(outputStack, outputStackLocation, b, off, maxLength);
-            outputStackLocation += maxLength;
-            off += maxLength;
-            len -= maxLength;
-            bytesRead += maxLength;
-        }
-        while (len > 0) {
+        int bytesRead = readFromStack(b, off, len);
+        while (len - bytesRead > 0) {
             int result = decompressNextSymbol();
             if (result < 0) {
                 if (bytesRead > 0) {
                     count(bytesRead);
                     return bytesRead;
-                } else {
-                    return result;
                 }
+                return result;
             }
-            remainingInStack = outputStack.length - outputStackLocation;
-            if (remainingInStack > 0) {
-                int maxLength = Math.min(remainingInStack, len);
-                System.arraycopy(outputStack, outputStackLocation, b, off, maxLength);
-                outputStackLocation += maxLength;
-                off += maxLength;
-                len -= maxLength;
-                bytesRead += maxLength;
-            }
+            bytesRead += readFromStack(b, off + bytesRead, len - bytesRead);
         }
         count(bytesRead);
         return bytesRead;
@@ -196,4 +178,15 @@ public abstract class AbstractLZWInputSt
         previousCode = code;
         return outputStackLocation;
     }
+
+    private int readFromStack(byte[] b, int off, int len) {
+        int remainingInStack = outputStack.length - outputStackLocation;
+        if (remainingInStack > 0) {
+            int maxLength = Math.min(remainingInStack, len);
+            System.arraycopy(outputStack, outputStackLocation, b, off, maxLength);
+            outputStackLocation += maxLength;
+            return maxLength;
+        }
+        return 0;
+    }
 }