• Main Page
  • Related Pages
  • Packages
  • Classes
  • Files
  • File List

utils/OctetBuffer.java

Go to the documentation of this file.
00001 
00002 package utils;
00003 
00004 /**
00005  *  Encapsulates binary payload that can be manipulated on the octet (byte) level.
00006  */
00007 public class OctetBuffer
00008 {
00009     /**
00010      *  The octet storage
00011      */
00012     private byte[] store = null;
00013     
00014     /**
00015      *  Position index
00016      */
00017     private int position = 0;
00018     
00019     /**
00020      *  Offset into backing store to enable slicing
00021      */
00022     private int sliceOffset = 0;
00023 
00024     /**
00025      *  Allocates buffer
00026      */
00027     public static OctetBuffer allocate( int size ) 
00028     {
00029         OctetBuffer bb = new OctetBuffer ();
00030         bb.store = new byte[ size ];
00031         bb.position = 0;
00032         bb.sliceOffset = 0;
00033         return bb;
00034     }
00035 
00036     /**
00037      *  Wraps existing byte[] array
00038      */
00039     public static OctetBuffer wrap( byte[] bs )
00040     {
00041         OctetBuffer bb = new OctetBuffer ();
00042         bb.store = bs;
00043         bb.position = 0;
00044         bb.sliceOffset = 0;
00045         return bb;
00046     }
00047 
00048     /**
00049      *  Slices buffer 
00050      */
00051     public OctetBuffer slice ()
00052     {
00053         OctetBuffer bb = new OctetBuffer();
00054         bb.store = store;
00055         bb.position = 0;
00056         bb.sliceOffset = position;
00057         return bb;
00058     }
00059 
00060     /**
00061      *  Returns internal byte[] store
00062      */
00063     public byte[] getStore ()
00064     {
00065         if ( sliceOffset != 0 ) {
00066             throw new java.lang.IllegalStateException ();
00067         }
00068 
00069         return store;
00070     }
00071 
00072     /**
00073      *  Returns internal byte[] store
00074      */
00075     public int getStoreSize ()
00076     {
00077         if ( sliceOffset != 0 ) {
00078             throw new java.lang.IllegalStateException ();
00079         }
00080 
00081         return store.length;
00082     }
00083 
00084     /**
00085      *  Gets current position
00086      */
00087     public int getPosition ()
00088     {
00089         return position;
00090     }
00091 
00092     /**
00093      *  Returns remaining space
00094      */
00095     public int getFreeSpace ()
00096     {
00097         return store.length - sliceOffset - position;
00098     }
00099 
00100     /**
00101      *  Returns true if there is free space in the store
00102      */
00103     public boolean hasFreeSpace ()
00104     {
00105         return sliceOffset + position < store.length;
00106     }
00107     //////////////////////////////////////////////////////////////// short ///////////////
00108     
00109     /**
00110      *  Gets primitive 'short' from the slice offset in the store
00111      */
00112     public short getShort ()
00113     {
00114         if (sliceOffset + position + 2 > store.length) {
00115             throw new IndexOutOfBoundsException();
00116         }
00117         short s = (short) ( (store[sliceOffset + position] << 8) +
00118                            (store[sliceOffset + position + 1] & 0xFF));
00119         position += 2;
00120         return s;
00121     }
00122 
00123     /**
00124      *  Gets primitive 'short' from the offset in the store
00125      */
00126     public short getShort( int offset )
00127     {
00128         if (sliceOffset + offset + 2 > store.length) {
00129             throw new IndexOutOfBoundsException();
00130         }
00131         short s = (short) ( (store[sliceOffset + offset] << 8) +
00132                            (store[sliceOffset + offset + 1] & 0xFF));
00133         return s;
00134     }
00135 
00136     /**
00137      *  Puts primitive 'short' at the slice offset in the store
00138      */
00139     public void putShort( short value )
00140     {
00141         if (sliceOffset + position + 2 > store.length) {
00142             throw new IndexOutOfBoundsException();
00143         }
00144         store[sliceOffset + position++] = (byte) (value >> 8);
00145         store[sliceOffset + position++] = (byte) (value & 0xff);
00146     }
00147 
00148     /**
00149      *  Puts primitive 'short' at the slice offset in the store
00150      */
00151     public void putShort( int offset, short value )
00152     {
00153         if (sliceOffset + offset + 2 > store.length) {
00154             throw new IndexOutOfBoundsException();
00155         }
00156         store[sliceOffset + offset++] = (byte) (value >> 8);
00157         store[sliceOffset + offset++] = (byte) (value & 0xff);
00158     }
00159 
00160     //////////////////////////////////////////////////////////////// int /////////////////
00161     
00162     /**
00163      *  Gets primitive 'int' from the slice offset in the store
00164      */
00165     public int getInt ()
00166     {
00167         if (sliceOffset + position + 4 > store.length) {
00168             throw new IndexOutOfBoundsException();
00169         }
00170         int i = (store[sliceOffset + position] << 24)
00171             + ( (store[sliceOffset + position + 1] & 0xFF) << 16)
00172             + ( (store[sliceOffset + position + 2] & 0xFF) << 8)
00173             + (store[sliceOffset + position + 3] & 0xFF);
00174         position += 4;
00175         return i;
00176     }
00177 
00178     /**
00179      *  Puts primitive 'int' at the slice offset in the store
00180      */
00181     public void putInt( int value )
00182     {
00183         if (sliceOffset + position + 4 > store.length) {
00184             throw new IndexOutOfBoundsException();
00185         }
00186         store[sliceOffset + position++] = (byte) (value >> 24);
00187         store[sliceOffset + position++] = (byte) ( (value >> 16) & 0xff);
00188         store[sliceOffset + position++] = (byte) ( (value >> 8) & 0xff);
00189         store[sliceOffset + position++] = (byte) (value & 0xff);
00190     }
00191 
00192     //////////////////////////////////////////////////////////////// byte ////////////////
00193     
00194     /**
00195      *  Gets primitive 'byte' from the slice offset in the store
00196      */
00197     public byte get ()
00198     {
00199         if (sliceOffset + position + 1 > store.length) {
00200             throw new IndexOutOfBoundsException();
00201         }
00202         return store[sliceOffset + position++];
00203     }
00204 
00205     /**
00206      *  Puts primitive 'byte' from the slice offset in the store
00207      */
00208     public void put( byte value )
00209     {
00210         if (sliceOffset + position + 1 > store.length) {
00211             throw new IndexOutOfBoundsException();
00212         }
00213         store[position] = value;
00214         position++;
00215     }
00216 
00217     //////////////////////////////////////////////////////////////// byte[] //////////////
00218     
00219     /**
00220      *  Puts byte[] array at the slice offset in the store
00221      */
00222     public void put( byte[] array ) 
00223     {
00224         if (sliceOffset + position + array.length > store.length) {
00225             throw new IndexOutOfBoundsException();
00226         }
00227         System.arraycopy(array, 0, store, sliceOffset + position, array.length);
00228         position += array.length;
00229     }
00230 
00231     /**
00232      *  Gets byte[] array from the slice offset in the store
00233      */
00234     public void get( byte[] array )
00235     {
00236         int l = getFreeSpace();
00237         if (l > array.length) {
00238             l = array.length;
00239         }
00240         System.arraycopy(store, sliceOffset + position, array, 0, l);
00241         position += l;
00242     }
00243 
00244     //////////////////////////////////////////////////////////////// char ////////////////
00245     
00246     /**
00247      *  Puts primitive 'char' at the slice offset in the store
00248      */
00249     public void putChar( char value )
00250     {
00251         if (sliceOffset + position + 2 > store.length) {
00252             throw new IndexOutOfBoundsException();
00253         }
00254         store[sliceOffset + position++] = (byte) ( ( (short) value) >> 8);
00255         store[sliceOffset + position++] = (byte) value;
00256     }
00257 
00258     /**
00259      *  Puts primitive 'char' at the slice offset in the store
00260      */
00261     public void putChar( int offset, char value )
00262     {
00263         if (sliceOffset + offset + 2 > store.length) {
00264             throw new IndexOutOfBoundsException();
00265         }
00266         store[sliceOffset + offset] = (byte) ( ( (short) value) >> 8);
00267         store[sliceOffset + offset + 1] = (byte) value;
00268     }
00269 
00270     /**
00271      *  Gets primitive 'char' from the slice offset in the store
00272      */
00273     public char getChar ()
00274     {
00275         if (sliceOffset + position + 2 > store.length) {
00276             throw new IndexOutOfBoundsException();
00277         }
00278         short s = (short) ( (store[sliceOffset + position] << 8) +
00279                            (store[sliceOffset + position + 1] & 0xFF));
00280         position += 2;
00281         return (char) s;
00282     }
00283 
00284     /**
00285      *  Gets primitive 'char' from the slice offset in the store
00286      */
00287     public char getChar( int offset )
00288     {
00289         if (sliceOffset + offset + 2 > store.length) {
00290             throw new IndexOutOfBoundsException();
00291         }
00292         short s = (short) ( (store[sliceOffset + offset] << 8) +
00293                            (store[sliceOffset + offset + 1] & 0xFF));
00294         return (char) s;
00295     }
00296     
00297     /**
00298      * Converts a 'byte' to an 'int'.
00299      */
00300     public static int toInt( byte b )
00301     {
00302         return ( b + 0x100 ) & 0xFF;
00303     }
00304 }

Generated on Thu Dec 16 2010 14:44:42 for VoIP Kryptofon by  doxygen 1.7.2