You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by jv...@apache.org on 2008/08/13 12:23:35 UTC
svn commit: r685502 -
/mina/trunk/core/src/main/java/org/apache/mina/util/Base64.java
Author: jvermillard
Date: Wed Aug 13 03:23:32 2008
New Revision: 685502
URL: http://svn.apache.org/viewvc?rev=685502&view=rev
Log:
forgot to format Base64.java
Modified:
mina/trunk/core/src/main/java/org/apache/mina/util/Base64.java
Modified: mina/trunk/core/src/main/java/org/apache/mina/util/Base64.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/util/Base64.java?rev=685502&r1=685501&r2=685502&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/util/Base64.java (original)
+++ mina/trunk/core/src/main/java/org/apache/mina/util/Base64.java Wed Aug 13 03:23:32 2008
@@ -88,7 +88,7 @@
* Used to test the sign of a byte.
*/
static final int SIGN = -128;
-
+
/**
* Byte used to pad output.
*/
@@ -97,6 +97,7 @@
// Create arrays to hold the base64 characters and a
// lookup for base64 chars
private static byte[] base64Alphabet = new byte[BASELENGTH];
+
private static byte[] lookUpBase64Alphabet = new byte[LOOKUPLENGTH];
// Populating the lookup and character arrays
@@ -191,7 +192,6 @@
return encodeBase64(binaryData, true);
}
-
/**
* Decodes an Object using the base64 algorithm. This method
* is provided in order to satisfy the requirements of the
@@ -206,7 +206,8 @@
*/
public Object decode(Object pObject) {
if (!(pObject instanceof byte[])) {
- throw new InvalidParameterException("Parameter supplied to Base64 decode is not a byte[]");
+ throw new InvalidParameterException(
+ "Parameter supplied to Base64 decode is not a byte[]");
}
return decode((byte[]) pObject);
}
@@ -252,8 +253,8 @@
// allow for extra length to account for the separator(s)
if (isChunked) {
- nbrChunks =
- (CHUNK_SEPARATOR.length == 0 ? 0 : (int) Math.ceil((float) encodedDataLength / CHUNK_SIZE));
+ nbrChunks = (CHUNK_SEPARATOR.length == 0 ? 0 : (int) Math
+ .ceil((float) encodedDataLength / CHUNK_SIZE));
encodedDataLength += nbrChunks * CHUNK_SEPARATOR.length;
}
@@ -279,21 +280,21 @@
l = (byte) (b2 & 0x0f);
k = (byte) (b1 & 0x03);
- byte val1 =
- ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0);
- byte val2 =
- ((b2 & SIGN) == 0) ? (byte) (b2 >> 4) : (byte) ((b2) >> 4 ^ 0xf0);
- byte val3 =
- ((b3 & SIGN) == 0) ? (byte) (b3 >> 6) : (byte) ((b3) >> 6 ^ 0xfc);
+ byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2)
+ : (byte) ((b1) >> 2 ^ 0xc0);
+ byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4)
+ : (byte) ((b2) >> 4 ^ 0xf0);
+ byte val3 = ((b3 & SIGN) == 0) ? (byte) (b3 >> 6)
+ : (byte) ((b3) >> 6 ^ 0xfc);
encodedData[encodedIndex] = lookUpBase64Alphabet[val1];
//log.debug( "val2 = " + val2 );
//log.debug( "k4 = " + (k<<4) );
//log.debug( "vak = " + (val2 | (k<<4)) );
- encodedData[encodedIndex + 1] =
- lookUpBase64Alphabet[val2 | (k << 4)];
- encodedData[encodedIndex + 2] =
- lookUpBase64Alphabet[(l << 2) | val3];
+ encodedData[encodedIndex + 1] = lookUpBase64Alphabet[val2
+ | (k << 4)];
+ encodedData[encodedIndex + 2] = lookUpBase64Alphabet[(l << 2)
+ | val3];
encodedData[encodedIndex + 3] = lookUpBase64Alphabet[b3 & 0x3f];
encodedIndex += 4;
@@ -302,16 +303,11 @@
if (isChunked) {
// this assumes that CHUNK_SIZE % 4 == 0
if (encodedIndex == nextSeparatorIndex) {
- System.arraycopy(
- CHUNK_SEPARATOR,
- 0,
- encodedData,
- encodedIndex,
- CHUNK_SEPARATOR.length);
+ System.arraycopy(CHUNK_SEPARATOR, 0, encodedData,
+ encodedIndex, CHUNK_SEPARATOR.length);
chunksSoFar++;
- nextSeparatorIndex =
- (CHUNK_SIZE * (chunksSoFar + 1)) +
- (chunksSoFar * CHUNK_SEPARATOR.length);
+ nextSeparatorIndex = (CHUNK_SIZE * (chunksSoFar + 1))
+ + (chunksSoFar * CHUNK_SEPARATOR.length);
encodedIndex += CHUNK_SEPARATOR.length;
}
}
@@ -325,8 +321,8 @@
k = (byte) (b1 & 0x03);
//log.debug("b1=" + b1);
//log.debug("b1<<2 = " + (b1>>2) );
- byte val1 =
- ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0);
+ byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2)
+ : (byte) ((b1) >> 2 ^ 0xc0);
encodedData[encodedIndex] = lookUpBase64Alphabet[val1];
encodedData[encodedIndex + 1] = lookUpBase64Alphabet[k << 4];
encodedData[encodedIndex + 2] = PAD;
@@ -338,14 +334,14 @@
l = (byte) (b2 & 0x0f);
k = (byte) (b1 & 0x03);
- byte val1 =
- ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0);
- byte val2 =
- ((b2 & SIGN) == 0) ? (byte) (b2 >> 4) : (byte) ((b2) >> 4 ^ 0xf0);
+ byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2)
+ : (byte) ((b1) >> 2 ^ 0xc0);
+ byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4)
+ : (byte) ((b2) >> 4 ^ 0xf0);
encodedData[encodedIndex] = lookUpBase64Alphabet[val1];
- encodedData[encodedIndex + 1] =
- lookUpBase64Alphabet[val2 | (k << 4)];
+ encodedData[encodedIndex + 1] = lookUpBase64Alphabet[val2
+ | (k << 4)];
encodedData[encodedIndex + 2] = lookUpBase64Alphabet[l << 2];
encodedData[encodedIndex + 3] = PAD;
}
@@ -353,12 +349,9 @@
if (isChunked) {
// we also add a separator to the end of the final chunk.
if (chunksSoFar < nbrChunks) {
- System.arraycopy(
- CHUNK_SEPARATOR,
- 0,
- encodedData,
- encodedDataLength - CHUNK_SEPARATOR.length,
- CHUNK_SEPARATOR.length);
+ System.arraycopy(CHUNK_SEPARATOR, 0, encodedData,
+ encodedDataLength - CHUNK_SEPARATOR.length,
+ CHUNK_SEPARATOR.length);
}
}
@@ -399,23 +392,22 @@
}
decodedData = new byte[lastData - numberQuadruple];
}
-
+
for (int i = 0; i < numberQuadruple; i++) {
dataIndex = i * 4;
marker0 = base64Data[dataIndex + 2];
marker1 = base64Data[dataIndex + 3];
-
+
b1 = base64Alphabet[base64Data[dataIndex]];
b2 = base64Alphabet[base64Data[dataIndex + 1]];
-
+
if (marker0 != PAD && marker1 != PAD) {
//No PAD e.g 3cQl
b3 = base64Alphabet[marker0];
b4 = base64Alphabet[marker1];
-
+
decodedData[encodedIndex] = (byte) (b1 << 2 | b2 >> 4);
- decodedData[encodedIndex + 1] =
- (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
+ decodedData[encodedIndex + 1] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
decodedData[encodedIndex + 2] = (byte) (b3 << 6 | b4);
} else if (marker0 == PAD) {
//Two PAD e.g. 3c[Pad][Pad]
@@ -423,16 +415,15 @@
} else if (marker1 == PAD) {
//One PAD e.g. 3cQ[Pad]
b3 = base64Alphabet[marker0];
-
+
decodedData[encodedIndex] = (byte) (b1 << 2 | b2 >> 4);
- decodedData[encodedIndex + 1] =
- (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
+ decodedData[encodedIndex + 1] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
}
encodedIndex += 3;
}
return decodedData;
}
-
+
/**
* Discards any whitespace from a base-64 encoded block.
*
@@ -443,16 +434,16 @@
static byte[] discardWhitespace(byte[] data) {
byte groomedData[] = new byte[data.length];
int bytesCopied = 0;
-
+
for (int i = 0; i < data.length; i++) {
switch (data[i]) {
- case (byte) ' ' :
- case (byte) '\n' :
- case (byte) '\r' :
- case (byte) '\t' :
- break;
+ case (byte) ' ':
+ case (byte) '\n':
+ case (byte) '\r':
+ case (byte) '\t':
+ break;
default:
- groomedData[bytesCopied++] = data[i];
+ groomedData[bytesCopied++] = data[i];
}
}
@@ -489,7 +480,6 @@
return packedData;
}
-
// Implementation of the Encoder Interface
/**
@@ -507,7 +497,7 @@
public Object encode(Object pObject) {
if (!(pObject instanceof byte[])) {
throw new InvalidParameterException(
- "Parameter supplied to Base64 encode is not a byte[]");
+ "Parameter supplied to Base64 encode is not a byte[]");
}
return encode((byte[]) pObject);
}