commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject [10/12] commons-compress git commit: mostly cosmetic changes
Date Wed, 05 Jul 2017 15:34:29 GMT
mostly cosmetic changes


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

Branch: refs/heads/master
Commit: fde66702c33d3888b682adc7e6c35dd21cb1e8da
Parents: 90a73a4
Author: Stefan Bodewig <bodewig@apache.org>
Authored: Wed Jul 5 17:31:40 2017 +0200
Committer: Stefan Bodewig <bodewig@apache.org>
Committed: Wed Jul 5 17:31:40 2017 +0200

----------------------------------------------------------------------
 .../compress/archivers/zip/HasCharset.java      |  1 +
 .../compress/archivers/zip/NioZipEncoding.java  | 29 ++++++++++----------
 .../archivers/zip/ZipEncodingHelper.java        |  9 +++---
 .../compress/archivers/zip/ZipEncodingTest.java |  9 +++---
 4 files changed, 24 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-compress/blob/fde66702/src/main/java/org/apache/commons/compress/archivers/zip/HasCharset.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/HasCharset.java b/src/main/java/org/apache/commons/compress/archivers/zip/HasCharset.java
index 7581c18..b370a01 100644
--- a/src/main/java/org/apache/commons/compress/archivers/zip/HasCharset.java
+++ b/src/main/java/org/apache/commons/compress/archivers/zip/HasCharset.java
@@ -28,6 +28,7 @@ import java.nio.charset.Charset;
  * This avoids introducing a
  * potentially breaking change, or making {@link NioZipEncoding} a public class.
  * </p>
+ * @since 1.15
  */
 public interface HasCharset {
 

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/fde66702/src/main/java/org/apache/commons/compress/archivers/zip/NioZipEncoding.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/NioZipEncoding.java b/src/main/java/org/apache/commons/compress/archivers/zip/NioZipEncoding.java
index 606ab12..4b0069f 100644
--- a/src/main/java/org/apache/commons/compress/archivers/zip/NioZipEncoding.java
+++ b/src/main/java/org/apache/commons/compress/archivers/zip/NioZipEncoding.java
@@ -34,12 +34,13 @@ import java.nio.charset.CodingErrorAction;
  * <p>The methods of this class are reentrant.</p>
  * @Immutable
  */
-class NioZipEncoding implements ZipEncoding,HasCharset {
+class NioZipEncoding implements ZipEncoding, HasCharset {
 
     private final Charset charset;
-    private  boolean useReplacement= false;
-    private static final byte[] REPLACEMENT_BYTES = new byte[]{'?'};
-    private static final String REPLACEMENT_STRING = "?";
+    private final boolean useReplacement;
+    private static final char REPLACEMENT = '?';
+    private static final byte[] REPLACEMENT_BYTES = { (byte) REPLACEMENT };
+    private static final String REPLACEMENT_STRING = String.valueOf(REPLACEMENT);
 
     /**
      * Construct an NioZipEncoding using the given charset.
@@ -49,7 +50,6 @@ class NioZipEncoding implements ZipEncoding,HasCharset {
     NioZipEncoding(final Charset charset, boolean useReplacement) {
         this.charset = charset;
         this.useReplacement = useReplacement;
-
     }
 
     @Override
@@ -102,7 +102,7 @@ class NioZipEncoding implements ZipEncoding,HasCharset {
         final CharsetEncoder enc = newEncoder();
 
         final CharBuffer cb = CharBuffer.wrap(name);
-        CharBuffer tmp=null;
+        CharBuffer tmp = null;
         ByteBuffer out = ByteBuffer.allocate(estimateInitialBufferSize(enc, cb.remaining()));
 
         while (cb.remaining() > 0) {
@@ -127,13 +127,13 @@ class NioZipEncoding implements ZipEncoding,HasCharset {
                         }
                     }
                     int totalExtraSpace = estimateIncrementalEncodingSize(enc, charCount);
-                    out = ZipEncodingHelper.growBufferBy(out, totalExtraSpace- out.remaining());
+                    out = ZipEncodingHelper.growBufferBy(out, totalExtraSpace - out.remaining());
                 }
-                if(tmp == null) {
+                if (tmp == null) {
                     tmp = CharBuffer.allocate(6);
                 }
                 for (int i = 0; i < res.length(); ++i) {
-                    out = encodeFully(enc, encodeSurrogate(tmp,cb.get()), out);
+                    out = encodeFully(enc, encodeSurrogate(tmp, cb.get()), out);
                 }
 
             } else if (res.isOverflow()) {
@@ -143,8 +143,7 @@ class NioZipEncoding implements ZipEncoding,HasCharset {
         }
         CoderResult coderResult = enc.encode(cb, out, true);
 
-        assert coderResult.isUnderflow() : "unexpected coder result: " + coderResult;
-        
+        // may have caused underflow, but that's been ignored traditionally
 
         out.limit(out.position());
         out.rewind();
@@ -157,16 +156,16 @@ class NioZipEncoding implements ZipEncoding,HasCharset {
             if (result.isOverflow()) {
                 int increment = estimateIncrementalEncodingSize(enc, cb.remaining());
                 out = ZipEncodingHelper.growBufferBy(out, increment);
-            } 
+            }
         }
         return out;
     }
 
-    static char[] HEX_CHARS = new char[]{
+    private static final char[] HEX_CHARS = new char[] {
         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
     };
 
-    private CharBuffer encodeSurrogate( CharBuffer cb,char c) {
+    private static CharBuffer encodeSurrogate(CharBuffer cb, char c) {
         cb.position(0).limit(6);
         cb.put('%');
         cb.put('U');
@@ -191,7 +190,7 @@ class NioZipEncoding implements ZipEncoding,HasCharset {
      * @param charChount number of characters in string
      * @return estimated size in bytes.
      */
-    private int estimateInitialBufferSize(CharsetEncoder enc, int charChount) {
+    private static int estimateInitialBufferSize(CharsetEncoder enc, int charChount) {
         float first = enc.maxBytesPerChar();
         float rest = (charChount - 1) * enc.averageBytesPerChar();
         return (int) Math.ceil(first + rest);

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/fde66702/src/main/java/org/apache/commons/compress/archivers/zip/ZipEncodingHelper.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipEncodingHelper.java
b/src/main/java/org/apache/commons/compress/archivers/zip/ZipEncodingHelper.java
index fb550fd..68a6305 100644
--- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipEncodingHelper.java
+++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipEncodingHelper.java
@@ -46,9 +46,9 @@ public abstract class ZipEncodingHelper {
     static final String UTF8 = "UTF8";
 
     /**
-     * name of the encoding UTF-8
+     * the encoding UTF-8
      */
-    static final ZipEncoding UTF8_ZIP_ENCODING = getZipEncoding("UTF-8");
+    static final ZipEncoding UTF8_ZIP_ENCODING = getZipEncoding(UTF8);
 
     /**
      * Instantiates a zip encoding. An NIO based character set encoder/decoder will be returned.
@@ -67,12 +67,11 @@ public abstract class ZipEncodingHelper {
         if (name != null) {
             try {
                 cs = Charset.forName(name);
-            } catch (UnsupportedCharsetException e) {
+            } catch (UnsupportedCharsetException e) { // NOSONAR we use the default encoding
instead
             }
         }
-        boolean useReplacement = cs.name().equals("UTF-8");
+        boolean useReplacement = isUTF8(cs.name());
         return new NioZipEncoding(cs, useReplacement);
-
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/fde66702/src/test/java/org/apache/commons/compress/archivers/zip/ZipEncodingTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ZipEncodingTest.java
b/src/test/java/org/apache/commons/compress/archivers/zip/ZipEncodingTest.java
index 34a9cb8..15273c7 100644
--- a/src/test/java/org/apache/commons/compress/archivers/zip/ZipEncodingTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/zip/ZipEncodingTest.java
@@ -51,13 +51,14 @@ public class ZipEncodingTest {
         };
         assertNotNull(o);
     }
+
     @Test
     public void testGetNonexistentEncodng() throws IOException {
         ZipEncoding ze = ZipEncodingHelper.getZipEncoding("I-am-a-banana");
         assertNotNull(ze);
         if (ze instanceof HasCharset) {
             HasCharset hasCharset = (HasCharset) ze;
-            Assert.assertEquals(Charset.defaultCharset(),hasCharset.getCharset());
+            Assert.assertEquals(Charset.defaultCharset(), hasCharset.getCharset());
         }
     }
 
@@ -65,18 +66,19 @@ public class ZipEncodingTest {
     public void testIsUTF8() throws IOException {
        assertTrue(ZipEncodingHelper.isUTF8("UTF-8"));
        assertTrue(ZipEncodingHelper.isUTF8("UTF8"));
-       Assert.assertEquals(Charset.defaultCharset().name().equals("UTF-8"),ZipEncodingHelper.isUTF8(null));
+       Assert.assertEquals(Charset.defaultCharset().name().equals("UTF-8"), ZipEncodingHelper.isUTF8(null));
     }
+
     @Test
     public void testSimpleCp437Encoding() throws IOException {
         doSimpleEncodingsTest(437);
     }
+
     @Test
     public void testSimpleCp850Encoding() throws IOException {
         doSimpleEncodingsTest(850);
     }
 
-
     @Test
     public void testEbcidic() throws IOException {
 
@@ -187,7 +189,6 @@ public class ZipEncodingTest {
         assertEquals("%U2016".getBytes(name), enc.encode(UNENC_STRING));
         assertFalse(enc.canEncode(BAD_STRING));
         assertEquals(BAD_STRING_ENC.getBytes(name), enc.encode(BAD_STRING));
-        assertEquals(BAD_STRING_ENC.getBytes(name), enc.encode(BAD_STRING));
     }
 
 }


Mime
View raw message