Class ByteBuffer


  • public class ByteBuffer
    extends java.lang.Object
    Implements a mutable byte buffer similar to StringBuffer.
    Author:
    Andreas Schwier (info@cardcontact.de)
    • Constructor Summary

      Constructors 
      Constructor Description
      ByteBuffer()
      Create empty ByteBuffer with initial capacity of 16 bytes
      ByteBuffer​(byte[] bytes)
      Create ByteBuffer which contains a exact copy of the byte array passed as parameter
      ByteBuffer​(int length)
      Create empty ByteBuffer with initial capacity as defined by parameter length
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      ByteBuffer append​(byte _byte)
      Append single byte
      ByteBuffer append​(byte[] bytes)
      Append byte array
      ByteBuffer append​(ByteBuffer bb)
      Append ByteBuffer
      ByteBuffer clear​(int offset, int count)
      Clear buffer in specified range and move trailing data behind offset + count
      ByteBuffer copy​(int offset, byte[] source)
      Copy source into buffer at offset
      static int find​(byte[] source, byte[] search, int offset)
      Search byte arrays for matching search string and return zero based offset.
      int find​(byte[] search, int offset)
      Search ByteBuffer for matching search string and return zero based offset.
      static int find​(byte[] source, int length, byte[] search, int offset)
      Search byte arrays for matching search string and return zero based offset.
      byte getByteAt​(int offset)
      Return byte at zero based offset
      byte[] getBytes()
      Return ByteBuffer as byte arrays
      byte[] getBytes​(int offset, int count)
      Return part of ByteBuffer as byte arrays
      ByteBuffer insert​(int offset, byte _byte)
      Insert byte at offset
      ByteBuffer insert​(int offset, byte[] bytes)
      Insert contents of byte array at offset
      ByteBuffer insert​(int offset, byte[] bytes, int length)
      Insert bytes at offset
      ByteBuffer insert​(int offset, ByteBuffer bb)
      Insert contents of ByteBuffer at offset
      int length()
      Return length of ByteBuffer
      java.lang.String toString()
      Return hexadecimal string for content of ByteBuffer
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • ByteBuffer

        public ByteBuffer()
        Create empty ByteBuffer with initial capacity of 16 bytes
      • ByteBuffer

        public ByteBuffer​(int length)
        Create empty ByteBuffer with initial capacity as defined by parameter length
        Parameters:
        length - Initial capacity of ByteBuffer
      • ByteBuffer

        public ByteBuffer​(byte[] bytes)
        Create ByteBuffer which contains a exact copy of the byte array passed as parameter
        Parameters:
        bytes - Byte array to create ByteBuffer from
    • Method Detail

      • append

        public ByteBuffer append​(byte _byte)
        Append single byte
        Parameters:
        _byte -
        Returns:
        this
      • append

        public ByteBuffer append​(byte[] bytes)
        Append byte array
        Parameters:
        bytes -
        Returns:
        this
      • append

        public ByteBuffer append​(ByteBuffer bb)
        Append ByteBuffer
        Parameters:
        bb -
        Returns:
        this
      • insert

        public ByteBuffer insert​(int offset,
                                 byte[] bytes,
                                 int length)
                          throws java.lang.IndexOutOfBoundsException
        Insert bytes at offset
        Parameters:
        offset -
        bytes -
        length -
        Returns:
        Throws:
        java.lang.IndexOutOfBoundsException
      • insert

        public ByteBuffer insert​(int offset,
                                 byte _byte)
                          throws java.lang.IndexOutOfBoundsException
        Insert byte at offset
        Parameters:
        offset - Position at which to insert the bytes
        _byte - Byte to insert
        Returns:
        this
        Throws:
        java.lang.IndexOutOfBoundsException - If offset is not in range
      • insert

        public ByteBuffer insert​(int offset,
                                 byte[] bytes)
                          throws java.lang.IndexOutOfBoundsException
        Insert contents of byte array at offset
        Parameters:
        offset - Position at which to insert the bytes
        bytes - Byte array to insert
        Returns:
        this
        Throws:
        java.lang.IndexOutOfBoundsException - If offset is not in range
      • insert

        public ByteBuffer insert​(int offset,
                                 ByteBuffer bb)
                          throws java.lang.IndexOutOfBoundsException
        Insert contents of ByteBuffer at offset
        Parameters:
        offset - Position at which to insert the ByteBuffer
        bb - ByteBuffer to insert
        Returns:
        this
        Throws:
        java.lang.IndexOutOfBoundsException - If offset is not in range
      • length

        public int length()
        Return length of ByteBuffer
        Returns:
        Length of ByteBuffer
      • getByteAt

        public byte getByteAt​(int offset)
                       throws java.lang.IndexOutOfBoundsException
        Return byte at zero based offset
        Parameters:
        offset -
        Returns:
        byte at offset
        Throws:
        java.lang.IndexOutOfBoundsException
      • clear

        public ByteBuffer clear​(int offset,
                                int count)
                         throws java.lang.IndexOutOfBoundsException
        Clear buffer in specified range and move trailing data behind offset + count
        Parameters:
        offset -
        count -
        Returns:
        this
        Throws:
        java.lang.IndexOutOfBoundsException
      • copy

        public ByteBuffer copy​(int offset,
                               byte[] source)
        Copy source into buffer at offset
        Parameters:
        offset -
        source -
        Returns:
        this
      • find

        public static int find​(byte[] source,
                               int length,
                               byte[] search,
                               int offset)
        Search byte arrays for matching search string and return zero based offset. Start search at given offset. An empty search string is always found.
        Parameters:
        source - Byte array to search in
        length - Range in source to search in
        search - Byte array to search for
        offset - Offset to start at
        Returns:
        Zero based offset of match or -1
      • find

        public static int find​(byte[] source,
                               byte[] search,
                               int offset)
        Search byte arrays for matching search string and return zero based offset. Start search at given offset. An empty search string is always found.
        Parameters:
        source - Byte array to search in
        search - Byte array to search for
        offset - Offset to start at
        Returns:
        Zero based offset of match or -1
      • find

        public int find​(byte[] search,
                        int offset)
        Search ByteBuffer for matching search string and return zero based offset. Start search at given offset. An empty search string is always found.
        Parameters:
        search - Byte array to search for
        offset - Offset to start at
        Returns:
        Zero based offset of match or -1
      • getBytes

        public byte[] getBytes()
        Return ByteBuffer as byte arrays
        Returns:
        Array of bytes
      • getBytes

        public byte[] getBytes​(int offset,
                               int count)
        Return part of ByteBuffer as byte arrays
        Parameters:
        offset - Zero based offset in buffer
        count - Number of bytes to extract
        Returns:
        Array of bytes
      • toString

        public java.lang.String toString()
        Return hexadecimal string for content of ByteBuffer
        Overrides:
        toString in class java.lang.Object
        Returns:
        Hexadecimal string