commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1695158 [1/2] - in /commons/proper/codec/trunk/src: changes/ main/java/org/apache/commons/codec/binary/ test/java/org/apache/commons/codec/binary/
Date Mon, 10 Aug 2015 19:44:56 GMT
Author: ggregory
Date: Mon Aug 10 19:44:55 2015
New Revision: 1695158

URL: http://svn.apache.org/r1695158
Log:
[CODEC-202] Add BaseNCodec.encode(byte[], int, int) input with offset and length parameters
for Base64 and Base32.

Modified:
    commons/proper/codec/trunk/src/changes/changes.xml
    commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/binary/BaseNCodec.java
    commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/Base32Test.java
    commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/Base64Test.java

Modified: commons/proper/codec/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/changes/changes.xml?rev=1695158&r1=1695157&r2=1695158&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/changes/changes.xml (original)
+++ commons/proper/codec/trunk/src/changes/changes.xml Mon Aug 10 19:44:55 2015
@@ -49,6 +49,7 @@ The <action> type attribute can be add,u
       <action dev="ggregory" type="add" issue="CODEC-195" due-to="Gary Gregory">Support
SHA-224 in DigestUtils on Java 8</action>         
       <action dev="ggregory" type="add" issue="CODEC-194" due-to="Gary Gregory">Support
java.nio.ByteBuffer in org.apache.commons.codec.binary.Hex</action>         
       <action dev="ggregory" type="add" issue="CODEC-193" due-to="Michael Donaghy">Support
java.nio.ByteBuffer in DigestUtils</action>         
+      <action dev="ggregory" type="add" issue="CODEC-202" due-to="Oleg Kalnichevski">Add
BaseNCodec.encode(byte[], int, int) input with offset and length parameters for Base64 and
Base32.</action>         
     </release>   
     <release version="1.10" date="5 November 2014" description="Feature and fix release.">
       <action dev="ggregory" type="add" issue="CODEC-192" due-to="Thomas Neidhart">Add
Daitch-Mokotoff Soundex</action>   

Modified: commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/binary/BaseNCodec.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/binary/BaseNCodec.java?rev=1695158&r1=1695157&r2=1695158&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/binary/BaseNCodec.java
(original)
+++ commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/binary/BaseNCodec.java
Mon Aug 10 19:44:55 2015
@@ -419,16 +419,36 @@ public abstract class BaseNCodec impleme
      *
      * @param pArray
      *            a byte array containing binary data
-     * @return A byte array containing only the basen alphabetic character data
+     * @return A byte array containing only the base N alphabetic character data
      */
     @Override
     public byte[] encode(final byte[] pArray) {
         if (pArray == null || pArray.length == 0) {
             return pArray;
         }
+        return encode(pArray, 0, pArray.length);
+    }
+
+	/**
+	 * Encodes a byte[] containing binary data, into a byte[] containing
+	 * characters in the alphabet.
+	 *
+	 * @param pArray
+	 *            a byte array containing binary data
+	 * @param offset
+	 *            initial offset of the subarray.
+	 * @param length
+	 *            length of the subarray.
+	 * @return A byte array containing only the base N alphabetic character data
+	 * @since 1.11
+	 */
+    public byte[] encode(final byte[] pArray, int offset, int length) {
+        if (pArray == null || pArray.length == 0) {
+            return pArray;
+        }
         final Context context = new Context();
-        encode(pArray, 0, pArray.length, context);
-        encode(pArray, 0, EOF, context); // Notify encoder of EOF.
+        encode(pArray, offset, length, context);
+        encode(pArray, offset, EOF, context); // Notify encoder of EOF.
         final byte[] buf = new byte[context.pos - context.readPos];
         readResults(buf, 0, buf.length, context);
         return buf;

Modified: commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/Base32Test.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/Base32Test.java?rev=1695158&r1=1695157&r2=1695158&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/Base32Test.java
(original)
+++ commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/Base32Test.java
Mon Aug 10 19:44:55 2015
@@ -26,6 +26,7 @@ import java.nio.charset.Charset;
 import java.util.Arrays;
 
 import org.apache.commons.codec.Charsets;
+import org.apache.commons.lang3.ArrayUtils;
 import org.junit.Test;
 
 public class Base32Test {
@@ -74,6 +75,31 @@ public class Base32Test {
         {"foobar" ,"MZXW6YTBOI%%%%%%"},
     };
 
+	@Test
+	public void testBase64AtBufferStart() {
+		testBase64InBuffer(0, 100);
+	}
+
+	@Test
+	public void testBase64AtBufferEnd() {
+		testBase64InBuffer(100, 0);
+	}
+
+	@Test
+	public void testBase64AtBufferMiddle() {
+		testBase64InBuffer(100, 100);
+	}
+
+	private void testBase64InBuffer(int startPasSize, int endPadSize) {
+		final Base32 codec = new Base32();
+		for (final String[] element : BASE32_TEST_CASES) {
+			final byte[] bytes = element[0].getBytes(CHARSET_UTF8);
+			byte[] buffer = ArrayUtils.addAll(bytes, new byte[endPadSize]);
+			buffer = ArrayUtils.addAll(new byte[startPasSize], buffer);
+			assertEquals(element[1], StringUtils.newStringUtf8(codec.encode(buffer, startPasSize,
bytes.length)));
+		}
+	}
+
     @Test
     public void testBase32Chunked () throws Exception {
         final Base32 codec = new Base32(20);



Mime
View raw message