Class RandomAccessByteArray

java.lang.Object
de.intarsys.tools.randomaccess.AbstractRandomAccess
de.intarsys.tools.randomaccess.RandomAccessByteArray
All Implemented Interfaces:
IRandomAccess

public class RandomAccessByteArray extends AbstractRandomAccess
A wrapper around a byte array to allow random access like API.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected byte[]
    The byte array data
    protected int
    The number of valid bytes in the byte array
  • Constructor Summary

    Constructors
    Constructor
    Description
    RandomAccessByteArray(byte[] buffer)
     
    RandomAccessByteArray(byte[] buffer, int length)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected final void
    basicSetLengthResize(int newLength)
     
    void
    Closes this random access data container and releases any system resources associated with the stream.
    void
    Force changes to be made persistent.
    protected byte[]
     
    long
    Returns the length of this data container.
    long
    Returns the current offset in this data container.
    boolean
    true if this is a read only data container.
    int
    Reads a byte of data from this data container.
    int
    read(byte[] buffer)
    Reads up to buffer.length bytes of data from this data container into an array of bytes.
    int
    read(byte[] buffer, int bufferOffset, int numBytes)
    Reads up to len bytes of data from this data container into an array of bytes.
    void
    seek(long pOffset)
    Sets the offset, measured from the beginning of the data container at which the next read or write occurs.
    void
    seekBy(long delta)
    Sets the offset, measured from the current offset at which the next read or write occurs.
    void
    setLength(long newLength)
    Assign the length.
    byte[]
     
    void
    write(byte[] buffer)
    Writes b.length bytes from the specified byte array, starting at the current offset.
    void
    write(byte[] buffer, int start, int numBytes)
    Writes len bytes from the specified byte array starting at start.
    void
    write(int b)
    Writes the specified byte .

    Methods inherited from class de.intarsys.tools.randomaccess.AbstractRandomAccess

    asInputStream, asOutputStream, getPositionStack, mark, reset

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • data

      protected byte[] data
      The byte array data
    • length

      protected int length
      The number of valid bytes in the byte array
  • Constructor Details

    • RandomAccessByteArray

      public RandomAccessByteArray(byte[] buffer)
    • RandomAccessByteArray

      public RandomAccessByteArray(byte[] buffer, int length)
  • Method Details

    • basicSetLengthResize

      protected final void basicSetLengthResize(int newLength)
    • close

      public void close() throws IOException
      Description copied from interface: IRandomAccess
      Closes this random access data container and releases any system resources associated with the stream. A closed random access data container cannot perform input or output operations and cannot be reopened.
      Throws:
      IOException - if an I/O error occurs.
    • flush

      public void flush() throws IOException
      Description copied from interface: IRandomAccess
      Force changes to be made persistent.
      Throws:
      IOException
    • getData

      protected byte[] getData()
      Returns:
      Returns the data.
    • getLength

      public long getLength() throws IOException
      Description copied from interface: IRandomAccess
      Returns the length of this data container.
      Returns:
      the length of this data container, measured in bytes.
      Throws:
      IOException - if an I/O error occurs.
    • getOffset

      public long getOffset()
      Description copied from interface: IRandomAccess
      Returns the current offset in this data container.
      Returns:
      the offset from the beginning of the data container, in bytes, at which the next read or write occurs.
    • isReadOnly

      public boolean isReadOnly()
      Description copied from interface: IRandomAccess
      true if this is a read only data container.
      Returns:
      true if this is a read only data container.
    • read

      public int read()
      Description copied from interface: IRandomAccess
      Reads a byte of data from this data container. The byte is returned as an integer in the range 0 to 255 (0x00-0x0ff). This method blocks if no input is yet available.

      This method behaves in exactly the same way as the InputStream.read() method of InputStream.

      Returns:
      the next byte of data, or -1 if the end of the data container has been reached.
    • read

      public int read(byte[] buffer)
      Description copied from interface: IRandomAccess
      Reads up to buffer.length bytes of data from this data container into an array of bytes. This method blocks until at least one byte of input is available.

      This method behaves in the exactly the same way as the InputStream.read(byte[]) method of InputStream.

      Parameters:
      buffer - the buffer into which the data is read.
      Returns:
      the total number of bytes read into the buffer, or -1 if there is no more data because the end of this data container has been reached.
    • read

      public int read(byte[] buffer, int bufferOffset, int numBytes)
      Description copied from interface: IRandomAccess
      Reads up to len bytes of data from this data container into an array of bytes. This method blocks until at least one byte of input is available.

      Parameters:
      buffer - the buffer into which the data is read.
      bufferOffset - the start offset of the data.
      numBytes - the maximum number of bytes read.
      Returns:
      the total number of bytes read into the buffer, or -1 if there is no more data because the end of the file has been reached.
    • seek

      public void seek(long pOffset) throws IOException
      Description copied from interface: IRandomAccess
      Sets the offset, measured from the beginning of the data container at which the next read or write occurs. The offset may be set beyond the end of the data container. Setting the offset beyond the end of the data container does not change the data container length. The length will change only by writing after the offset has been set beyond the end of the data container.
      Parameters:
      pOffset - the offset position, measured in bytes from the beginning of the data container
      Throws:
      IOException - if offset is less than 0 or if an I/O error occurs.
    • seekBy

      public void seekBy(long delta) throws IOException
      Description copied from interface: IRandomAccess
      Sets the offset, measured from the current offset at which the next read or write occurs. The offset may be set beyond the end of the data container. Setting the offset beyond the end of the data container does not change the data container length. The length will change only by writing after the offset has been set beyond the end of the data container.
      Parameters:
      delta - the amount of bytes by which to change the current offset position
      Throws:
      IOException - if the resulting offset is less than 0 or if an I/O error occurs.
    • setLength

      public void setLength(long newLength)
      Description copied from interface: IRandomAccess
      Assign the length. All bytes after length are truncated. If the real length is currently less than newLength, the data structure will be enlarged.
    • toByteArray

      public byte[] toByteArray()
    • write

      public void write(byte[] buffer)
      Description copied from interface: IRandomAccess
      Writes b.length bytes from the specified byte array, starting at the current offset.
      Parameters:
      buffer - the data.
    • write

      public void write(byte[] buffer, int start, int numBytes)
      Description copied from interface: IRandomAccess
      Writes len bytes from the specified byte array starting at start.
      Parameters:
      buffer - the data.
      start - the start offset in the data.
      numBytes - the number of bytes to write.
    • write

      public void write(int b)
      Description copied from interface: IRandomAccess
      Writes the specified byte . The write starts at the current offset.
      Parameters:
      b - the byte to be written.