Package org.geolatte.geom
Class ByteBuffer
- java.lang.Object
-
- org.geolatte.geom.ByteBuffer
-
public class ByteBuffer extends Object
A byte buffer class.This class is modeled on the
java.nio.Bufferinterface. Specifically, the properties capacity and limit are defined as forBuffer.- A buffer's capacity is the number of elements it contains. The capacity of a buffer is never negative and never changes.
- A buffer's limit is the index of the first element that should not be read or written. A buffer's limit is never negative and is never greater than its capacity.
- Author:
- Karel Maesen, Geovise BVBA creation-date: Oct 29, 2010
-
-
Field Summary
Fields Modifier and Type Field Description static intDOUBLE_SIZEbyte size for doublesstatic longUINT_MAX_VALUEMax.static intUINT_SIZEbyte size for unsigned int
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static ByteBufferallocate(int capacity)Allocates a newByteBufferof the specified capacity.intcapacity()Returns this buffer's capacity.booleanequals(Object o)static ByteBufferfrom(byte[] bytes)Wraps a byte array into aByteBuffer.static ByteBufferfrom(String hexString)Creates aByteBufferfrom a hexadecimal string.byteget()Relative get method.ByteOrdergetByteOrder()Gets the byte order of this instance.doublegetDouble()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.floatgetFloat()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.intgetInt()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.longgetLong()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.longgetUInt()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.inthashCode()booleanhasSameContent(ByteBuffer other)Used for testing purposes.booleanisEmpty()Reports if this buffer is empty (holds no bytes).intlimit()Returns this buffer's limit.voidput(byte value)Writes the given byte into this buffer at the current position, and then increments the position.voidputDouble(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.voidputFloat(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.voidputInt(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.voidputLong(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.voidputUInt(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.voidrewind()Rewinds the buffer.voidsetByteOrder(ByteOrder wbo)Sets the byte order for this instance.byte[]toByteArray()Returns the byte array that backs this buffer.StringtoString()Returns this instance as a hexadecimal string.
-
-
-
Field Detail
-
UINT_SIZE
public static final int UINT_SIZE
byte size for unsigned int- See Also:
- Constant Field Values
-
DOUBLE_SIZE
public static final int DOUBLE_SIZE
byte size for doubles- See Also:
- Constant Field Values
-
UINT_MAX_VALUE
public static final long UINT_MAX_VALUE
Max. permissible value for an unsigned int.- See Also:
- Constant Field Values
-
-
Method Detail
-
from
public static ByteBuffer from(String hexString)
Creates aByteBufferfrom 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.
- Parameters:
hexString- the bytes represented in hexadecimal form- Returns:
- A ByteBuffer based on the hexadecimal string
-
toString
public String toString()
Returns this instance as a hexadecimal string.
-
from
public static ByteBuffer from(byte[] bytes)
Wraps a byte array into aByteBuffer. 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.- Parameters:
bytes- The array that will back this buffer- Returns:
- The new byte buffer.
-
allocate
public static ByteBuffer allocate(int capacity)
Allocates a newByteBufferof 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.- Parameters:
capacity- The new buffer's capacity, in bytes- Returns:
- a new
ByteBufferinstance of the specified capacity
-
get
public byte get()
Relative get method. Reads the byte at this buffer's current position, and then increments the position.- Returns:
- The byte at the buffer's current position
- Throws:
BufferAccessException- If the buffer's current position is not smaller than its limit.
-
put
public void put(byte value)
Writes the given byte into this buffer at the current position, and then increments the position.- Parameters:
value- The byte to be written- Throws:
BufferAccessException- If this buffer's current position is not smaller than its limit.
-
capacity
public int capacity()
Returns this buffer's capacity.- Returns:
- The capacity of this buffer
-
limit
public int limit()
Returns this buffer's limit.- Returns:
- The limit of this buffer
-
rewind
public void rewind()
Rewinds the buffer.After rewind, the next get() or put() will take place on the first element of this instance.
-
isEmpty
public boolean isEmpty()
Reports if this buffer is empty (holds no bytes).- Returns:
- True if limit is 0, otherwise false
-
setByteOrder
public void setByteOrder(ByteOrder wbo)
Sets the byte order for this instance.
-
getInt
public 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.- Returns:
- The int value at the buffer's current position
- Throws:
BufferAccessException- If there are fewer than four bytes remaining in this buffer.
-
putInt
public 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.- Parameters:
value- The int value to be written- Throws:
BufferAccessException- If there are fewer than four bytes remaining in this buffer.
-
getLong
public 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.- Returns:
- The long value at the buffer's current position
- Throws:
BufferAccessException- If there are fewer than eight bytes remaining in this buffer.
-
putLong
public 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.- Parameters:
value- The long value to be written- Throws:
BufferAccessException- If there are fewer than eight bytes remaining in this buffer.
-
getFloat
public 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.- Returns:
- The float value at the buffer's current position
- Throws:
BufferAccessException- If there are fewer than four bytes remaining in this buffer.
-
putFloat
public 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.- Parameters:
value- The float value to be written- Throws:
BufferAccessException- If there are fewer than four bytes remaining in this buffer.
-
getDouble
public 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.- Returns:
- The double value at the buffer's current position
- Throws:
BufferAccessException- If there are fewer than eight bytes remaining in this buffer.
-
putDouble
public 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.- Parameters:
value- The double value to be written- Throws:
BufferAccessException- If there are fewer than eight bytes remaining in this buffer.
-
getUInt
public 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.- Returns:
- The value of the 4-byte unsigned integer at the current position as a long
- Throws:
BufferAccessException- If there are fewer than four bytes remaining in this buffer
-
putUInt
public 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.- Parameters:
value- The unsigned integer value to be written- Throws:
RuntimeException- If the specified value is larger than the largest unsigned integer (4294967295L)BufferAccessException- If there are fewer than eight bytes remaining in this buffer.
-
getByteOrder
public ByteOrder getByteOrder()
Gets the byte order of this instance.- Returns:
- This buffer's byte order
-
toByteArray
public byte[] toByteArray()
Returns the byte array that backs this buffer.- Returns:
- The array that backs this buffer
-
hasSameContent
public boolean hasSameContent(ByteBuffer other)
Used for testing purposes.- Parameters:
other- anotherByteBuffer- Returns:
- true if both buffers contain the same bytes, false otherwise
-
-