You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xmlrpc-dev@ws.apache.org by rh...@apache.org on 2003/02/01 23:29:37 UTC
cvs commit: xml-rpc/src/test/org/apache/xmlrpc Base64Test.java
rhoegg 2003/02/01 14:29:37
Modified: . Tag: XMLRPC_1_2_BRANCH build.xml
src/java/org/apache/xmlrpc Tag: XMLRPC_1_2_BRANCH
Base64.java
src/test/org/apache/xmlrpc Tag: XMLRPC_1_2_BRANCH
Base64Test.java
Log:
Brought Base64 up to date with HEAD.
Revision Changes Path
No revision
No revision
1.20.2.1 +1 -1 xml-rpc/build.xml
Index: build.xml
===================================================================
RCS file: /home/cvs/xml-rpc/build.xml,v
retrieving revision 1.20
retrieving revision 1.20.2.1
diff -u -r1.20 -r1.20.2.1
--- build.xml 10 Oct 2002 00:33:24 -0000 1.20
+++ build.xml 1 Feb 2003 22:29:37 -0000 1.20.2.1
@@ -3,7 +3,7 @@
<project name="xmlrpc" default="jar" basedir=".">
<!-- Properties which are not allowed to be overridden -->
- <property name="version" value="1.2-a3-dev"/>
+ <property name="version" value="1.2-b1-dev"/>
<!-- Allow any user specific values to override the defaults -->
<property file="${user.home}/build.properties" />
No revision
No revision
1.4.2.1 +32 -8 xml-rpc/src/java/org/apache/xmlrpc/Base64.java
Index: Base64.java
===================================================================
RCS file: /home/cvs/xml-rpc/src/java/org/apache/xmlrpc/Base64.java,v
retrieving revision 1.4
retrieving revision 1.4.2.1
diff -u -r1.4 -r1.4.2.1
--- Base64.java 1 Nov 2002 22:06:10 -0000 1.4
+++ Base64.java 1 Feb 2003 22:29:37 -0000 1.4.2.1
@@ -79,6 +79,9 @@
*/
public final class Base64
{
+ static final int CHUNK_SIZE = 76;
+ static final byte[] CHUNK_SEPARATOR = "\n".getBytes();
+
static private final int BASELENGTH = 255;
static private final int LOOKUPLENGTH = 64;
static private final int TWENTYFOURBITGROUP = 24;
@@ -167,24 +170,34 @@
int fewerThan24bits = lengthDataBits%TWENTYFOURBITGROUP;
int numberTriplets = lengthDataBits/TWENTYFOURBITGROUP;
byte encodedData[] = null;
-
+ int encodedDataLength = 0;
if (fewerThan24bits != 0)
{
//data not divisible by 24 bit
- encodedData = new byte[ (numberTriplets + 1 ) * 4 ];
+ encodedDataLength = (numberTriplets + 1 ) * 4;
}
else
{
// 16 or 8 bit
- encodedData = new byte[ numberTriplets * 4 ];
+ encodedDataLength = numberTriplets * 4;
}
+ // allow extra length for the separator
+ int nbrChunks = (CHUNK_SEPARATOR.length == 0 ? 0 :
+ (int) Math.ceil((float) encodedDataLength / CHUNK_SIZE));
+
+ encodedDataLength += (nbrChunks - 1) * CHUNK_SEPARATOR.length;
+ encodedData = new byte[encodedDataLength];
+
byte k = 0, l = 0, b1 = 0, b2 = 0, b3 = 0;
int encodedIndex = 0;
int dataIndex = 0;
int i = 0;
+ int nextSeparatorIndex = CHUNK_SIZE;
+ int chunksSoFar = 0;
+
//log.debug("number of triplets = " + numberTriplets);
for ( i = 0; i<numberTriplets; i++ )
{
@@ -198,7 +211,6 @@
l = (byte)(b2 & 0x0f);
k = (byte)(b1 & 0x03);
- encodedIndex = i * 4;
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);
@@ -212,11 +224,23 @@
encodedData[encodedIndex+2] =
lookUpBase64Alphabet[ (l <<2 ) | val3 ];
encodedData[encodedIndex+3] = lookUpBase64Alphabet[ b3 & 0x3f ];
+
+ encodedIndex += 4;
+
+ // this assumes that CHUNK_SIZE % 4 == 0
+ if(encodedIndex == nextSeparatorIndex){
+ System.arraycopy(CHUNK_SEPARATOR, 0, encodedData,
+ encodedIndex, CHUNK_SEPARATOR.length);
+ chunksSoFar++;
+ nextSeparatorIndex = (CHUNK_SIZE * (chunksSoFar + 1)) +
+ (chunksSoFar * CHUNK_SEPARATOR.length);
+ encodedIndex += CHUNK_SEPARATOR.length;
+ }
}
// form integral number of 6-bit groups
dataIndex = i*3;
- encodedIndex = i*4;
+
if (fewerThan24bits == EIGHTBIT )
{
b1 = binaryData[dataIndex];
No revision
No revision
1.7.2.1 +7 -4 xml-rpc/src/test/org/apache/xmlrpc/Base64Test.java
Index: Base64Test.java
===================================================================
RCS file: /home/cvs/xml-rpc/src/test/org/apache/xmlrpc/Base64Test.java,v
retrieving revision 1.7
retrieving revision 1.7.2.1
diff -u -r1.7 -r1.7.2.1
--- Base64Test.java 1 Nov 2002 22:06:10 -0000 1.7
+++ Base64Test.java 1 Feb 2003 22:29:37 -0000 1.7.2.1
@@ -131,9 +131,8 @@
assertEquals(TEST_DATA[i], new String(decoded));
}
- // FIXME: The Base64.encode() function doesn't wrap at 76 chars.
- //assertEquals(Base64.encode(UNENCODED.getBytes()),
- // ENCODED.getBytes());
+ assertEquals(Base64.encode(UNENCODED.getBytes()),
+ ENCODED.getBytes());
assertEquals(UNENCODED.getBytes(),
Base64.decode(ENCODED.getBytes()));
}
@@ -149,11 +148,15 @@
*/
private void assertEquals(byte[] a, byte[] b)
{
+ if (a.length != b.length)
+ {
+ fail("Byte arrays have different lengths (" + a.length + " != " + b.length + ")");
+ }
for (int i = 0; i < a.length; i++)
{
if (a[i] != b[i])
{
- fail("Byte arrays not equal (" + a[i] + " != " + b[i] + ")");
+ fail("Byte arrays not equal (" + a[i] + " != " + b[i] + " at position + " + i + ")");
}
}
}
Re: cvs commit: xml-rpc/src/test/org/apache/xmlrpc Base64Test.java
Posted by Ryan Hoegg <rh...@isisnetworks.net>.
Daniel Rall wrote:
>I realize that you just backported from HEAD, but it seems that the
>indentation of this file is seriously screwed up (likely caused by tab
>characters). This makes the diffs much more difficult to read. At the very
>least, HEAD should have the correct indentation.
>
>
You're right, I'll take a look at it this evening.
Re: cvs commit: xml-rpc/src/test/org/apache/xmlrpc Base64Test.java
Posted by Daniel Rall <dl...@collab.net>.
I realize that you just backported from HEAD, but it seems that the
indentation of this file is seriously screwed up (likely caused by tab
characters). This makes the diffs much more difficult to read. At the very
least, HEAD should have the correct indentation.
On 1 Feb 2003 rhoegg@apache.org wrote:
> rhoegg 2003/02/01 14:29:37
>
> Modified: . Tag: XMLRPC_1_2_BRANCH build.xml
> src/java/org/apache/xmlrpc Tag: XMLRPC_1_2_BRANCH
> Base64.java
> src/test/org/apache/xmlrpc Tag: XMLRPC_1_2_BRANCH
> Base64Test.java
> Log:
> Brought Base64 up to date with HEAD.
>
> Revision Changes Path
> No revision
>
>
> No revision
>
>
> 1.20.2.1 +1 -1 xml-rpc/build.xml
>
> Index: build.xml
> ===================================================================
> RCS file: /home/cvs/xml-rpc/build.xml,v
> retrieving revision 1.20
> retrieving revision 1.20.2.1
> diff -u -r1.20 -r1.20.2.1
> --- build.xml 10 Oct 2002 00:33:24 -0000 1.20
> +++ build.xml 1 Feb 2003 22:29:37 -0000 1.20.2.1
> @@ -3,7 +3,7 @@
> <project name="xmlrpc" default="jar" basedir=".">
>
> <!-- Properties which are not allowed to be overridden -->
> - <property name="version" value="1.2-a3-dev"/>
> + <property name="version" value="1.2-b1-dev"/>
>
> <!-- Allow any user specific values to override the defaults -->
> <property file="${user.home}/build.properties" />
>
>
>
> No revision
>
>
> No revision
>
>
> 1.4.2.1 +32 -8 xml-rpc/src/java/org/apache/xmlrpc/Base64.java
>
> Index: Base64.java
> ===================================================================
> RCS file: /home/cvs/xml-rpc/src/java/org/apache/xmlrpc/Base64.java,v
> retrieving revision 1.4
> retrieving revision 1.4.2.1
> diff -u -r1.4 -r1.4.2.1
> --- Base64.java 1 Nov 2002 22:06:10 -0000 1.4
> +++ Base64.java 1 Feb 2003 22:29:37 -0000 1.4.2.1
> @@ -79,6 +79,9 @@
> */
> public final class Base64
> {
> + static final int CHUNK_SIZE = 76;
> + static final byte[] CHUNK_SEPARATOR = "\n".getBytes();
> +
> static private final int BASELENGTH = 255;
> static private final int LOOKUPLENGTH = 64;
> static private final int TWENTYFOURBITGROUP = 24;
> @@ -167,24 +170,34 @@
> int fewerThan24bits = lengthDataBits%TWENTYFOURBITGROUP;
> int numberTriplets = lengthDataBits/TWENTYFOURBITGROUP;
> byte encodedData[] = null;
> -
> + int encodedDataLength = 0;
>
> if (fewerThan24bits != 0)
> {
> //data not divisible by 24 bit
> - encodedData = new byte[ (numberTriplets + 1 ) * 4 ];
> + encodedDataLength = (numberTriplets + 1 ) * 4;
> }
> else
> {
> // 16 or 8 bit
> - encodedData = new byte[ numberTriplets * 4 ];
> + encodedDataLength = numberTriplets * 4;
> }
>
> + // allow extra length for the separator
> + int nbrChunks = (CHUNK_SEPARATOR.length == 0 ? 0 :
> + (int) Math.ceil((float) encodedDataLength / CHUNK_SIZE));
> +
> + encodedDataLength += (nbrChunks - 1) * CHUNK_SEPARATOR.length;
> + encodedData = new byte[encodedDataLength];
> +
> byte k = 0, l = 0, b1 = 0, b2 = 0, b3 = 0;
>
> int encodedIndex = 0;
> int dataIndex = 0;
> int i = 0;
> + int nextSeparatorIndex = CHUNK_SIZE;
> + int chunksSoFar = 0;
> +
> //log.debug("number of triplets = " + numberTriplets);
> for ( i = 0; i<numberTriplets; i++ )
> {
> @@ -198,7 +211,6 @@
> l = (byte)(b2 & 0x0f);
> k = (byte)(b1 & 0x03);
>
> - encodedIndex = i * 4;
> 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);
> @@ -212,11 +224,23 @@
> encodedData[encodedIndex+2] =
> lookUpBase64Alphabet[ (l <<2 ) | val3 ];
> encodedData[encodedIndex+3] = lookUpBase64Alphabet[ b3 & 0x3f ];
> +
> + encodedIndex += 4;
> +
> + // this assumes that CHUNK_SIZE % 4 == 0
> + if(encodedIndex == nextSeparatorIndex){
> + System.arraycopy(CHUNK_SEPARATOR, 0, encodedData,
> + encodedIndex, CHUNK_SEPARATOR.length);
> + chunksSoFar++;
> + nextSeparatorIndex = (CHUNK_SIZE * (chunksSoFar + 1)) +
> + (chunksSoFar * CHUNK_SEPARATOR.length);
> + encodedIndex += CHUNK_SEPARATOR.length;
> + }
> }
>
> // form integral number of 6-bit groups
> dataIndex = i*3;
> - encodedIndex = i*4;
> +
> if (fewerThan24bits == EIGHTBIT )
> {
> b1 = binaryData[dataIndex];
>
>
>
> No revision
>
>
> No revision
>
>
> 1.7.2.1 +7 -4 xml-rpc/src/test/org/apache/xmlrpc/Base64Test.java
>
> Index: Base64Test.java
> ===================================================================
> RCS file: /home/cvs/xml-rpc/src/test/org/apache/xmlrpc/Base64Test.java,v
> retrieving revision 1.7
> retrieving revision 1.7.2.1
> diff -u -r1.7 -r1.7.2.1
> --- Base64Test.java 1 Nov 2002 22:06:10 -0000 1.7
> +++ Base64Test.java 1 Feb 2003 22:29:37 -0000 1.7.2.1
> @@ -131,9 +131,8 @@
> assertEquals(TEST_DATA[i], new String(decoded));
> }
>
> - // FIXME: The Base64.encode() function doesn't wrap at 76 chars.
> - //assertEquals(Base64.encode(UNENCODED.getBytes()),
> - // ENCODED.getBytes());
> + assertEquals(Base64.encode(UNENCODED.getBytes()),
> + ENCODED.getBytes());
> assertEquals(UNENCODED.getBytes(),
> Base64.decode(ENCODED.getBytes()));
> }
> @@ -149,11 +148,15 @@
> */
> private void assertEquals(byte[] a, byte[] b)
> {
> + if (a.length != b.length)
> + {
> + fail("Byte arrays have different lengths (" + a.length + " != " + b.length + ")");
> + }
> for (int i = 0; i < a.length; i++)
> {
> if (a[i] != b[i])
> {
> - fail("Byte arrays not equal (" + a[i] + " != " + b[i] + ")");
> + fail("Byte arrays not equal (" + a[i] + " != " + b[i] + " at position + " + i + ")");
> }
> }
> }
>
>
>
>