Repository: commons-compress
Updated Branches:
  refs/heads/master d5f61b439 -> c1470f52e


make SonarQube less unhappy


Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/0367fb31
Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/0367fb31
Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/0367fb31

Branch: refs/heads/master
Commit: 0367fb31efae4535e15f01f6c27ea814a0739b81
Parents: d5f61b4
Author: Stefan Bodewig <[email protected]>
Authored: Sun Jan 21 14:25:38 2018 +0100
Committer: Stefan Bodewig <[email protected]>
Committed: Sun Jan 21 14:25:38 2018 +0100

----------------------------------------------------------------------
 .../bzip2/BZip2CompressorInputStream.java       |   6 +-
 .../compressors/deflate64/HuffmanDecoder.java   | 100 +++++++++++--------
 .../compressors/lz77support/LZ77Compressor.java |   2 +-
 .../compressors/zstandard/ZstdUtils.java        |   2 +-
 4 files changed, 61 insertions(+), 49 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-compress/blob/0367fb31/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java
 
b/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java
index 64a9007..9c7a80a 100644
--- 
a/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java
+++ 
b/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java
@@ -642,9 +642,9 @@ public class BZip2CompressorInputStream extends 
CompressorInputStream implements
                     nextSym = perm_zt[tmp];
                 }
 
-                final int yy_0 = yy[0];
-                checkBounds(yy_0, 256, "yy");
-                final byte ch = seqToUnseq[yy_0];
+                final int yy0 = yy[0];
+                checkBounds(yy0, 256, "yy");
+                final byte ch = seqToUnseq[yy0];
                 unzftab[ch & 0xff] += s + 1;
 
                 while (s-- >= 0) {

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/0367fb31/src/main/java/org/apache/commons/compress/compressors/deflate64/HuffmanDecoder.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/commons/compress/compressors/deflate64/HuffmanDecoder.java
 
b/src/main/java/org/apache/commons/compress/compressors/deflate64/HuffmanDecoder.java
index 62d19d9..23a7236 100644
--- 
a/src/main/java/org/apache/commons/compress/compressors/deflate64/HuffmanDecoder.java
+++ 
b/src/main/java/org/apache/commons/compress/compressors/deflate64/HuffmanDecoder.java
@@ -131,51 +131,58 @@ class HuffmanDecoder implements Closeable {
 
     public int decode(byte[] b, int off, int len) throws IOException {
         while (!finalBlock || state.hasData()) {
-            switch (state.state()) {
-                case INITIAL:
+            if (state.state() == HuffmanState.INITIAL) {
                     finalBlock = readBits(1) == 1;
                     int mode = (int) readBits(2);
                     switch (mode) {
                         case 0:
-                            reader.alignWithByteBoundary();
-                            long bLen = readBits(16);
-                            long bNLen = readBits(16);
-                            if (((bLen ^ 0xFFFF) & 0xFFFF) != bNLen) {
-                                //noinspection DuplicateStringLiteralInspection
-                                throw new IllegalStateException("Illegal LEN / 
NLEN values");
-                            }
-                            state = new UncompressedState(bLen);
+                            switchToUncompressedState();
                             break;
                         case 1:
                             state = new HuffmanCodes(FIXED_CODES, 
FIXED_LITERALS, FIXED_DISTANCE);
                             break;
                         case 2:
-                            int literals = (int) (readBits(5) + 257);
-                            int[] literalTable = new int[literals];
-
-                            int distances = (int) (readBits(5) + 1);
-                            int[] distanceTable = new int[distances];
-
-                            populateDynamicTables(reader, literalTable, 
distanceTable);
-
-                            state = new HuffmanCodes(DYNAMIC_CODES, 
literalTable, distanceTable);
+                            int[][] tables = readDynamicTables();
+                            state = new HuffmanCodes(DYNAMIC_CODES, tables[0], 
tables[1]);
                             break;
                         default:
                             throw new IllegalStateException("Unsupported 
compression: " + mode);
                     }
-                    break;
-                default:
+            } else {
                     return state.read(b, off, len);
             }
         }
         return -1;
     }
 
+    private void switchToUncompressedState() throws IOException {
+        reader.alignWithByteBoundary();
+        long bLen = readBits(16);
+        long bNLen = readBits(16);
+        if (((bLen ^ 0xFFFF) & 0xFFFF) != bNLen) {
+            //noinspection DuplicateStringLiteralInspection
+            throw new IllegalStateException("Illegal LEN / NLEN values");
+        }
+        state = new UncompressedState(bLen);
+    }
+
+    private int[][] readDynamicTables() throws IOException {
+        int[][] result = new int[2][];
+        int literals = (int) (readBits(5) + 257);
+        result[0] = new int[literals];
+
+        int distances = (int) (readBits(5) + 1);
+        result[1] = new int[distances];
+
+        populateDynamicTables(reader, result[0], result[1]);
+        return result;
+    }
+
     int available() throws IOException {
         return state.available();
     }
 
-    private static abstract class DecoderState {
+    private abstract static class DecoderState {
         abstract HuffmanState state();
 
         abstract int read(byte[] b, int off, int len) throws IOException;
@@ -202,20 +209,22 @@ class HuffmanDecoder implements Closeable {
         int read(byte[] b, int off, int len) throws IOException {
             // as len is an int and (blockLength - read) is >= 0 the min must 
fit into an int as well
             int max = (int) Math.min(blockLength - read, len);
-            for (int i = 0; i < max; i++) {
+            int readSoFar = 0;
+            while (readSoFar < max) {
+                int readNow;
                 if (reader.bitsCached() > 0) {
                     byte next = (byte) readBits(Byte.SIZE);
-                    b[off + i] = memory.add(next);
-                    read++;
+                    b[off + readSoFar] = memory.add(next);
+                    readNow = 1;
                 } else {
-                    int readNow = in.read(b, off + i, max - i);
+                    readNow = in.read(b, off + readSoFar, max - readSoFar);
                     if (readNow == -1) {
                         throw new EOFException("Truncated Deflate64 Stream");
                     }
-                    memory.add(b, off + i, readNow);
-                    read += readNow;
-                    i += readNow;
+                    memory.add(b, off + readSoFar, readNow);
                 }
+                read += readNow;
+                readSoFar += readNow;
             }
             return max;
         }
@@ -345,7 +354,7 @@ class HuffmanDecoder implements Closeable {
         BinaryTreeNode node = tree;
         while (node != null && node.literal == -1) {
             long bit = readBits(reader, 1);
-            node = bit == 0 ? node.left : node.right;
+            node = bit == 0 ? node.leftNode : node.rightNode;
         }
         return node != null ? node.literal : -1;
     }
@@ -360,18 +369,20 @@ class HuffmanDecoder implements Closeable {
 
         BinaryTreeNode codeLengthTree = buildTree(codeLengthValues);
 
-        int[] auxBuffer = new int[literals.length + distances.length];
+        final int[] auxBuffer = new int[literals.length + distances.length];
 
-        int value = -1, length = 0;
-        for (int i = 0; i < auxBuffer.length; ) {
+        int value = -1;
+        int length = 0;
+        int off = 0;
+        while (off < auxBuffer.length) {
             if (length > 0) {
-                auxBuffer[i++] = value;
+                auxBuffer[off++] = value;
                 length--;
             } else {
                 int symbol = nextSymbol(reader, codeLengthTree);
                 if (symbol < 16) {
                     value = symbol;
-                    auxBuffer[i++] = value;
+                    auxBuffer[off++] = value;
                 } else if (symbol == 16) {
                     length = (int) (readBits(reader, 2) + 3);
                 } else if (symbol == 17) {
@@ -391,7 +402,8 @@ class HuffmanDecoder implements Closeable {
     private static class BinaryTreeNode {
         private final int bits;
         int literal = -1;
-        BinaryTreeNode left, right;
+        BinaryTreeNode leftNode;
+        BinaryTreeNode rightNode;
 
         private BinaryTreeNode(int bits) {
             this.bits = bits;
@@ -399,22 +411,22 @@ class HuffmanDecoder implements Closeable {
 
         void leaf(int symbol) {
             literal = symbol;
-            left = null;
-            right = null;
+            leftNode = null;
+            rightNode = null;
         }
 
         BinaryTreeNode left() {
-            if (left == null && literal == -1) {
-                left = new BinaryTreeNode(bits + 1);
+            if (leftNode == null && literal == -1) {
+                leftNode = new BinaryTreeNode(bits + 1);
             }
-            return left;
+            return leftNode;
         }
 
         BinaryTreeNode right() {
-            if (right == null && literal == -1) {
-                right = new BinaryTreeNode(bits + 1);
+            if (rightNode == null && literal == -1) {
+                rightNode = new BinaryTreeNode(bits + 1);
             }
-            return right;
+            return rightNode;
         }
     }
 

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/0367fb31/src/main/java/org/apache/commons/compress/compressors/lz77support/LZ77Compressor.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/commons/compress/compressors/lz77support/LZ77Compressor.java
 
b/src/main/java/org/apache/commons/compress/compressors/lz77support/LZ77Compressor.java
index 5086b60..4e494f3 100644
--- 
a/src/main/java/org/apache/commons/compress/compressors/lz77support/LZ77Compressor.java
+++ 
b/src/main/java/org/apache/commons/compress/compressors/lz77support/LZ77Compressor.java
@@ -90,7 +90,7 @@ public class LZ77Compressor {
         public enum BlockType {
             LITERAL, BACK_REFERENCE, EOD
         }
-        abstract public BlockType getType();
+        public abstract BlockType getType();
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/0367fb31/src/main/java/org/apache/commons/compress/compressors/zstandard/ZstdUtils.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/commons/compress/compressors/zstandard/ZstdUtils.java
 
b/src/main/java/org/apache/commons/compress/compressors/zstandard/ZstdUtils.java
index cbbe3cd..8b2f8d3 100644
--- 
a/src/main/java/org/apache/commons/compress/compressors/zstandard/ZstdUtils.java
+++ 
b/src/main/java/org/apache/commons/compress/compressors/zstandard/ZstdUtils.java
@@ -74,7 +74,7 @@ public class ZstdUtils {
         try {
             Class.forName("com.github.luben.zstd.ZstdInputStream");
             return true;
-        } catch (NoClassDefFoundError | Exception error) {
+        } catch (NoClassDefFoundError | Exception error) { // NOSONAR
             return false;
         }
     }

Reply via email to