Class ByteArray

java.lang.Object
com.github.toolarium.common.ByteArray
All Implemented Interfaces:
Closeable, Serializable, AutoCloseable, Cloneable, Channel, ReadableByteChannel, WritableByteChannel

public class ByteArray extends Object implements Serializable, WritableByteChannel, ReadableByteChannel, Cloneable
Class used to represent an array of bytes as an Object.
See Also:
  • Field Details

    • 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:
  • Constructor Details

    • ByteArray

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

      public ByteArray(String data)
      Create a ByteArray from a String
      Parameters:
      data - initial data
    • ByteArray

      public ByteArray(StringBuilder data)
      Create a ByteArray from a String
      Parameters:
      data - initial data
    • ByteArray

      public ByteArray(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 Details

    • 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(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(String data)
      Append a String
      Parameters:
      data - the data
      Returns:
      the byte array (no copy)
    • append

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

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

      public ByteArray append(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 String toHex()
      Convert to hex string
      Returns:
      the byte array as hex string
    • toString

      public String toString()
      Convert to String
      Overrides:
      toString in class 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 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 ArrayIndexOutOfBoundsException
      Return a specific byte from the byte array
      Parameters:
      pos - the position in the byte array
      Returns:
      a byte
      Throws:
      ArrayIndexOutOfBoundsException - in case of error
    • set

      public byte set(int pos, byte b) throws 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:
      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(ByteBuffer src)
      Specified by:
      write in interface WritableByteChannel
      See Also:
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Channel
      Specified by:
      close in interface Closeable
      See Also:
    • isOpen

      public boolean isOpen()
      Specified by:
      isOpen in interface Channel
      See Also:
    • read

      public int read(ByteBuffer dst)
      Specified by:
      read in interface ReadableByteChannel
      See Also:
    • equals

      public boolean equals(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 Object
      Parameters:
      other - the object to compare
      Returns:
      true if the objects are equals
    • hashCode

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

      public ByteArray clone()
      Overrides:
      clone in class Object
      See Also:
    • 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