Wooops forgot the patch! So here it is.
> -----Original Message-----
> From: Alex Karasulu [mailto:[EMAIL PROTECTED]
> Sent: Thursday, March 11, 2004 12:36 PM
> To: 'Jakarta Commons Developers List'
> Subject: [codec][PATCH] Added overload to Binary.toAsciiString()
>
> Hi,
>
> Could someone add this additional overload to the toAsciiString()
> static method of the Binary. null and empty string handling was
> also added to make things more forgiving.
>
> The patch has an additional test case for the overload as well.
>
> Alex
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
? patch.txt
? src/java/org/apache/commons/codec/stateful
Index: src/java/org/apache/commons/codec/binary/Binary.java
===================================================================
RCS file:
/home/cvspublic/jakarta-commons/codec/src/java/org/apache/commons/codec/binary/Binary.java,v
retrieving revision 1.4
diff -r1.4 Binary.java
18,21c18,21
< import org.apache.commons.codec.BinaryDecoder;
< import org.apache.commons.codec.BinaryEncoder;
< import org.apache.commons.codec.DecoderException;
< import org.apache.commons.codec.EncoderException;
---
> import org.apache.commons.codec.BinaryDecoder ;
> import org.apache.commons.codec.BinaryEncoder ;
> import org.apache.commons.codec.DecoderException ;
> import org.apache.commons.codec.EncoderException ;
32a33,41
> /*
> * tried to avoid using ArrayUtils to minimize dependencies
> * while using these empty arrays - dep is just not worth it.
> */
> /** empty char array */
> private static final char[] EMPTY_CHAR_ARRAY = new char[0] ;
> /** empty byte array */
> private static final byte[] EMPTY_BYTE_ARRAY = new byte[0] ;
>
65a75,79
> if ( raw == null || raw.length == 0 )
> {
> return EMPTY_BYTE_ARRAY ;
> }
>
86c100,107
< return toAsciiChars( ( byte [] ) raw ) ;
---
> byte[] rawBytes = ( byte[] ) raw ;
>
> if ( rawBytes == null || rawBytes.length == 0 )
> {
> return EMPTY_CHAR_ARRAY ;
> }
>
> return toAsciiChars( rawBytes ) ;
100a122,126
> if ( ascii == null )
> {
> return EMPTY_BYTE_ARRAY ;
> }
>
129a156,160
> if ( ascii == null || ascii.length == 0 )
> {
> return EMPTY_BYTE_ARRAY ;
> }
>
144a176,180
> if ( ascii == null || ascii.length() == 0 )
> {
> return EMPTY_BYTE_ARRAY ;
> }
>
157a194,198
> if ( ascii == null || ascii.length == 0 )
> {
> return EMPTY_BYTE_ARRAY ;
> }
>
226a268,272
> if ( ascii == null || ascii.length == 0 )
> {
> return EMPTY_BYTE_ARRAY ;
> }
>
296a343,347
> if ( raw == null || raw.length == 0 )
> {
> return EMPTY_BYTE_ARRAY ;
> }
>
398a450,454
> if ( raw == null || raw.length == 0 )
> {
> return EMPTY_CHAR_ARRAY ;
> }
>
489a546,564
>
> /**
> * Converts an integer interpreting it as 4 raw bytes and converting it
> * into a String of ascii 0 and 1 characters.
> *
> * @param raw the raw binary data to convert
> * @return a String of 0 and 1 characters representing the binary data
> * @see org.apache.commons.codec.BinaryEncoder#encode(byte[])
> */
> public static String toAsciiString( int raw )
> {
> byte[] intBytes = new byte[4] ;
> intBytes[0] = (byte) ( (int) 0x000000ff & raw ) ;
> intBytes[1] = (byte) ( (int) ( 0x0000ff00 & raw ) >> 8 ) ;
> intBytes[2] = (byte) ( (int) ( 0x00ff0000 & raw ) >> 16 ) ;
> intBytes[3] = (byte) ( (int) ( 0xff000000 & raw ) >> 24 ) ;
> return new String( toAsciiChars( intBytes ) ) ;
> }
>
Index: src/test/org/apache/commons/codec/binary/BinaryTest.java
===================================================================
RCS file:
/home/cvspublic/jakarta-commons/codec/src/test/org/apache/commons/codec/binary/BinaryTest.java,v
retrieving revision 1.3
diff -r1.3 BinaryTest.java
1748a1749,1787
>
>
> public void testToAsciiStringInt()
> {
> assertEquals( "00000000" +
> "00000000" +
> "00000000" +
> "00000000", Binary.toAsciiString( 0 ) ) ;
>
> assertEquals( "00000000" +
> "00000000" +
> "00000000" +
> "00000001", Binary.toAsciiString( 1 ) ) ;
>
> assertEquals( "00000000" +
> "00000000" +
> "00000000" +
> "11111111", Binary.toAsciiString( 255 ) ) ;
>
> assertEquals( "00000000" +
> "00000000" +
> "00000001" +
> "00000000", Binary.toAsciiString( 256 ) ) ;
>
> assertEquals( "00000000" +
> "00000000" +
> "11111111" +
> "00000000", Binary.toAsciiString( 0xff00 ) ) ;
>
> assertEquals( "00000000" +
> "11111111" +
> "00000000" +
> "00000000", Binary.toAsciiString( 0xff0000 ) ) ;
>
> assertEquals( "11111111" +
> "00000000" +
> "11111111" +
> "00000000", Binary.toAsciiString( 0xff00ff00 ) ) ;
> }
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]