Class ByteArray

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Serializable, java.lang.AutoCloseable, java.lang.Cloneable, java.nio.channels.Channel, java.nio.channels.ReadableByteChannel, java.nio.channels.WritableByteChannel

    public class ByteArray
    extends java.lang.Object
    implements java.io.Serializable, java.nio.channels.WritableByteChannel, java.nio.channels.ReadableByteChannel, java.lang.Cloneable
    Class used to represent an array of bytes as an Object.
    See Also:
    Serialized Form
    • Field Detail

      • NL

        public static final ByteArray NL
        The new line character as byte array
      • CR

        public static final ByteArray CR
        The cr character as byte array
      • TAB

        public static final ByteArray TAB
        The tab character as byte array
      • SPACE

        public static final ByteArray SPACE
        The space as byte array
      • DOT

        public static final ByteArray DOT
        The dot character as byte array
      • EMPTY

        public static final ByteArray EMPTY
        The dot character as byte array
      • DEFAULT_BLOCK_SIZE

        public static final int DEFAULT_BLOCK_SIZE
        Defines the default byte array size
        See Also:
        Constant Field Values
    • Constructor Detail

      • ByteArray

        public ByteArray()
        Create a ByteArray with the default size
      • ByteArray

        public ByteArray​(java.lang.String data)
        Create a ByteArray from a String
        Parameters:
        data - initial data
      • ByteArray

        public ByteArray​(java.lang.StringBuilder data)
        Create a ByteArray from a String
        Parameters:
        data - initial data
      • ByteArray

        public ByteArray​(java.lang.StringBuffer data)
        Create a ByteArray from a String
        Parameters:
        data - initial data
      • ByteArray

        public ByteArray​(ByteArray data)
        Copy constructor
        Parameters:
        data - initial data
      • ByteArray

        public ByteArray​(byte[] data)
        Create a ByteArray from an array of bytes
        Parameters:
        data - initial data
    • Method Detail

      • createByteArrayWithSize

        public static ByteArray createByteArrayWithSize​(int size)
        Creates a byte array with the given size
        Parameters:
        size - the size
        Returns:
        the byte array with the given size
      • createByteArray

        public static ByteArray createByteArray​(byte b,
                                                int size)
        Creates a byte array with a simple byte
        Parameters:
        b - the byte
        size - the size of the byte array
        Returns:
        the created byte array
      • createByteArray

        public static ByteArray createByteArray​(java.nio.ByteBuffer buffer)
        Creates a byte array
        Parameters:
        buffer - the byte buffer
        Returns:
        the created byte array
      • append

        public ByteArray append​(byte ch)
        Append a byte
        Parameters:
        ch - the data
        Returns:
        the byte array (no copy)
      • append

        public ByteArray append​(ByteArray data)
        Append a ByteArray
        Parameters:
        data - the data
        Returns:
        the byte array (no copy)
      • append

        public ByteArray append​(byte[] data)
        Append an array of bytes
        Parameters:
        data - the data
        Returns:
        the byte array (no copy)
      • append

        public ByteArray append​(byte[] data,
                                int off,
                                int len)
        Append an array of bytes
        Parameters:
        data - the data
        off - the offset
        len - the length
        Returns:
        the byte array (no copy)
      • append

        public ByteArray append​(java.lang.String data)
        Append a String
        Parameters:
        data - the data
        Returns:
        the byte array (no copy)
      • append

        public ByteArray append​(java.lang.StringBuilder data)
        Append a StringBuilder
        Parameters:
        data - the data
        Returns:
        the byte array (no copy)
      • append

        public ByteArray append​(java.lang.StringBuffer data)
        Append a StringBuffer
        Parameters:
        data - the data
        Returns:
        the byte array (no copy)
      • append

        public ByteArray append​(java.nio.ByteBuffer data)
        Append a buffer
        Parameters:
        data - the data
        Returns:
        the byte array (no copy)
      • reverse

        public ByteArray reverse()
        Reverse the internal array
        Returns:
        the reversed byte array (no copy)
      • trim

        public ByteArray trim​(byte ch)
        Trims the data with the given byte
        Parameters:
        ch - the trim byte
        Returns:
        the byte array (no copy)
      • trimRight

        public ByteArray trimRight​(byte ch)
        Trims the data right with the given byte
        Parameters:
        ch - the trim byte
        Returns:
        the byte array (no copy)
      • trimLeft

        public ByteArray trimLeft​(byte ch)
        Trims the data left with the given byte
        Parameters:
        ch - the trim byte
        Returns:
        the byte array (no copy)
      • replace

        public ByteArray replace​(byte toReplace,
                                 byte newByte)
        Replaces a given byte with another
        Parameters:
        toReplace - the byte to replace
        newByte - the new byte
        Returns:
        the byte array (no copy)
      • replace

        public ByteArray replace​(ByteArray replaceData,
                                 ByteArray newData)
        Replaces data in a given byte array.
        Parameters:
        replaceData - the data to replace
        newData - the new data
        Returns:
        the replaced byte array as a copy (copy)
      • indexOf

        public int indexOf​(byte toFind)
        Finds a byte in the data
        Parameters:
        toFind - the byte to find
        Returns:
        the position in the data
      • indexOf

        public int indexOf​(ByteArray toFind)
        Returns the index within this byte array of the first occurrence of the specified substring, starting at the specified index.
        Parameters:
        toFind - the sub array for which to search.
        Returns:
        the index within this array of the first occurrence of the specified sub array, starting at the specified index.
      • indexOf

        public int indexOf​(ByteArray toFind,
                           int fromIndex)
        Returns the index within this byte array of the first occurrence of the specified substring, starting at the specified index.
        Parameters:
        toFind - the sub array for which to search.
        fromIndex - the index from which to start the search.
        Returns:
        the index within this array of the first occurrence of the specified sub array, starting at the specified index.
      • lastIndexOf

        public int lastIndexOf​(byte toFind)
        Finds a byte in the data
        Parameters:
        toFind - the byte to find
        Returns:
        the position in the data
      • startsWith

        public boolean startsWith​(ByteArray prefix)
        Test if the array starts with the given prefix
        Parameters:
        prefix - the bytes to test
        Returns:
        true if it starts with the prefix
      • startsWith

        public boolean startsWith​(ByteArray prefix,
                                  int toffset)
        Test if the array starts with the given prefix
        Parameters:
        prefix - the bytes to test
        toffset - the offset to start
        Returns:
        true if it starts with the prefix
      • startsWith

        public boolean startsWith​(byte prefix)
        Test if the array starts with the given prefix
        Parameters:
        prefix - the bytes to test
        Returns:
        true if it starts with the prefix
      • countStartsWith

        public int countStartsWith​(ByteArray prefix)
        Test how many times the given prefix is contained at the beginning
        Parameters:
        prefix - the bytes to test
        Returns:
        how many times the prefix was found at the beginning
      • endsWith

        public boolean endsWith​(byte prefix)
        Test if the array ends with the given prefix
        Parameters:
        prefix - the bytes to test
        Returns:
        true if it ends with the prefix
      • endsWith

        public boolean endsWith​(ByteArray prefix)
        Test if the array starts with the given prefix
        Parameters:
        prefix - the bytes to test
        Returns:
        true if it starts with the prefix
      • countEndsWith

        public int countEndsWith​(ByteArray prefix)
        Test how many times the given prefix is contained at the end
        Parameters:
        prefix - the bytes to test
        Returns:
        how many times the prefix was found at the end
      • toHex

        public java.lang.String toHex()
        Convert to hex string
        Returns:
        the byte array as hex string
      • toString

        public java.lang.String toString()
        Convert to String
        Overrides:
        toString in class java.lang.Object
        Returns:
        the byte array as String
      • toBytes

        public byte[] toBytes()
        Return the bytes as a copy of the original
        Returns:
        the bytes (copy)
      • toLong

        public long toLong()
        Build a long from the array.
        Returns:
        the long value.
      • toByteBuffer

        public java.nio.ByteBuffer toByteBuffer()
        Return a byte buffer of the data
        Returns:
        the buffer
      • toByteArray

        public ByteArray toByteArray​(int startPos,
                                     int endPos)
        Return a sub part of the original bytes
        Parameters:
        startPos - the start position
        endPos - the end position
        Returns:
        the bytes (copy)
      • getBytes

        public ByteArray getBytes()
        Return the bytes as a copy
        Returns:
        the bytes (copy)
      • getBytes

        public ByteArray getBytes​(int startPos,
                                  int endPos)
        Return the bytes as a copy
        Parameters:
        startPos - the start position
        endPos - the end position
        Returns:
        the bytes (copy)
      • concat

        public ByteArray concat​(byte b)
        Concat a given byte and return a new byte array
        Parameters:
        b - the byte to append
        Returns:
        the concated byte array (copy)
      • concat

        public ByteArray concat​(ByteArray b)
        Concat a given byte and return a new byte array
        Parameters:
        b - the byte to append
        Returns:
        the concated byte array (copy)
      • get

        public byte get​(int pos)
                 throws java.lang.ArrayIndexOutOfBoundsException
        Return a specific byte from the byte array
        Parameters:
        pos - the position in the byte array
        Returns:
        a byte
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - in case of error
      • set

        public byte set​(int pos,
                        byte b)
                 throws java.lang.ArrayIndexOutOfBoundsException
        Set a specific byte in the byte array
        Parameters:
        pos - the position in the byte array
        b - the byte to set in the byte array
        Returns:
        the replaced byte
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - in case of error
      • length

        public int length()
        Return the number of bytes
        Returns:
        the number of bytes
      • erase

        public ByteArray erase()
        Erase the byte array
        Returns:
        the byte array (no copy)
      • chopRight

        public ByteArray chopRight()
        Chop a byte
        Returns:
        the byte array (no copy)
      • chopRight

        public ByteArray chopRight​(int num)
        Chop some bytes
        Parameters:
        num - number of bytes to chop
        Returns:
        the byte array (no copy)
      • chopLeft

        public ByteArray chopLeft()
        Chop a byte
        Returns:
        the byte array (no copy)
      • chopLeft

        public ByteArray chopLeft​(int num)
        Chop some bytes
        Parameters:
        num - number of bytes to chop
        Returns:
        the byte array (no copy)
      • write

        public int write​(java.nio.ByteBuffer src)
        Specified by:
        write in interface java.nio.channels.WritableByteChannel
        See Also:
        WritableByteChannel.write(java.nio.ByteBuffer)
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.nio.channels.Channel
        Specified by:
        close in interface java.io.Closeable
        See Also:
        Channel.close()
      • isOpen

        public boolean isOpen()
        Specified by:
        isOpen in interface java.nio.channels.Channel
        See Also:
        Channel.isOpen()
      • read

        public int read​(java.nio.ByteBuffer dst)
        Specified by:
        read in interface java.nio.channels.ReadableByteChannel
        See Also:
        ReadableByteChannel.read(java.nio.ByteBuffer)
      • equals

        public boolean equals​(java.lang.Object other)
        Implements the default functionality of the equals method. Subclasses should implements their own equals method like:
        public boolean equals( MyObject other )
         {
             if( this==other )
                 return true;
        
             if( !super.equals( other ) )
                 return false;
        
             if( x !=((MyObject)other).x )
                 return false;
        
             ...
             return true;
         }
        Overrides:
        equals in class java.lang.Object
        Parameters:
        other - the object to compare
        Returns:
        true if the objects are equals
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
        See Also:
        Object.hashCode()
      • clone

        public ByteArray clone()
        Overrides:
        clone in class java.lang.Object
        See Also:
        Object.clone()
      • crowUpDataSize

        protected void crowUpDataSize​(int requestedSpace)
        Gets the additional size
        Parameters:
        requestedSpace - the requested space
      • getNewDataSize

        protected int getNewDataSize​(int requestedSpace)
        Gets the new data size
        Parameters:
        requestedSpace - the requested space
        Returns:
        the new size