java.lang.Object
hla.rti1516e.encoding.ByteWrapper
Utility class for managing data in byte arrays.
-
Constructor Summary
ConstructorsConstructorDescriptionConstruct a ByteWrapper backed by a zero-length byte array.ByteWrapper(byte[] buffer) Constructs aByteWrapperbacked by the specified byte array.ByteWrapper(byte[] buffer, int offset) Constructs aByteWrapperbacked by the specified byte array.ByteWrapper(byte[] buffer, int offset, int length) Constructs aByteWrapperbacked by the specified byte array.ByteWrapper(int length) Construct a ByteWrapper backed by a byte array with the specifiedlength. -
Method Summary
Modifier and TypeMethodDescriptionfinal voidadvance(int n) Advances the current position byn.voidalign(int alignment) Advances the current position until the specifiedalignmentis achieved.final byte[]array()Returns the backing array.final intget()Reads the next byte of the ByteWrapper.final voidget(byte[] dest) Readsdest.lengthbytes from the ByteWrapper intodest.final intgetInt()Reads the next four byte of the ByteWrapper as a hi-endian 32-bit integer.final intgetPos()Returns the current position.voidput(byte[] src) Writes a byte array to the ByteWrapper and advances the current posisiton by the size of the byte array.voidput(byte[] src, int offset, int count) Writes a subset of a byte array to the ByteWrapper and advances the current posisiton by the size of the subset.voidput(int b) Writesbyteto the ByteWrapper and advances the current position by 1.voidputInt(int value) Writesvalueto the ByteWrapper as a hi-endian 32-bit integer.voidreassign(byte[] buffer, int offset, int length) Changes the backing store used by this ByteWrapper.intReturns the number of remaining bytes in the byte array.voidreset()Resets current position to the start of the ByteWrapper.slice()Creates aByteWrapperbacked by the same byte array using the current position as its offset.slice(int length) Creates aByteWrapperbacked by the same byte array using the current position as its offset, and the specifiedlengthto mark the limit.toString()Returns a string representation of the ByteWrapper.voidverify(int length) Verify thatlengthbytes can be read.
-
Constructor Details
-
ByteWrapper
public ByteWrapper()Construct a ByteWrapper backed by a zero-length byte array. -
ByteWrapper
public ByteWrapper(int length) Construct a ByteWrapper backed by a byte array with the specifiedlength.- Parameters:
length- length of the backing byte array
-
ByteWrapper
public ByteWrapper(byte[] buffer) Constructs aByteWrapperbacked by the specified byte array. (Changes to the ByteWrapper will write through to the specified byte array.)- Parameters:
buffer- backing byte array
-
ByteWrapper
public ByteWrapper(byte[] buffer, int offset) Constructs aByteWrapperbacked by the specified byte array. (Changes to the ByteWrapper will write through to the specified byte array.) Theoffsetwill be at the start position. Limit will be atbuffer.length.- Parameters:
buffer- backing byte arrayoffset- start position offset
-
ByteWrapper
public ByteWrapper(byte[] buffer, int offset, int length) Constructs aByteWrapperbacked by the specified byte array. (Changes to the ByteWrapper will write through to the specified byte array.)- Parameters:
buffer- backing byte arrayoffset- start position offsetlength- length of the segment to use
-
-
Method Details
-
reassign
public void reassign(byte[] buffer, int offset, int length) Changes the backing store used by this ByteWrapper. Changes to the ByteWrapper will write through to the specified byte array.- Parameters:
buffer- backing byte arrayoffset- start position offsetlength- length of the segment to use
-
reset
public void reset()Resets current position to the start of the ByteWrapper. -
verify
public void verify(int length) Verify thatlengthbytes can be read.- Parameters:
length- number of byte to verify- Throws:
ArrayIndexOutOfBoundsException- iflengthbytes can not be read
-
getInt
public final int getInt()Reads the next four byte of the ByteWrapper as a hi-endian 32-bit integer. The current position is increased by 4.- Returns:
- decoded value
- Throws:
ArrayIndexOutOfBoundsException- if the bytes can not be read
-
get
public final int get()Reads the next byte of the ByteWrapper. The current position is increased by 1.- Returns:
- decoded value
- Throws:
ArrayIndexOutOfBoundsException- if the bytes can not be read
-
get
public final void get(byte[] dest) Readsdest.lengthbytes from the ByteWrapper intodest. The current position is increased bydest.length.- Parameters:
dest- destination for the read bytes- Throws:
ArrayIndexOutOfBoundsException- if the bytes can not be read
-
putInt
public void putInt(int value) Writesvalueto the ByteWrapper as a hi-endian 32-bit integer. The current position is increased by 4.- Parameters:
value- value to write- Throws:
ArrayIndexOutOfBoundsException- if the bytes can not be written
-
put
public void put(int b) Writesbyteto the ByteWrapper and advances the current position by 1.- Parameters:
b- byte to write- Throws:
ArrayIndexOutOfBoundsException- if the bytes can not be written
-
put
public void put(byte[] src) Writes a byte array to the ByteWrapper and advances the current posisiton by the size of the byte array.- Parameters:
src- byte array to write- Throws:
ArrayIndexOutOfBoundsException- if the bytes can not be written
-
put
public void put(byte[] src, int offset, int count) Writes a subset of a byte array to the ByteWrapper and advances the current posisiton by the size of the subset.- Parameters:
src- byte array to writeoffset- offset of subset to writecount- size of offset to write- Throws:
ArrayIndexOutOfBoundsException- if the bytes can not be written
-
array
public final byte[] array()Returns the backing array.- Returns:
- the backing byte array
-
getPos
public final int getPos()Returns the current position.- Returns:
- the current potition within the byte array
- See Also:
-
remaining
public int remaining()Returns the number of remaining bytes in the byte array.- Returns:
- the number of remaining bytes in the byte array
-
advance
public final void advance(int n) Advances the current position byn.- Parameters:
n- number of positions to advance- Throws:
ArrayIndexOutOfBoundsException- if the position can not be advanced
-
align
public void align(int alignment) Advances the current position until the specifiedalignmentis achieved.- Parameters:
alignment- alignment that the current position must support
-
slice
Creates aByteWrapperbacked by the same byte array using the current position as its offset.- Returns:
- a new
ByteWrapperbacked by the same byte array starting at the current position
-
slice
Creates aByteWrapperbacked by the same byte array using the current position as its offset, and the specifiedlengthto mark the limit.- Parameters:
length- length of the newByteWrapper- Returns:
- a new
ByteWrapperbacked by the same byte array starting at the current position with the definedlength - Throws:
ArrayIndexOutOfBoundsException- if thelengthis to long
-
toString
Returns a string representation of the ByteWrapper.
-