commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject cvs commit: jakarta-commons/codec/src/java/org/apache/commons/codec/binary Binary.java
Date Sun, 21 Mar 2004 01:25:59 GMT
ggregory    2004/03/20 17:25:59

  Modified:    codec/src/java/org/apache/commons/codec/binary Binary.java
  Log:
  Format source to match classes in this package. In particular, "foo ( bar)" replaced with
"foo(bar)". Etc.
  
  Revision  Changes    Path
  1.8       +147 -202  jakarta-commons/codec/src/java/org/apache/commons/codec/binary/Binary.java
  
  Index: Binary.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/codec/src/java/org/apache/commons/codec/binary/Binary.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- Binary.java	21 Mar 2004 01:18:56 -0000	1.7
  +++ Binary.java	21 Mar 2004 01:25:59 -0000	1.8
  @@ -13,7 +13,8 @@
    * See the License for the specific language governing permissions and
    * limitations under the License.
    */ 
  -package org.apache.commons.codec.binary ;
  +
  +package org.apache.commons.codec.binary;
   
   import org.apache.commons.codec.BinaryDecoder;
   import org.apache.commons.codec.BinaryEncoder;
  @@ -22,319 +23,263 @@
   
   /**
    * Encodes and decodes byte arrays to and from ASCII bit Strings.
  - *
  - * @todo may want to add more bit vector functions like and/or/xor/nand
  - * @todo also might be good to generate boolean[] from byte[] et. cetera.
  + * 
  + * @todo may want to add more bit vector functions like and/or/xor/nand 
  + * @todo also might be good to generate boolean[]
  + * from byte[] et. cetera.
  + * 
    * @author Apache Software Foundation
    * @since 1.3
    * @version $Id $
    */
  -public class Binary implements BinaryDecoder, BinaryEncoder
  -{
  +public class Binary implements BinaryDecoder, BinaryEncoder {
       /*
  -     * tried to avoid using ArrayUtils to minimize dependencies
  -     * while using these empty arrays - dep is just not worth it. 
  +     * 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] ;
  +    private static final char[] EMPTY_CHAR_ARRAY = new char[0];
  +
       /** Empty byte array. */
  -    private static final byte[] EMPTY_BYTE_ARRAY = new byte[0] ;
  +    private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
   
       /** Mask for bit 0 of a byte. */
  -    private static final int BIT_0 = 1 ;
  +    private static final int BIT_0 = 1;
  +
       /** Mask for bit 1 of a byte. */
  -    private static final int BIT_1 = 0x02 ;
  +    private static final int BIT_1 = 0x02;
  +
       /** Mask for bit 2 of a byte. */
  -    private static final int BIT_2 = 0x04 ;
  +    private static final int BIT_2 = 0x04;
  +
       /** Mask for bit 3 of a byte. */
  -    private static final int BIT_3 = 0x08 ;
  +    private static final int BIT_3 = 0x08;
  +
       /** Mask for bit 4 of a byte. */
  -    private static final int BIT_4 = 0x10 ;
  +    private static final int BIT_4 = 0x10;
  +
       /** Mask for bit 5 of a byte. */
  -    private static final int BIT_5 = 0x20 ;
  +    private static final int BIT_5 = 0x20;
  +
       /** Mask for bit 6 of a byte. */
  -    private static final int BIT_6 = 0x40 ;
  +    private static final int BIT_6 = 0x40;
  +
       /** Mask for bit 7 of a byte. */
  -    private static final int BIT_7 = 0x80 ;
  -    
  -    private static final int [] BITS = 
  -    {
  -      BIT_0, BIT_1, BIT_2, BIT_3, BIT_4, BIT_5, BIT_6, BIT_7
  -    } ;
  -    
  -    
  +    private static final int BIT_7 = 0x80;
  +
  +    private static final int[] BITS = {BIT_0, BIT_1, BIT_2, BIT_3, BIT_4, BIT_5, BIT_6,
BIT_7};
  +
       /**
  -     * Converts an array of raw binary data into an array of ascii 0 and 1
  -     * characters.
  +     * Converts an array of raw binary data into an array of ascii 0 and 1 characters.
        * 
  -     * @param raw the raw binary data to convert
  +     * @param raw
  +     *                  the raw binary data to convert
        * @return 0 and 1 ascii character bytes one for each bit of the argument
        * @see org.apache.commons.codec.BinaryEncoder#encode(byte[])
        */
  -    public byte[] encode( byte[] raw )
  -    {
  -        return toAsciiBytes( raw ) ;
  +    public byte[] encode(byte[] raw) {
  +        return toAsciiBytes(raw);
       }
  -    
   
       /**
  -     * Converts an array of raw binary data into an array of ascii 0 and 1
  -     * chars.
  +     * Converts an array of raw binary data into an array of ascii 0 and 1 chars.
        * 
  -     * @param raw the raw binary data to convert
  +     * @param raw
  +     *                  the raw binary data to convert
        * @return 0 and 1 ascii character chars one for each bit of the argument
  -     * @throws EncoderException if the argument is not a byte[]
  +     * @throws EncoderException
  +     *                  if the argument is not a byte[]
        * @see org.apache.commons.codec.Encoder#encode(java.lang.Object)
        */
  -    public Object encode( Object raw ) throws EncoderException
  -    {
  -        if ( ! ( raw instanceof byte [] ) )
  -        {
  -            throw new EncoderException( "argument not a byte array" ) ;
  +    public Object encode(Object raw) throws EncoderException {
  +        if (!(raw instanceof byte[])) {
  +            throw new EncoderException("argument not a byte array");
           }
  -        
  -        return toAsciiChars( ( byte [] ) raw ) ;
  +        return toAsciiChars((byte[]) raw);
       }
  -    
   
       /**
        * Decodes a byte array where each byte represents an ascii '0' or '1'.
        * 
  -     * @param ascii each byte represents an ascii '0' or '1'
  -     * @return the raw encoded binary where each bit corresponds to a byte in
  -     *      the byte array argument
  -     * @throws DecoderException if argument is not a byte[], char[] or String
  +     * @param ascii
  +     *                  each byte represents an ascii '0' or '1'
  +     * @return the raw encoded binary where each bit corresponds to a byte in the byte
array argument
  +     * @throws DecoderException
  +     *                  if argument is not a byte[], char[] or String
        * @see org.apache.commons.codec.Decoder#decode(java.lang.Object)
        */
  -    public Object decode( Object ascii ) throws DecoderException
  -    {
  -        if ( ascii == null )
  -        {
  -            return EMPTY_BYTE_ARRAY ;
  -        }
  -    
  -        if ( ascii instanceof byte[] )
  -        {
  -            return fromAscii( ( byte[] ) ascii ) ;
  -        }
  -        
  -        if ( ascii instanceof char[] )
  -        {
  -            return fromAscii( ( char[] ) ascii ) ;
  -        }
  -        
  -        if ( ascii instanceof String )
  -        {
  -            return fromAscii( ( ( String ) ascii ).toCharArray() ) ;
  +    public Object decode(Object ascii) throws DecoderException {
  +        if (ascii == null) {
  +            return EMPTY_BYTE_ARRAY;
           }
  -
  -        throw new DecoderException( "argument not a byte array" ) ;
  +        if (ascii instanceof byte[]) {
  +            return fromAscii((byte[]) ascii);
  +        }
  +        if (ascii instanceof char[]) {
  +            return fromAscii((char[]) ascii);
  +        }
  +        if (ascii instanceof String) {
  +            return fromAscii(((String) ascii).toCharArray());
  +        }
  +        throw new DecoderException("argument not a byte array");
       }
   
  -    
       /**
        * Decodes a byte array where each byte represents an ascii '0' or '1'.
        * 
  -     * @param ascii each byte represents an ascii '0' or '1'
  -     * @return the raw encoded binary where each bit corresponds to a byte in
  -     *      the byte array argument 
  +     * @param ascii
  +     *                  each byte represents an ascii '0' or '1'
  +     * @return the raw encoded binary where each bit corresponds to a byte in the byte
array argument
        * @see org.apache.commons.codec.Decoder#decode(Object)
        */
  -    public byte[] decode( byte[] ascii ) 
  -    {
  -        return fromAscii( ascii ) ;
  +    public byte[] decode(byte[] ascii) {
  +        return fromAscii(ascii);
       }
  -    
  -    
  +
       /**
  -     * Decodes a String where each char of the String represents an ascii '0' 
  -     * or '1'.
  +     * Decodes a String where each char of the String represents an ascii '0' or '1'.
        * 
  -     * @param ascii String of '0' and '1' characters
  -     * @return the raw encoded binary where each bit corresponds to a byte in
  -     *      the byte array argument 
  +     * @param ascii
  +     *                  String of '0' and '1' characters
  +     * @return the raw encoded binary where each bit corresponds to a byte in the byte
array argument
        * @see org.apache.commons.codec.Decoder#decode(Object)
        */
  -    public byte[] toByteArray( String ascii )
  -    {
  +    public byte[] toByteArray(String ascii) {
           if (ascii == null) {
               return EMPTY_BYTE_ARRAY;
           }
  -        return fromAscii( ascii.toCharArray() ) ;
  +        return fromAscii(ascii.toCharArray());
       }
  -    
  -    
  +
       // ------------------------------------------------------------------------
       //
       // static codec operations
       //
       // ------------------------------------------------------------------------
  -    
       /**
        * Decodes a byte array where each char represents an ascii '0' or '1'.
        * 
  -     * @param ascii each char represents an ascii '0' or '1'
  -     * @return the raw encoded binary where each bit corresponds to a char in
  -     *      the char array argument 
  -     */
  -    public static byte[] fromAscii( char[] ascii )
  -    {
  -        if ( ascii == null || ascii.length == 0 )
  -        {
  -            return EMPTY_BYTE_ARRAY ;
  +     * @param ascii
  +     *                  each char represents an ascii '0' or '1'
  +     * @return the raw encoded binary where each bit corresponds to a char in the char
array argument
  +     */
  +    public static byte[] fromAscii(char[] ascii) {
  +        if (ascii == null || ascii.length == 0) {
  +            return EMPTY_BYTE_ARRAY;
           }
  -    
           // get length/8 times bytes with 3 bit shifts to the right of the length
  -        byte[] l_raw = new byte[ ascii.length >> 3 ] ;
  -        
  +        byte[] l_raw = new byte[ascii.length >> 3];
           /*
  -         * We decr index jj by 8 as we go along to not recompute indices using 
  -         * multiplication every time inside the loop.
  +         * We decr index jj by 8 as we go along to not recompute indices using multiplication
every time inside the
  +         * loop.
            */
  -        for ( int ii=0, jj=ascii.length-1; ii < l_raw.length; ii++, jj-=8 )
  -        {
  -            for ( int bits=0; bits < BITS.length; ++bits) 
  -            {
  -                if ( ascii[jj-bits] == '1' )
  -                {
  -                    l_raw[ii] |= BITS[bits] ;
  +        for (int ii = 0, jj = ascii.length - 1; ii < l_raw.length; ii++, jj -= 8) {
  +            for (int bits = 0; bits < BITS.length; ++bits) {
  +                if (ascii[jj - bits] == '1') {
  +                    l_raw[ii] |= BITS[bits];
                   }
  -            }        
  -            
  +            }
           }
  -        
  -        return l_raw ;
  +        return l_raw;
       }
   
  -    
       /**
        * Decodes a byte array where each byte represents an ascii '0' or '1'.
        * 
  -     * @param ascii each byte represents an ascii '0' or '1'
  -     * @return the raw encoded binary where each bit corresponds to a byte in
  -     *      the byte array argument 
  -     */
  -    public static byte[] fromAscii( byte[] ascii )
  -    {
  -        if ( ascii == null || ascii.length == 0 )
  -        {
  -            return EMPTY_BYTE_ARRAY ;
  +     * @param ascii
  +     *                  each byte represents an ascii '0' or '1'
  +     * @return the raw encoded binary where each bit corresponds to a byte in the byte
array argument
  +     */
  +    public static byte[] fromAscii(byte[] ascii) {
  +        if (ascii == null || ascii.length == 0) {
  +            return EMPTY_BYTE_ARRAY;
           }
  -    
           // get length/8 times bytes with 3 bit shifts to the right of the length
  -        byte[] l_raw = new byte[ ascii.length >> 3 ] ;
  -        
  +        byte[] l_raw = new byte[ascii.length >> 3];
           /*
  -         * We decr index jj by 8 as we go along to not recompute indices using 
  -         * multiplication every time inside the loop.
  +         * We decr index jj by 8 as we go along to not recompute indices using multiplication
every time inside the
  +         * loop.
            */
  -        for ( int ii=0, jj=ascii.length-1; ii < l_raw.length; ii++, jj-=8 )
  -        {
  -            for ( int bits=0; bits < BITS.length; ++bits) 
  -            {
  -                if ( ascii[jj-bits] == '1' )
  -                {
  -                    l_raw[ii] |= BITS[bits] ;
  +        for (int ii = 0, jj = ascii.length - 1; ii < l_raw.length; ii++, jj -= 8) {
  +            for (int bits = 0; bits < BITS.length; ++bits) {
  +                if (ascii[jj - bits] == '1') {
  +                    l_raw[ii] |= BITS[bits];
                   }
  -            }        
  +            }
           }
  -        
  -        return l_raw ;
  +        return l_raw;
       }
   
  -    
       /**
  -     * Converts an array of raw binary data into an array of ascii 0 and 1
  -     * character bytes - each byte is a truncated char.
  +     * Converts an array of raw binary data into an array of ascii 0 and 1 character bytes
- each byte is a truncated
  +     * char.
        * 
  -     * @param raw the raw binary data to convert
  +     * @param raw
  +     *                  the raw binary data to convert
        * @return an array of 0 and 1 character bytes for each bit of the argument
        * @see org.apache.commons.codec.BinaryEncoder#encode(byte[])
        */
  -    public static byte[] toAsciiBytes( byte[] raw )
  -    {
  -        if ( raw == null || raw.length == 0 )
  -        {
  -            return EMPTY_BYTE_ARRAY ;
  +    public static byte[] toAsciiBytes(byte[] raw) {
  +        if (raw == null || raw.length == 0) {
  +            return EMPTY_BYTE_ARRAY;
           }
  -    
           // get 8 times the bytes with 3 bit shifts to the left of the length
  -        byte [] l_ascii = new byte[ raw.length << 3 ] ;
  -        
  +        byte[] l_ascii = new byte[raw.length << 3];
           /*
  -         * We decr index jj by 8 as we go along to not recompute indices using 
  -         * multiplication every time inside the loop.
  +         * We decr index jj by 8 as we go along to not recompute indices using multiplication
every time inside the
  +         * loop.
            */
  -        for ( int ii=0, jj=l_ascii.length-1; ii < raw.length; ii++, jj-=8 )
  -        {
  -            for ( int bits=0; bits < BITS.length; ++bits) 
  -            {
  -                if ( ( raw[ii] & BITS[bits] ) == 0 )
  -                {
  -                    l_ascii[jj-bits] = '0' ;
  +        for (int ii = 0, jj = l_ascii.length - 1; ii < raw.length; ii++, jj -= 8) {
  +            for (int bits = 0; bits < BITS.length; ++bits) {
  +                if ((raw[ii] & BITS[bits]) == 0) {
  +                    l_ascii[jj - bits] = '0';
  +                } else {
  +                    l_ascii[jj - bits] = '1';
                   }
  -                else
  -                {
  -                    l_ascii[jj-bits] = '1' ;
  -                }
  -            }        
  +            }
           }
  -        
  -        return l_ascii ;
  +        return l_ascii;
       }
   
  -
       /**
  -     * Converts an array of raw binary data into an array of ascii 0 and 1
  -     * characters.
  +     * Converts an array of raw binary data into an array of ascii 0 and 1 characters.
        * 
  -     * @param raw the raw binary data to convert
  +     * @param raw
  +     *                  the raw binary data to convert
        * @return an array of 0 and 1 characters for each bit of the argument
        * @see org.apache.commons.codec.BinaryEncoder#encode(byte[])
        */
  -    public static char[] toAsciiChars( byte[] raw )
  -    {
  -        if ( raw == null || raw.length == 0 )
  -        {
  -            return EMPTY_CHAR_ARRAY ;
  +    public static char[] toAsciiChars(byte[] raw) {
  +        if (raw == null || raw.length == 0) {
  +            return EMPTY_CHAR_ARRAY;
           }
  -        
           // get 8 times the bytes with 3 bit shifts to the left of the length
  -        char [] l_ascii = new char[ raw.length << 3 ] ;
  -        
  +        char[] l_ascii = new char[raw.length << 3];
           /*
  -         * We decr index jj by 8 as we go along to not recompute indices using 
  -         * multiplication every time inside the loop.
  +         * We decr index jj by 8 as we go along to not recompute indices using multiplication
every time inside the
  +         * loop.
            */
  -        for ( int ii=0, jj=l_ascii.length-1; ii < raw.length; ii++, jj-=8 )
  -        {
  -            for ( int bits=0; bits < BITS.length; ++bits )
  -            {
  -                if ( ( raw[ii] & BITS[bits] ) == 0 )
  -                {
  -                    l_ascii[jj-bits] = '0' ;
  -                }
  -                else
  -                {
  -                    l_ascii[jj-bits] = '1' ;
  +        for (int ii = 0, jj = l_ascii.length - 1; ii < raw.length; ii++, jj -= 8) {
  +            for (int bits = 0; bits < BITS.length; ++bits) {
  +                if ((raw[ii] & BITS[bits]) == 0) {
  +                    l_ascii[jj - bits] = '0';
  +                } else {
  +                    l_ascii[jj - bits] = '1';
                   }
               }
           }
  -        
  -        return l_ascii ;
  +        return l_ascii;
       }
   
  -
       /**
  -     * Converts an array of raw binary data into a String of ascii 0 and 1
  -     * characters.
  +     * Converts an array of raw binary data into a String of ascii 0 and 1 characters.
        * 
  -     * @param raw the raw binary data to convert
  +     * @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( byte[] raw )
  -    {
  -        return new String( toAsciiChars( raw ) ) ;
  +    public static String toAsciiString(byte[] raw) {
  +        return new String(toAsciiChars(raw));
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message