directory-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Enrique Rodriguez <erodrig...@apache.org>
Subject ByteBuffer convenience methods
Date Thu, 13 Jan 2005 02:18:02 GMT
Trustin,

I'd like to suggest adding some convenience methods to the ByteBuffer 
impl in MINA to handle unsigned types in Java.  The standard method for 
handling unsigned type in Java (since there isn't language support) is 
to use a type one size larger than the unsigned type.  This technique 
appears across the net and is also in the O'Reilly NIO book.

I have been using these as static methods in a utility class, but they 
would be nice to have in MINA's ByteBuffer.  They are used pretty 
extensively in binary protocols.  I have attached non-static versions, 
below, for your review.

-enrique


     public short getUnsignedByte()
     {
         return ( (short)( buf.get() & 0xff ) );
     }

     public void putUnsignedByte( int value )
     {
         buf.put( (byte)( value & 0xff ) );
     }

     public short getUnsignedByte( int position )
     {
         return ( (short)( buf.get (position) & (short)0xff ) );
     }

     public void putUnsignedByte( int position, int value )
     {
         buf.put( position, (byte)( value & 0xff ) );
     }

     public int getUnsignedShort()
     {
         return ( buf.getShort() & 0xffff );
     }

     public void putUnsignedShort( int value )
     {
         buf.putShort( (short)( value & 0xffff ) );
     }

     public int getUnsignedShort( int position )
     {
         return ( buf.getShort( position ) & 0xffff );
     }

     public void putUnsignedShort( int position, int value )
     {
         buf.putShort( position, (short)( value & 0xffff ) );
     }

     public long getUnsignedInt()
     {
         return ( buf.getInt() & 0xffffffffL );
     }

     public void putUnsignedInt( long value )
     {
         buf.putInt( (int)( value & 0xffffffffL ) );
     }

     public long getUnsignedInt( int position )
     {
         return ( buf.getInt( position ) & 0xffffffffL );
     }

     public void putUnsignedInt( int position, long value )
     {
         buf.putInt( position, (int)( value & 0xffffffffL ) );
     }


Mime
View raw message