You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2012/08/30 15:56:54 UTC
svn commit: r1378941 - in
/commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/binary:
Base32.java BaseNCodec.java
Author: ggregory
Date: Thu Aug 30 13:56:54 2012
New Revision: 1378941
URL: http://svn.apache.org/viewvc?rev=1378941&view=rev
Log:
Refactor but do not create new methods (presumably for performance). Start to simplify use of Context and what feels like side-effect programming. Also ends-up reducing line complexity (length) which makes Checksyle happy.
Modified:
commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/binary/Base32.java
commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/binary/BaseNCodec.java
Modified: commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/binary/Base32.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/binary/Base32.java?rev=1378941&r1=1378940&r2=1378941&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/binary/Base32.java (original)
+++ commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/binary/Base32.java Thu Aug 30 13:56:54 2012
@@ -383,60 +383,60 @@ public class Base32 extends BaseNCodec {
if (0 == context.modulus && lineLength == 0) {
return; // no leftovers to process and not using chunking
}
- ensureBufferSize(encodeSize, context);
+ final byte[] buffer = ensureBufferSize(encodeSize, context);
int savedPos = context.pos;
switch (context.modulus) { // % 5
case 1 : // Only 1 octet; take top 5 bits then remainder
- context.buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 3) & MASK_5BITS]; // 8-1*5 = 3
- context.buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea << 2) & MASK_5BITS]; // 5-3=2
- context.buffer[context.pos++] = PAD;
- context.buffer[context.pos++] = PAD;
- context.buffer[context.pos++] = PAD;
- context.buffer[context.pos++] = PAD;
- context.buffer[context.pos++] = PAD;
- context.buffer[context.pos++] = PAD;
+ buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 3) & MASK_5BITS]; // 8-1*5 = 3
+ buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea << 2) & MASK_5BITS]; // 5-3=2
+ buffer[context.pos++] = PAD;
+ buffer[context.pos++] = PAD;
+ buffer[context.pos++] = PAD;
+ buffer[context.pos++] = PAD;
+ buffer[context.pos++] = PAD;
+ buffer[context.pos++] = PAD;
break;
case 2 : // 2 octets = 16 bits to use
- context.buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 11) & MASK_5BITS]; // 16-1*5 = 11
- context.buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 6) & MASK_5BITS]; // 16-2*5 = 6
- context.buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 1) & MASK_5BITS]; // 16-3*5 = 1
- context.buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea << 4) & MASK_5BITS]; // 5-1 = 4
- context.buffer[context.pos++] = PAD;
- context.buffer[context.pos++] = PAD;
- context.buffer[context.pos++] = PAD;
- context.buffer[context.pos++] = PAD;
+ buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 11) & MASK_5BITS]; // 16-1*5 = 11
+ buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 6) & MASK_5BITS]; // 16-2*5 = 6
+ buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 1) & MASK_5BITS]; // 16-3*5 = 1
+ buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea << 4) & MASK_5BITS]; // 5-1 = 4
+ buffer[context.pos++] = PAD;
+ buffer[context.pos++] = PAD;
+ buffer[context.pos++] = PAD;
+ buffer[context.pos++] = PAD;
break;
case 3 : // 3 octets = 24 bits to use
- context.buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 19) & MASK_5BITS]; // 24-1*5 = 19
- context.buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 14) & MASK_5BITS]; // 24-2*5 = 14
- context.buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 9) & MASK_5BITS]; // 24-3*5 = 9
- context.buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 4) & MASK_5BITS]; // 24-4*5 = 4
- context.buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea << 1) & MASK_5BITS]; // 5-4 = 1
- context.buffer[context.pos++] = PAD;
- context.buffer[context.pos++] = PAD;
- context.buffer[context.pos++] = PAD;
+ buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 19) & MASK_5BITS]; // 24-1*5 = 19
+ buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 14) & MASK_5BITS]; // 24-2*5 = 14
+ buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 9) & MASK_5BITS]; // 24-3*5 = 9
+ buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 4) & MASK_5BITS]; // 24-4*5 = 4
+ buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea << 1) & MASK_5BITS]; // 5-4 = 1
+ buffer[context.pos++] = PAD;
+ buffer[context.pos++] = PAD;
+ buffer[context.pos++] = PAD;
break;
case 4 : // 4 octets = 32 bits to use
- context.buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 27) & MASK_5BITS]; // 32-1*5 = 27
- context.buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 22) & MASK_5BITS]; // 32-2*5 = 22
- context.buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 17) & MASK_5BITS]; // 32-3*5 = 17
- context.buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 12) & MASK_5BITS]; // 32-4*5 = 12
- context.buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 7) & MASK_5BITS]; // 32-5*5 = 7
- context.buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 2) & MASK_5BITS]; // 32-6*5 = 2
- context.buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea << 3) & MASK_5BITS]; // 5-2 = 3
- context.buffer[context.pos++] = PAD;
+ buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 27) & MASK_5BITS]; // 32-1*5 = 27
+ buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 22) & MASK_5BITS]; // 32-2*5 = 22
+ buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 17) & MASK_5BITS]; // 32-3*5 = 17
+ buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 12) & MASK_5BITS]; // 32-4*5 = 12
+ buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 7) & MASK_5BITS]; // 32-5*5 = 7
+ buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 2) & MASK_5BITS]; // 32-6*5 = 2
+ buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea << 3) & MASK_5BITS]; // 5-2 = 3
+ buffer[context.pos++] = PAD;
break;
}
context.currentLinePos += context.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 && context.currentLinePos > 0){ // add chunk separator if required
- System.arraycopy(lineSeparator, 0, context.buffer, context.pos, lineSeparator.length);
+ System.arraycopy(lineSeparator, 0, buffer, context.pos, lineSeparator.length);
context.pos += lineSeparator.length;
}
} else {
for (int i = 0; i < inAvail; i++) {
- ensureBufferSize(encodeSize, context);
+ final byte[] buffer = ensureBufferSize(encodeSize, context);
context.modulus = (context.modulus+1) % BYTES_PER_UNENCODED_BLOCK;
int b = in[inPos++];
if (b < 0) {
@@ -444,17 +444,17 @@ public class Base32 extends BaseNCodec {
}
context.lbitWorkArea = (context.lbitWorkArea << 8) + b; // BITS_PER_BYTE
if (0 == context.modulus) { // we have enough bytes to create our output
- context.buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 35) & MASK_5BITS];
- context.buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 30) & MASK_5BITS];
- context.buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 25) & MASK_5BITS];
- context.buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 20) & MASK_5BITS];
- context.buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 15) & MASK_5BITS];
- context.buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 10) & MASK_5BITS];
- context.buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 5) & MASK_5BITS];
- context.buffer[context.pos++] = encodeTable[(int)context.lbitWorkArea & MASK_5BITS];
+ buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 35) & MASK_5BITS];
+ buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 30) & MASK_5BITS];
+ buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 25) & MASK_5BITS];
+ buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 20) & MASK_5BITS];
+ buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 15) & MASK_5BITS];
+ buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 10) & MASK_5BITS];
+ buffer[context.pos++] = encodeTable[(int)(context.lbitWorkArea >> 5) & MASK_5BITS];
+ buffer[context.pos++] = encodeTable[(int)context.lbitWorkArea & MASK_5BITS];
context.currentLinePos += BYTES_PER_ENCODED_BLOCK;
if (lineLength > 0 && lineLength <= context.currentLinePos) {
- System.arraycopy(lineSeparator, 0, context.buffer, context.pos, lineSeparator.length);
+ System.arraycopy(lineSeparator, 0, buffer, context.pos, lineSeparator.length);
context.pos += lineSeparator.length;
context.currentLinePos = 0;
}
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=1378941&r1=1378940&r2=1378941&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 Thu Aug 30 13:56:54 2012
@@ -207,7 +207,7 @@ public abstract class BaseNCodec impleme
* Increases our buffer by the {@link #DEFAULT_BUFFER_RESIZE_FACTOR}.
* @param context the context to be used
*/
- private void resizeBuffer(Context context) {
+ private byte[] resizeBuffer(Context context) {
if (context.buffer == null) {
context.buffer = new byte[getDefaultBufferSize()];
context.pos = 0;
@@ -217,6 +217,7 @@ public abstract class BaseNCodec impleme
System.arraycopy(context.buffer, 0, b, 0, context.buffer.length);
context.buffer = b;
}
+ return context.buffer;
}
/**
@@ -225,10 +226,11 @@ public abstract class BaseNCodec impleme
* @param size minimum spare space required
* @param context the context to be used
*/
- protected void ensureBufferSize(int size, Context context){
+ protected byte[] ensureBufferSize(int size, Context context){
if ((context.buffer == null) || (context.buffer.length < context.pos + size)){
- resizeBuffer(context);
+ return resizeBuffer(context);
}
+ return context.buffer;
}
/**