Package nom.tam.util

Class ByteArrayIO

java.lang.Object
nom.tam.util.ByteArrayIO
All Implemented Interfaces:
InputReader, OutputWriter, ReadWriteAccess

public class ByteArrayIO extends Object implements ReadWriteAccess
Reading from and writing to byte arrays with a stream-like interface (primarily for internal use) .
Since:
1.16
Author:
Attila Kovacs
  • Constructor Summary

    Constructors
    Constructor
    Description
    ByteArrayIO(byte[] buffer)
    Instantiates a new byte array with an IO interface, with a fixed-size buffer using the specified array as its backing storage.
    ByteArrayIO(int initialCapacity)
    Instantiates a new byte array with an IO interface, with a growable buffer initialized to the specific size.
  • Method Summary

    Modifier and Type
    Method
    Description
    final int
    Returns the current capacity of this buffer, that is the total number of bytes that may be written into the current backing buffer.
    Returns a copy of this byte array with an IO interface, including a deep copy of the buffered data.
    byte[]
    Returns the underlying byte array, which is the backing array of this buffer.
    final int
    Returns the number of bytes available for reading from the current position.
    final long
    Returns the current total length of this instance, that is the total number of bytes that may be read from it.
    final long
    Returns the current read/write position in this instance.
    void
    position(long offset)
    Sets a new position for the next read or write in this instance.
    final int
    Reads a byte.
    final int
    read(byte[] b, int from, int length)
    Reads up to the specified number of bytes into a buffer.
    void
    setLength(int length)
    Changes the length of this buffer.
    final void
    write(byte[] b, int from, int length)
    Writes up to the specified number of bytes from a buffer to the stream.
    final void
    write(int b)
    Writes a byte.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ByteArrayIO

      public ByteArrayIO(byte[] buffer)
      Instantiates a new byte array with an IO interface, with a fixed-size buffer using the specified array as its backing storage.
      Parameters:
      buffer - the fixed buffer.
    • ByteArrayIO

      public ByteArrayIO(int initialCapacity) throws IllegalArgumentException
      Instantiates a new byte array with an IO interface, with a growable buffer initialized to the specific size.
      Parameters:
      initialCapacity - the number of bytes to contain in the buffer.
      Throws:
      IllegalArgumentException - if the initial capacity is 0 or negative
  • Method Details

    • copy

      public ByteArrayIO copy()
      Returns a copy of this byte array with an IO interface, including a deep copy of the buffered data.
      Returns:
      a deep copy of this byte array with an IO interface instance.
    • getBuffer

      public byte[] getBuffer()
      Returns the underlying byte array, which is the backing array of this buffer.
      Returns:
      the backing array of this buffer.
    • capacity

      public final int capacity()
      Returns the current capacity of this buffer, that is the total number of bytes that may be written into the current backing buffer.
      Returns:
      the current size of the backing array.
    • length

      public final long length()
      Description copied from interface: ReadWriteAccess
      Returns the current total length of this instance, that is the total number of bytes that may be read from it.
      Specified by:
      length in interface ReadWriteAccess
      Returns:
      the total number of bytes contained in this instance
    • getRemaining

      public final int getRemaining()
      Returns the number of bytes available for reading from the current position.
      Returns:
      the number of bytes that can be read from this buffer from the current position.
    • position

      public final long position()
      Description copied from interface: ReadWriteAccess
      Returns the current read/write position in this instance. The position may exceed the length, depending on implementation, just as seek may go beyond the file size in RandomAccessFile.
      Specified by:
      position in interface ReadWriteAccess
      Returns:
      the current read/write position.
      See Also:
    • position

      public void position(long offset) throws IOException
      Description copied from interface: ReadWriteAccess
      Sets a new position for the next read or write in this instance. The position may exceed the length, depending on implementation, just as seek may go beyond the file size in RandomAccessFile.
      Specified by:
      position in interface ReadWriteAccess
      Parameters:
      offset - the new read/write position.
      Throws:
      IOException - if there was an IO error.
      See Also:
    • setLength

      public void setLength(int length) throws IllegalArgumentException
      Changes the length of this buffer. The total number of bytes available from reading from this buffer will be that of the new length. If the buffer is truncated and its pointer is positioned beyond the new size, then the pointer is changed to point to the new buffer end. If the buffer is enlarged beyond it current capacity, its capacity will grow as necessary provided the buffer is growable. Otherwise, an EOFException is thrown if the new length is beyond the fixed buffer capacity. If the new length is larger than the old one, the added buffer segment may have undefined contents.
      Parameters:
      length - The buffer length, that is number of bytes available for reading.
      Throws:
      IllegalArgumentException - if the length is negative or if the new new length exceeds the capacity of a fixed-type buffer.
      See Also:
    • write

      public final void write(int b) throws IOException
      Description copied from interface: OutputWriter
      Writes a byte. See the general contract of DataOutputStream.write(int).
      Specified by:
      write in interface OutputWriter
      Parameters:
      b - the (unsigned) byte value to write.
      Throws:
      IOException - if there was an underlying IO error
      See Also:
    • write

      public final void write(byte[] b, int from, int length) throws IOException
      Description copied from interface: OutputWriter
      Writes up to the specified number of bytes from a buffer to the stream. See the general contract of DataOutputStream.write(byte[], int, int).
      Specified by:
      write in interface OutputWriter
      Parameters:
      b - the buffer
      from - the starting buffer index
      length - the number of bytes to write.
      Throws:
      IOException - if there was an underlying IO error
      See Also:
    • read

      public final int read() throws IOException
      Description copied from interface: InputReader
      Reads a byte. See the general contract of FilterInputStream.read().
      Specified by:
      read in interface InputReader
      Returns:
      the (unsigned) byte value or -1 if there is nothing left to read.
      Throws:
      IOException - if there was an underlying IO error
      See Also:
    • read

      public final int read(byte[] b, int from, int length)
      Description copied from interface: InputReader
      Reads up to the specified number of bytes into a buffer. See the general contract of DataInputStream.read(byte[], int, int).
      Specified by:
      read in interface InputReader
      Parameters:
      b - the buffer
      from - the starting buffer index
      length - the number of bytes to read.
      Returns:
      the number of bytes actually read, or -1 if there is nothing left to read.
      See Also: