Skip to content

Commit 065b106

Browse files
committed
Address code-review comments by @aherbert
1 parent 0b672eb commit 065b106

2 files changed

Lines changed: 20 additions & 18 deletions

File tree

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,14 @@ public Base16(final boolean lowerCase, final CodecPolicy decodingPolicy) {
149149
void decode(final byte[] data, int offset, final int length, final Context context) {
150150
if (context.eof || length < 0) {
151151
context.eof = true;
152-
if (context.ibitWorkArea > 0) {
152+
if (context.ibitWorkArea != 0) {
153153
validateTrailingCharacter();
154154
}
155155
return;
156156
}
157157

158158
final int dataLen = Math.min(data.length - offset, length);
159-
final int availableChars = (context.ibitWorkArea > 0 ? 1 : 0) + dataLen;
159+
final int availableChars = (context.ibitWorkArea != 0 ? 1 : 0) + dataLen;
160160

161161
// small optimisation to short-cut the rest of this method when it is fed byte-by-byte
162162
if (availableChars == 1 && availableChars == dataLen) {
@@ -198,7 +198,7 @@ void decode(final byte[] data, int offset, final int length, final Context conte
198198

199199
private int decodeOctet(final byte octet) {
200200
int decoded = -1;
201-
if (octet >= 0 && octet < decodeTable.length) {
201+
if ((octet & 0xff) < decodeTable.length) {
202202
decoded = decodeTable[octet];
203203
}
204204

@@ -241,7 +241,7 @@ void encode(final byte[] data, final int offset, final int length, final Context
241241
*/
242242
@Override
243243
public boolean isInAlphabet(final byte octet) {
244-
return octet >= 0 && octet < decodeTable.length && decodeTable[octet] != -1;
244+
return (octet & 0xff) < decodeTable.length && decodeTable[octet] != -1;
245245
}
246246

247247
/**

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import static org.junit.Assert.assertArrayEquals;
3333
import static org.junit.Assert.assertEquals;
3434
import static org.junit.Assert.assertFalse;
35+
import static org.junit.Assert.assertNull;
3536
import static org.junit.Assert.assertTrue;
3637
import static org.junit.Assert.fail;
3738

@@ -116,20 +117,20 @@ public void testConstructors() {
116117

117118
@Test
118119
public void testConstructor_LowerCase() {
119-
final Base16 Base16 = new Base16(true);
120-
final byte[] encoded = Base16.encode(BaseNTestData.DECODED);
120+
final Base16 base16 = new Base16(true);
121+
final byte[] encoded = base16.encode(BaseNTestData.DECODED);
121122
final String expectedResult = Base16TestData.ENCODED_UTF8_LOWERCASE;
122123
final String result = StringUtils.newStringUtf8(encoded);
123124
assertEquals("new Base16(true)", expectedResult, result);
124125
}
125126

126127
@Test
127128
public void testConstructor_LowerCase_DecodingPolicy() {
128-
final Base16 Base16 = new Base16(false, CodecPolicy.STRICT);
129-
final byte[] encoded = Base16.encode(BaseNTestData.DECODED);
129+
final Base16 base16 = new Base16(false, CodecPolicy.STRICT);
130+
final byte[] encoded = base16.encode(BaseNTestData.DECODED);
130131
final String expectedResult = Base16TestData.ENCODED_UTF8_UPPERCASE;
131132
final String result = StringUtils.newStringUtf8(encoded);
132-
assertEquals("new Base16(false, CodecPolicy.STRICT)", result, expectedResult);
133+
assertEquals("new base16(false, CodecPolicy.STRICT)", result, expectedResult);
133134
}
134135

135136
/**
@@ -435,27 +436,27 @@ public void testTriplets() {
435436

436437
@Test
437438
public void testByteToStringVariations() throws DecoderException {
438-
final Base16 Base16 = new Base16();
439+
final Base16 base16 = new Base16();
439440
final byte[] b1 = StringUtils.getBytesUtf8("Hello World");
440441
final byte[] b2 = new byte[0];
441442
final byte[] b3 = null;
442443

443-
assertEquals("byteToString Hello World", "48656C6C6F20576F726C64", Base16.encodeToString(b1));
444+
assertEquals("byteToString Hello World", "48656C6C6F20576F726C64", base16.encodeToString(b1));
444445
assertEquals("byteToString static Hello World", "48656C6C6F20576F726C64", StringUtils.newStringUtf8(new Base16().encode(b1)));
445-
assertEquals("byteToString \"\"", "", Base16.encodeToString(b2));
446+
assertEquals("byteToString \"\"", "", base16.encodeToString(b2));
446447
assertEquals("byteToString static \"\"", "", StringUtils.newStringUtf8(new Base16().encode(b2)));
447-
assertEquals("byteToString null", null, Base16.encodeToString(b3));
448+
assertEquals("byteToString null", null, base16.encodeToString(b3));
448449
assertEquals("byteToString static null", null, StringUtils.newStringUtf8(new Base16().encode(b3)));
449450
}
450451

451452
@Test
452453
public void testStringToByteVariations() throws DecoderException {
453-
final Base16 Base16 = new Base16();
454+
final Base16 base16 = new Base16();
454455
final String s1 = "48656C6C6F20576F726C64";
455456
final String s2 = "";
456457
final String s3 = null;
457458

458-
assertEquals("StringToByte Hello World", "Hello World", StringUtils.newStringUtf8(Base16.decode(s1)));
459+
assertEquals("StringToByte Hello World", "Hello World", StringUtils.newStringUtf8(base16.decode(s1)));
459460
assertEquals("StringToByte Hello World", "Hello World",
460461
StringUtils.newStringUtf8((byte[]) new Base16().decode((Object) s1)));
461462
assertEquals("StringToByte static Hello World", "Hello World",
@@ -587,22 +588,23 @@ public void testDecodeSingleBytes() {
587588
@Test
588589
public void testDecodeSingleBytesOptimisation() {
589590
final BaseNCodec.Context context = new BaseNCodec.Context();
590-
context.ibitWorkArea = 0;
591+
assertEquals(0, context.ibitWorkArea);
592+
assertNull(context.buffer);
591593

592594
final byte[] data = new byte[1];
593595

594596
final Base16 b16 = new Base16();
595-
assertEquals(0, context.ibitWorkArea);
596597

597598
data[0] = (byte) 'E';
598599
b16.decode(data, 0, 1, context);
599600
assertEquals(15, context.ibitWorkArea);
601+
assertNull(context.buffer);
600602

601603
data[0] = (byte) 'F';
602604
b16.decode(data, 0, 1, context);
603605
assertEquals(0, context.ibitWorkArea);
604606

605-
assertEquals(-17, context.buffer[0]);
607+
assertEquals((byte)0xEF, context.buffer[0]);
606608
}
607609

608610
@Test(expected=IllegalArgumentException.class)

0 commit comments

Comments
 (0)