public class ByteBuffer extends Object
This class is modeled on the java.nio.Buffer
interface. Specifically, the properties capacity and limit are
defined as for Buffer
.
Modifier and Type | Field and Description |
---|---|
static int |
DOUBLE_SIZE
byte size for doubles
|
static long |
UINT_MAX_VALUE
Max.
|
static int |
UINT_SIZE
byte size for unsigned int
|
Modifier and Type | Method and Description |
---|---|
static ByteBuffer |
allocate(int capacity)
Allocates a new
ByteBuffer of the specified capacity. |
int |
capacity()
Returns this buffer's capacity.
|
boolean |
equals(Object o) |
static ByteBuffer |
from(byte[] bytes)
Wraps a byte array into a
ByteBuffer . |
static ByteBuffer |
from(String hexString)
Creates a
ByteBuffer from a hexadecimal string. |
byte |
get()
Relative get method.
|
ByteOrder |
getByteOrder()
Gets the byte order of this instance.
|
double |
getDouble()
Reads the next 8 bytes as a double from this instance at the current position, taking into account the byte-order,
and then increments the position by eight.
|
float |
getFloat()
Reads the next 4 bytes as a float from this instance at the current position, taking into account the byte-order,
and then increments the position by four.
|
int |
getInt()
Reads the next 4 bytes as an int from this instance at the current position, taking into account the byte-order,
and then increments the position by four.
|
long |
getLong()
Reads the next 8 bytes as a long from this instance at the current position, taking into account the byte-order,
and then increments the position by eight.
|
long |
getUInt()
Reads the next 4 bytes as an unsigned integer from this instance at the current position,
taking into account the byte-order, and then increments the position by four.
|
int |
hashCode() |
boolean |
hasSameContent(ByteBuffer other)
Used for testing purposes.
|
boolean |
isEmpty()
Reports if this buffer is empty (holds no bytes).
|
int |
limit()
Returns this buffer's limit.
|
void |
put(byte value)
Writes the given byte into this buffer at the current position, and then increments the position.
|
void |
putDouble(Double value)
Writes the specified double-value as 8 bytes to this instance at the current position, respecting the byte-order,
and then increments the position by eight.
|
void |
putFloat(float value)
Writes the specified float-value as 4 bytes to this instance at the current position, respecting the byte-order,
and then increments the position by four.
|
void |
putInt(int value)
Writes the specified int-value as 4 bytes to this instance at the current position, respecting the byte-order,
and then increments the position by four.
|
void |
putLong(long value)
Writes the specified long value as 8 bytes to this instance at the current position, respecting the byte-order,
and then increments the position by eight.
|
void |
putUInt(long value)
Interprets the specified long-value as and unsigned integer, and appends it as 4 bytes
to this instance at the current position, respecting the byte-order.
|
void |
rewind()
Rewinds the buffer.
|
void |
setByteOrder(ByteOrder wbo)
Sets the byte order for this instance.
|
byte[] |
toByteArray()
Returns the byte array that backs this buffer.
|
String |
toString()
Returns this instance as a hexadecimal string.
|
public static final int UINT_SIZE
public static final int DOUBLE_SIZE
public static final long UINT_MAX_VALUE
public static ByteBuffer from(String hexString)
ByteBuffer
from a hexadecimal string.
Every two chars in the string are interpreted as the hexadecimal representation of a byte. If the string length is odd, the last character will be ignored.
hexString
- the bytes represented in hexadecimal formpublic String toString()
public static ByteBuffer from(byte[] bytes)
ByteBuffer
.
The new buffer will be backed by the given byte array; that is, modifications to the buffer will cause the array
to be modified and vice versa. The new buffer's capacity and limit will be bytes.length.bytes
- The array that will back this bufferpublic static ByteBuffer allocate(int capacity)
ByteBuffer
of the specified capacity.
The new buffer's position will be zero, its limit will be its capacity and each of its elements will be
initialized to zero.capacity
- The new buffer's capacity, in bytesByteBuffer
instance of the specified capacitypublic byte get()
BufferAccessException
- If the buffer's current position is not smaller than its limit.public void put(byte value)
value
- The byte to be writtenBufferAccessException
- If this buffer's current position is not smaller than its limit.public int capacity()
public int limit()
public void rewind()
After rewind, the next get() or put() will take place on the first element of this instance.
public boolean isEmpty()
public void setByteOrder(ByteOrder wbo)
public int getInt()
BufferAccessException
- If there are fewer than four bytes remaining in this buffer.public void putInt(int value)
value
- The int value to be writtenBufferAccessException
- If there are fewer than four bytes remaining in this buffer.public long getLong()
BufferAccessException
- If there are fewer than eight bytes remaining in this buffer.public void putLong(long value)
value
- The long value to be writtenBufferAccessException
- If there are fewer than eight bytes remaining in this buffer.public float getFloat()
BufferAccessException
- If there are fewer than four bytes remaining in this buffer.public void putFloat(float value)
value
- The float value to be writtenBufferAccessException
- If there are fewer than four bytes remaining in this buffer.public double getDouble()
BufferAccessException
- If there are fewer than eight bytes remaining in this buffer.public void putDouble(Double value)
value
- The double value to be writtenBufferAccessException
- If there are fewer than eight bytes remaining in this buffer.public long getUInt()
BufferAccessException
- If there are fewer than four bytes remaining in this bufferpublic void putUInt(long value)
value
- The unsigned integer value to be writtenRuntimeException
- If the specified value is larger than the largest unsigned integer (4294967295L)BufferAccessException
- If there are fewer than eight bytes remaining in this buffer.public ByteOrder getByteOrder()
public byte[] toByteArray()
public boolean hasSameContent(ByteBuffer other)
other
- another ByteBuffer
Copyright © 2015 geolatte.org. All rights reserved.