A random-accessible sequence of zero or more bytes (octets). This interface provides an abstract view for one or more primitive byte arrays ( {@code byte[]}) and {@linkplain ByteBuffer NIO buffers}.
Index
Unlike an ordinary primitive byte array, {@link ByteArray} allows a negativeindex, which means its {@linkplain #firstIndex() first index} can be eitherless than {@code 0}, equal to {@code 0} or even greater than {@code 0}. Therefore, you should not make an assumption that an array's first index is always {@code 0}. For example, to iterate all bytes over an array one by one, you have to do the following, regardless of the array's implementation detail:
ByteArray array = ...; for (int i = array.firstIndex(); i < array.endIndex(); i ++) { byte b = array.get8(i); System.out.println((char) b); }
Getters and Setters
Various getter and setter methods are provided, from a single byte access method to big and little endian 16 / 24 / 32 / 48 / 64-bit integer access methods. Bulk access operations for primitive byte array, NIO buffer and {@link ByteArray} are also available.
Search operations
{@code indexOf()} and
{@code lastIndexOf()} methods help you locate an index of a value which meets a certain criteria. Complicated dynamic sequential search can be done with {@link ByteArrayIndexFinder} as well as simple static single byte search.
I/O operations
{@code copyTo()} methods write the partial or entire content of an array to an {@link OutputStream} or a NIO {@link Channel}. Gathering write operation is performed whenever possible.
Conversion to a NIO buffer
{@code getByteBuffer()} methods return a NIO buffer which is either a wrapper (using {@link ByteBuffer#wrap(byte[])}) or a deep copy (creating a new NIO buffer) of the partial or entire content of an array. These methods will always avoid buffer allocation and memory copy whenever possible, but there's no guarantee that they will always return a wrapper.
@author The Netty Project (netty@googlegroups.com)
@author Trustin Lee (trustin@gmail.com)
@version $Rev: 456 $, $Date: 2008-06-26 10:14:06 +0900 (Thu, 26 Jun 2008) $
@apiviz.landmark