public class ByteBuffer
extends java.lang.Object
When encoding (or reading) data into a ByteBuffer, the underlying byte array will automatically be resized as necessary.
In practice subclasses should define methods that are ideal for the target format (ASN.1, MPEG, PDF, ... etc) but methods for encoding and decoding integers are included.
This class is similar in purpose to java.nio.ByteBuffer, java.io.ByteArray{InputStream,OutputStream} and java.io.Data{InputStream,OutputStream}.
Modifier and Type | Field and Description |
---|---|
protected byte[] |
buf |
protected int |
db |
protected int |
de |
protected int |
di |
protected int |
frame |
protected ByteBuffer |
master |
protected int |
start |
Constructor and Description |
---|
ByteBuffer()
Create an empty buffer.
|
ByteBuffer(byte[] buf,
int off,
int len)
Create a buffer with the supplied array with data at offset off of size len.
|
Modifier and Type | Method and Description |
---|---|
void |
advance(int n)
Advance the current buffer index by n.
|
int |
align(int boundary)
Advance the current buffer index to the next multiple of boundry.
|
int |
align(int boundary,
byte value)
Advance the current buffer index to the next multiple of boundry and fill any padding bytes with the supplied byte value.
|
void |
compact()
This will invalidate any derived buffers.
|
void |
decodeBytes(byte[] dst,
int off,
int len)
Copy the bytes from the underlying byte array into the supplied byte array and advance the current buffer index.
|
int |
decodeUint16be() |
int |
decodeUint16le() |
int |
decodeUint32be() |
int |
decodeUint32le() |
long |
decodeUint64be() |
long |
decodeUint64le() |
byte |
decodeUint8() |
ByteBuffer |
derive(int index)
Return a new ByteBuffer that shares the same underlying buffer, start
and length but with an independent index.
|
void |
encodeBytes(byte[] src,
int off,
int len)
Copy the supplied bytes into the underlying byte array and advance the current buffer index.
|
void |
encodeUint16be(int u16) |
void |
encodeUint16le(int u16) |
void |
encodeUint32be(int u32) |
void |
encodeUint32le(int u32) |
void |
encodeUint64be(long u64) |
void |
encodeUint64le(long u64) |
void |
encodeUint8(int u8) |
protected void |
ensureSpace(int len)
If necessary, the underlying byte array to be resized to accommodate the specified amount of space relative to the current buffer index.
|
byte[] |
getBuffer() |
int |
getFrame()
Get the frame index relative to the "start" position.
|
int |
getIndex()
Get the current index relative to the "start" position.
|
int |
getLength()
Get the length of the data in the buffer.
|
int |
getRemaining()
Return the amount of data in the buffer relative to the current index.
|
int |
getStart()
Return the "start" position.
|
int |
getTailspace()
Return the amount of space available at the end of the buffer.
|
void |
hexdump(java.io.PrintStream out,
int index,
int len) |
int |
read(java.io.InputStream in) |
void |
reset() |
void |
setFrame(int index)
Set the frame index relative to the "start" position.
|
void |
setIndex(int index)
Set the current index relative to the "start" position.
|
void |
setStart()
Set the "start" position of the underlying byte[] array to the current index.
|
int |
shift(int index,
boolean compact) |
byte[] |
toByteArray() |
java.lang.String |
toString() |
int |
write(java.io.OutputStream out,
int index,
boolean advance) |
protected ByteBuffer master
protected byte[] buf
protected int start
protected int db
protected int di
protected int de
protected int frame
public ByteBuffer(byte[] buf, int off, int len)
buf
- the underlying byte array of this buffer.off
- the offset within buf to the data of interest.len
- the length of data starting at off.public ByteBuffer()
public void reset()
public ByteBuffer derive(int index)
Derived buffers are invalid if compact()
is called.
public byte[] getBuffer()
public void setStart()
public int getStart()
public void setIndex(int index)
public int getIndex()
public void setFrame(int index)
The "frame" index value has no impact on the behavior of other methods of this class. However, the intent is that it be used to mark the position up to which data may be written out with write(java.io.OutputStream, int, boolean)
. Framing is used for I/O multiplexing.
index
- the index marking the end of the "frame".public int getFrame()
public int getLength()
read(java.io.InputStream)
. The data begining is only advanced by write(java.io.OutputStream, int, boolean)
or shift(int, boolean)
.public int getTailspace()
public int getRemaining()
protected void ensureSpace(int len)
For example, with an underlying byte array size of 64 and current buffer index of 50, calling ensureSpace(200)
would cause the underlying byte array to resized to 256 bytes because 50 + 200 is 250 and 256 is the next power of 2 that hold 250 bytes.
public void advance(int n)
n
- the number of bytes to advance the index.public int align(int boundary)
public int align(int boundary, byte value)
public void encodeBytes(byte[] src, int off, int len) throws EncodingException
EncodingException
public void decodeBytes(byte[] dst, int off, int len) throws EncodingException
dst
- EncodingException
- if the len parameter exceeds the amount of remaining data.public void encodeUint8(int u8)
public void encodeUint16le(int u16)
public void encodeUint16be(int u16)
public void encodeUint32le(int u32)
public void encodeUint32be(int u32)
public void encodeUint64le(long u64)
public void encodeUint64be(long u64)
public byte decodeUint8()
public int decodeUint16le()
public int decodeUint16be()
public int decodeUint32le()
public int decodeUint32be()
public long decodeUint64le()
public long decodeUint64be()
public void compact()
public int write(java.io.OutputStream out, int index, boolean advance) throws java.io.IOException
java.io.IOException
public int shift(int index, boolean compact)
public int read(java.io.InputStream in) throws java.io.IOException
java.io.IOException
public byte[] toByteArray()
public void hexdump(java.io.PrintStream out, int index, int len)
public java.lang.String toString()
toString
in class java.lang.Object