You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2011/01/28 02:16:17 UTC

svn commit: r1064407 - in /commons/proper/codec/trunk/src: java/org/apache/commons/codec/binary/Base32.java java/org/apache/commons/codec/binary/BaseNCodec.java test/org/apache/commons/codec/binary/Base32Test.java

Author: sebb
Date: Fri Jan 28 01:16:16 2011
New Revision: 1064407

URL: http://svn.apache.org/viewvc?rev=1064407&view=rev
Log:
bitWorkArea needs to be in implementation class
Allow static access to default pad
Fix bug in Base32 chunking and add some tests

Modified:
    commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base32.java
    commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/BaseNCodec.java
    commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base32Test.java

Modified: commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base32.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base32.java?rev=1064407&r1=1064406&r2=1064407&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base32.java (original)
+++ commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base32.java Fri Jan 28 01:16:16 2011
@@ -143,6 +143,12 @@ public class Base32 extends BaseNCodec {
     private final int encodeSize;
 
     /**
+     * Place holder for the bytes we're dealing with for our based logic. 
+     * Bitwise operations store and extract the encoding or decoding from this variable.
+     */
+    private long bitWorkArea;
+
+    /**
      * Creates a Base32 codec used for decoding and encoding.
      * <p>
      * When encoding the line length is 0 (no chunking).
@@ -281,10 +287,11 @@ public class Base32 extends BaseNCodec {
         // encoding.
         if (inAvail < 0) {
             eof = true;
-            if (0 == modulus) {
-                return; // no leftovers to process
+            if (0 == modulus && lineLength == 0) {
+                return; // no leftovers to process and not using chunking
             }
             ensureBufferSize(encodeSize);
+            int savedPos = pos;
             switch (modulus) { // % 5
                 case 1 : // Only 1 octet; take top 5 bits then remainder
                     buffer[pos++] = encodeTable[(int)(bitWorkArea >> 3) & MASK_5BITS]; // 8-1*5 = 3
@@ -328,14 +335,16 @@ public class Base32 extends BaseNCodec {
                     buffer[pos++] = PAD;
                     break;
             }
-            if (lineLength > 0){ // add chunk separator if required
+            currentLinePos += pos - savedPos; // keep track of current line position
+            // if currentPos == 0 we are at the start of a line, so don't add CRLF
+            if (lineLength > 0 && currentLinePos > 0){ // add chunk separator if required
                 System.arraycopy(lineSeparator, 0, buffer, pos, lineSeparator.length);
                 pos += lineSeparator.length;
             }            
         } else {
             for (int i = 0; i < inAvail; i++) {
                 ensureBufferSize(encodeSize);
-                modulus = (++modulus) % BITS_PER_ENCODED_BYTE;
+                modulus = (++modulus) % BYTES_PER_UNENCODED_BLOCK;
                 int b = in[inPos++];
                 if (b < 0) {
                     b += 256;

Modified: commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/BaseNCodec.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/BaseNCodec.java?rev=1064407&r1=1064406&r2=1064407&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/BaseNCodec.java (original)
+++ commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/BaseNCodec.java Fri Jan 28 01:16:16 2011
@@ -69,7 +69,9 @@ public abstract class BaseNCodec impleme
     /**
      * Byte used to pad output.
      */
-    protected final byte PAD = '='; // instance variable just in case it needs to vary later
+    protected static final byte PAD_DEFAULT = '='; // Allow static access to default
+    
+    protected final byte PAD = PAD_DEFAULT; // instance variable just in case it needs to vary later
 
     /** Number of bytes in each full block of unencoded data, e.g. 4 for Base64 and 5 for Base32 */
     private final int unencodedBlockSize;
@@ -111,12 +113,6 @@ public abstract class BaseNCodec impleme
     protected boolean eof;
 
     /**
-     * Place holder for the bytes we're dealing with for our based logic. 
-     * Bitwise operations store and extract the encoding or decoding from this variable.
-     */
-    protected long bitWorkArea;
-
-    /**
      * Variable tracks how many characters have been written to the current line. Only used when encoding. We use it to
      * make sure each encoded line never goes beyond lineLength (if lineLength > 0).
      */

Modified: commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base32Test.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base32Test.java?rev=1064407&r1=1064406&r2=1064407&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base32Test.java (original)
+++ commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base32Test.java Fri Jan 28 01:16:16 2011
@@ -45,6 +45,16 @@ public class Base32Test extends TestCase
     };
 
 
+    private static final String [][] BASE32_TEST_CASES_CHUNKED = { //Chunked
+        {""       ,""},
+        {"f"      ,"MY======\r\n"},
+        {"fo"     ,"MZXQ====\r\n"},
+        {"foo"    ,"MZXW6===\r\n"},
+        {"foob"   ,"MZXW6YQ=\r\n"},
+        {"fooba"  ,"MZXW6YTB\r\n"},
+        {"foobar" ,"MZXW6YTBOI======\r\n"},
+    };
+
     public void testBase32Samples() throws Exception {
         Base32 codec = new Base32();
         for (int i = 0; i < BASE32_TEST_CASES.length; i++) {
@@ -59,6 +69,13 @@ public class Base32Test extends TestCase
         }
     }
 
+    public void testBase32Chunked () throws Exception {
+        Base32 codec = new Base32(20);
+        for (int i = 0; i < BASE32_TEST_CASES_CHUNKED.length; i++) {
+                assertEquals(BASE32_TEST_CASES_CHUNKED[i][1], codec.encodeAsString(BASE32_TEST_CASES_CHUNKED[i][0].getBytes("UTF-8")));
+        }        
+    }
+
     public void testSingleCharEncoding() {
         for (int i = 0; i < 20; i++) {
             Base32 codec = new Base32();