Skip to content

Commit 1a3a1b3

Browse files
committed
Refactor: Use byte[] from ensureBufferSize() instead of always deferencing it from the context. Use final on lvars to clarify intent and avoid potential bugs.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1378965 13f79535-47bb-0310-9956-ffa450edef68
1 parent 114830d commit 1a3a1b3

1 file changed

Lines changed: 16 additions & 16 deletions

File tree

  • src/main/java/org/apache/commons/codec/binary

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -339,30 +339,30 @@ void encode(byte[] in, int inPos, int inAvail, Context context) {
339339
switch (context.modulus) { // 0-2
340340
case 1 : // 8 bits = 6 + 2
341341
// top 6 bits:
342-
context.buffer[context.pos++] = encodeTable[(context.ibitWorkArea >> 2) & MASK_6BITS];
342+
buffer[context.pos++] = encodeTable[(context.ibitWorkArea >> 2) & MASK_6BITS];
343343
// remaining 2:
344-
context.buffer[context.pos++] = encodeTable[(context.ibitWorkArea << 4) & MASK_6BITS];
344+
buffer[context.pos++] = encodeTable[(context.ibitWorkArea << 4) & MASK_6BITS];
345345
// URL-SAFE skips the padding to further reduce size.
346346
if (encodeTable == STANDARD_ENCODE_TABLE) {
347-
context.buffer[context.pos++] = PAD;
348-
context.buffer[context.pos++] = PAD;
347+
buffer[context.pos++] = PAD;
348+
buffer[context.pos++] = PAD;
349349
}
350350
break;
351351

352352
case 2 : // 16 bits = 6 + 6 + 4
353-
context.buffer[context.pos++] = encodeTable[(context.ibitWorkArea >> 10) & MASK_6BITS];
354-
context.buffer[context.pos++] = encodeTable[(context.ibitWorkArea >> 4) & MASK_6BITS];
355-
context.buffer[context.pos++] = encodeTable[(context.ibitWorkArea << 2) & MASK_6BITS];
353+
buffer[context.pos++] = encodeTable[(context.ibitWorkArea >> 10) & MASK_6BITS];
354+
buffer[context.pos++] = encodeTable[(context.ibitWorkArea >> 4) & MASK_6BITS];
355+
buffer[context.pos++] = encodeTable[(context.ibitWorkArea << 2) & MASK_6BITS];
356356
// URL-SAFE skips the padding to further reduce size.
357357
if (encodeTable == STANDARD_ENCODE_TABLE) {
358-
context.buffer[context.pos++] = PAD;
358+
buffer[context.pos++] = PAD;
359359
}
360360
break;
361361
}
362362
context.currentLinePos += context.pos - savedPos; // keep track of current line position
363363
// if currentPos == 0 we are at the start of a line, so don't add CRLF
364364
if (lineLength > 0 && context.currentLinePos > 0) {
365-
System.arraycopy(lineSeparator, 0, context.buffer, context.pos, lineSeparator.length);
365+
System.arraycopy(lineSeparator, 0, buffer, context.pos, lineSeparator.length);
366366
context.pos += lineSeparator.length;
367367
}
368368
} else {
@@ -375,13 +375,13 @@ void encode(byte[] in, int inPos, int inAvail, Context context) {
375375
}
376376
context.ibitWorkArea = (context.ibitWorkArea << 8) + b; // BITS_PER_BYTE
377377
if (0 == context.modulus) { // 3 bytes = 24 bits = 4 * 6 bits to extract
378-
context.buffer[context.pos++] = encodeTable[(context.ibitWorkArea >> 18) & MASK_6BITS];
379-
context.buffer[context.pos++] = encodeTable[(context.ibitWorkArea >> 12) & MASK_6BITS];
380-
context.buffer[context.pos++] = encodeTable[(context.ibitWorkArea >> 6) & MASK_6BITS];
381-
context.buffer[context.pos++] = encodeTable[context.ibitWorkArea & MASK_6BITS];
378+
buffer[context.pos++] = encodeTable[(context.ibitWorkArea >> 18) & MASK_6BITS];
379+
buffer[context.pos++] = encodeTable[(context.ibitWorkArea >> 12) & MASK_6BITS];
380+
buffer[context.pos++] = encodeTable[(context.ibitWorkArea >> 6) & MASK_6BITS];
381+
buffer[context.pos++] = encodeTable[context.ibitWorkArea & MASK_6BITS];
382382
context.currentLinePos += BYTES_PER_ENCODED_BLOCK;
383383
if (lineLength > 0 && lineLength <= context.currentLinePos) {
384-
System.arraycopy(lineSeparator, 0, context.buffer, context.pos, lineSeparator.length);
384+
System.arraycopy(lineSeparator, 0, buffer, context.pos, lineSeparator.length);
385385
context.pos += lineSeparator.length;
386386
context.currentLinePos = 0;
387387
}
@@ -425,14 +425,14 @@ void decode(byte[] in, int inPos, int inAvail, Context context) {
425425
}
426426
for (int i = 0; i < inAvail; i++) {
427427
final byte[] buffer = ensureBufferSize(decodeSize, context);
428-
byte b = in[inPos++];
428+
final byte b = in[inPos++];
429429
if (b == PAD) {
430430
// We're done.
431431
context.eof = true;
432432
break;
433433
} else {
434434
if (b >= 0 && b < DECODE_TABLE.length) {
435-
int result = DECODE_TABLE[b];
435+
final int result = DECODE_TABLE[b];
436436
if (result >= 0) {
437437
context.modulus = (context.modulus+1) % BYTES_PER_ENCODED_BLOCK;
438438
context.ibitWorkArea = (context.ibitWorkArea << BITS_PER_ENCODED_BYTE) + result;

0 commit comments

Comments
 (0)