Package nom.tam.util
Class ByteArrayIO
java.lang.Object
nom.tam.util.ByteArrayIO
- All Implemented Interfaces:
InputReader
,OutputWriter
,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
ConstructorDescriptionByteArrayIO
(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 TypeMethodDescriptionfinal 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.copy()
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
length()
Returns the current total length of this instance, that is the total number of bytes that may be read from it.final long
position()
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
read()
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.
-
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
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
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 interfaceReadWriteAccess
- 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 asseek
may go beyond the file size inRandomAccessFile
.- Specified by:
position
in interfaceReadWriteAccess
- Returns:
- the current read/write position.
- See Also:
-
position
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 asseek
may go beyond the file size inRandomAccessFile
.- Specified by:
position
in interfaceReadWriteAccess
- Parameters:
offset
- the new read/write position.- Throws:
IOException
- if there was an IO error.- See Also:
-
setLength
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
Description copied from interface:OutputWriter
Writes a byte. See the general contract ofDataOutputStream.write(int)
.- Specified by:
write
in interfaceOutputWriter
- Parameters:
b
- the (unsigned) byte value to write.- Throws:
IOException
- if there was an underlying IO error- See Also:
-
write
Description copied from interface:OutputWriter
Writes up to the specified number of bytes from a buffer to the stream. See the general contract ofDataOutputStream.write(byte[], int, int)
.- Specified by:
write
in interfaceOutputWriter
- Parameters:
b
- the bufferfrom
- the starting buffer indexlength
- the number of bytes to write.- Throws:
IOException
- if there was an underlying IO error- See Also:
-
read
Description copied from interface:InputReader
Reads a byte. See the general contract ofFilterInputStream.read()
.- Specified by:
read
in interfaceInputReader
- 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 ofDataInputStream.read(byte[], int, int)
.- Specified by:
read
in interfaceInputReader
- Parameters:
b
- the bufferfrom
- the starting buffer indexlength
- the number of bytes to read.- Returns:
- the number of bytes actually read, or -1 if there is nothing left to read.
- See Also:
-