Skip to content

Commit afc89eb

Browse files
committed
[CODEC-202] Add BaseNCodec.encode(byte[], int, int) input with offset and length parameters for Base64 and Base32.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1695158 13f79535-47bb-0310-9956-ffa450edef68
1 parent f983a96 commit afc89eb

4 files changed

Lines changed: 1307 additions & 1219 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ The <action> type attribute can be add,update,fix,remove.
4949
<action dev="ggregory" type="add" issue="CODEC-195" due-to="Gary Gregory">Support SHA-224 in DigestUtils on Java 8</action>
5050
<action dev="ggregory" type="add" issue="CODEC-194" due-to="Gary Gregory">Support java.nio.ByteBuffer in org.apache.commons.codec.binary.Hex</action>
5151
<action dev="ggregory" type="add" issue="CODEC-193" due-to="Michael Donaghy">Support java.nio.ByteBuffer in DigestUtils</action>
52+
<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>
5253
</release>
5354
<release version="1.10" date="5 November 2014" description="Feature and fix release.">
5455
<action dev="ggregory" type="add" issue="CODEC-192" due-to="Thomas Neidhart">Add Daitch-Mokotoff Soundex</action>

src/main/java/org/apache/commons/codec/binary/BaseNCodec.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -419,16 +419,36 @@ public byte[] decode(final byte[] pArray) {
419419
*
420420
* @param pArray
421421
* a byte array containing binary data
422-
* @return A byte array containing only the basen alphabetic character data
422+
* @return A byte array containing only the base N alphabetic character data
423423
*/
424424
@Override
425425
public byte[] encode(final byte[] pArray) {
426+
if (pArray == null || pArray.length == 0) {
427+
return pArray;
428+
}
429+
return encode(pArray, 0, pArray.length);
430+
}
431+
432+
/**
433+
* Encodes a byte[] containing binary data, into a byte[] containing
434+
* characters in the alphabet.
435+
*
436+
* @param pArray
437+
* a byte array containing binary data
438+
* @param offset
439+
* initial offset of the subarray.
440+
* @param length
441+
* length of the subarray.
442+
* @return A byte array containing only the base N alphabetic character data
443+
* @since 1.11
444+
*/
445+
public byte[] encode(final byte[] pArray, int offset, int length) {
426446
if (pArray == null || pArray.length == 0) {
427447
return pArray;
428448
}
429449
final Context context = new Context();
430-
encode(pArray, 0, pArray.length, context);
431-
encode(pArray, 0, EOF, context); // Notify encoder of EOF.
450+
encode(pArray, offset, length, context);
451+
encode(pArray, offset, EOF, context); // Notify encoder of EOF.
432452
final byte[] buf = new byte[context.pos - context.readPos];
433453
readResults(buf, 0, buf.length, context);
434454
return buf;

src/test/java/org/apache/commons/codec/binary/Base32Test.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.Arrays;
2727

2828
import org.apache.commons.codec.Charsets;
29+
import org.apache.commons.lang3.ArrayUtils;
2930
import org.junit.Test;
3031

3132
public class Base32Test {
@@ -74,6 +75,31 @@ public class Base32Test {
7475
{"foobar" ,"MZXW6YTBOI%%%%%%"},
7576
};
7677

78+
@Test
79+
public void testBase64AtBufferStart() {
80+
testBase64InBuffer(0, 100);
81+
}
82+
83+
@Test
84+
public void testBase64AtBufferEnd() {
85+
testBase64InBuffer(100, 0);
86+
}
87+
88+
@Test
89+
public void testBase64AtBufferMiddle() {
90+
testBase64InBuffer(100, 100);
91+
}
92+
93+
private void testBase64InBuffer(int startPasSize, int endPadSize) {
94+
final Base32 codec = new Base32();
95+
for (final String[] element : BASE32_TEST_CASES) {
96+
final byte[] bytes = element[0].getBytes(CHARSET_UTF8);
97+
byte[] buffer = ArrayUtils.addAll(bytes, new byte[endPadSize]);
98+
buffer = ArrayUtils.addAll(new byte[startPasSize], buffer);
99+
assertEquals(element[1], StringUtils.newStringUtf8(codec.encode(buffer, startPasSize, bytes.length)));
100+
}
101+
}
102+
77103
@Test
78104
public void testBase32Chunked () throws Exception {
79105
final Base32 codec = new Base32(20);

0 commit comments

Comments
 (0)