Package org.aion.avm.userlib
Class AionBuffer
- java.lang.Object
-
- org.aion.avm.userlib.AionBuffer
-
public class AionBuffer extends java.lang.Object
A buffer, much like an NIO ByteBuffer, which allows the easy encoding/decoding of primitive values.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static AionBuffer
allocate(int capacity)
Creates a new AionBuffer instance with the given capacity.AionBuffer
clear()
Resets the position to 0 and the limit to the full capacity of the buffer.boolean
equals(java.lang.Object ob)
AionBuffer
flip()
Sets the limit to the current position and resets the position to 0.AionBuffer
get(byte[] dst)
Populates the given dst buffer with the next bytes in the buffer and advances the position.java.math.BigInteger
get32ByteInt()
Returns the next 32-byte signed BigInteger in the buffer and advances the position.Address
getAddress()
Returns the next 32-byte Aion address in the buffer and advances the position.byte[]
getArray()
Allows access to the byte array under the buffer.boolean
getBoolean()
Returns the next boolean in the buffer and advances the position.byte
getByte()
Returns the next byte in the buffer and advances the position.int
getCapacity()
char
getChar()
Returns the next char in the buffer and advances the position.double
getDouble()
Returns the next double in the buffer and advances the position.float
getFloat()
Returns the next float in the buffer and advances the position.int
getInt()
Returns the next int in the buffer and advances the position.int
getLimit()
long
getLong()
Returns the next long in the buffer and advances the position.int
getPosition()
short
getShort()
Returns the next short in the buffer and advances the position.int
hashCode()
AionBuffer
put(byte[] src)
Copies the bytes from src into the buffer and advances the position.AionBuffer
put32ByteInt(java.math.BigInteger value)
Stores a 32-byte signed BigInteger into the buffer and advances the position.AionBuffer
putAddress(Address value)
Stores an Aion address into the buffer and advances the position.AionBuffer
putBoolean(boolean flag)
Stores a boolean into the buffer and advances the position.AionBuffer
putByte(byte b)
Stores a byte into the buffer and advances the position.AionBuffer
putChar(char value)
Stores a char into the buffer and advances the position.AionBuffer
putDouble(double value)
Stores a double into the buffer and advances the position.AionBuffer
putFloat(float value)
Stores a float into the buffer and advances the position.AionBuffer
putInt(int value)
Stores a int into the buffer and advances the position.AionBuffer
putLong(long value)
Stores a long into the buffer and advances the position.AionBuffer
putShort(short value)
Stores a short into the buffer and advances the position.AionBuffer
rewind()
Sets the position back to 0.java.lang.String
toString()
static AionBuffer
wrap(byte[] array)
Creates a new AionBuffer instance wrapping the given byte array.
-
-
-
Method Detail
-
allocate
public static AionBuffer allocate(int capacity)
Creates a new AionBuffer instance with the given capacity.- Parameters:
capacity
- The size of the underlying buffer to create, in bytes.- Returns:
- The new AionBuffer instance.
-
wrap
public static AionBuffer wrap(byte[] array)
Creates a new AionBuffer instance wrapping the given byte array.- Parameters:
array
- The array to wrap.- Returns:
- The new AionBuffer instance.
-
get
public AionBuffer get(byte[] dst)
Populates the given dst buffer with the next bytes in the buffer and advances the position. Note that dst MUST not be larger than the rest of the bytes in the receiver.- Parameters:
dst
- The byte array to populate with the contents of the receiver.- Returns:
- The receiver (for call chaining).
-
getBoolean
public boolean getBoolean()
Returns the next boolean in the buffer and advances the position. Note that we store booleans as a 1-byte quantity (0x1 or 0x0).- Returns:
- The underlying byte, interpreted as a boolean (0x1 is true).
-
getByte
public byte getByte()
Returns the next byte in the buffer and advances the position.- Returns:
- The byte.
-
getChar
public char getChar()
Returns the next char in the buffer and advances the position.- Returns:
- The char.
-
getDouble
public double getDouble()
Returns the next double in the buffer and advances the position.- Returns:
- The double.
-
getFloat
public float getFloat()
Returns the next float in the buffer and advances the position.- Returns:
- The float.
-
getInt
public int getInt()
Returns the next int in the buffer and advances the position.- Returns:
- The int.
-
getLong
public long getLong()
Returns the next long in the buffer and advances the position.- Returns:
- The long.
-
getShort
public short getShort()
Returns the next short in the buffer and advances the position.- Returns:
- The short.
-
getAddress
public Address getAddress()
Returns the next 32-byte Aion address in the buffer and advances the position.- Returns:
- The address.
-
get32ByteInt
public java.math.BigInteger get32ByteInt()
Returns the next 32-byte signed BigInteger in the buffer and advances the position.- Returns:
- The BigInteger.
-
put
public AionBuffer put(byte[] src)
Copies the bytes from src into the buffer and advances the position. Note that src MUST not be larger than the rest of the bytes in the receiver.- Parameters:
src
- The bytes to copy.- Returns:
- The receiver (for call chaining).
-
putBoolean
public AionBuffer putBoolean(boolean flag)
Stores a boolean into the buffer and advances the position. Note that we store booleans as a 1-byte quantity (0x1 or 0x0).- Parameters:
flag
- The boolean to store as a byte (0x1 for true, 0x0 for false).- Returns:
- The receiver (for call chaining).
-
putByte
public AionBuffer putByte(byte b)
Stores a byte into the buffer and advances the position.- Parameters:
b
- The byte to write.- Returns:
- The receiver (for call chaining).
-
putChar
public AionBuffer putChar(char value)
Stores a char into the buffer and advances the position.- Parameters:
value
- The char to write.- Returns:
- The receiver (for call chaining).
-
putDouble
public AionBuffer putDouble(double value)
Stores a double into the buffer and advances the position.- Parameters:
value
- The double to write.- Returns:
- The receiver (for call chaining).
-
putFloat
public AionBuffer putFloat(float value)
Stores a float into the buffer and advances the position.- Parameters:
value
- The float to write.- Returns:
- The receiver (for call chaining).
-
putInt
public AionBuffer putInt(int value)
Stores a int into the buffer and advances the position.- Parameters:
value
- The int to write.- Returns:
- The receiver (for call chaining).
-
putLong
public AionBuffer putLong(long value)
Stores a long into the buffer and advances the position.- Parameters:
value
- The long to write.- Returns:
- The receiver (for call chaining).
-
putShort
public AionBuffer putShort(short value)
Stores a short into the buffer and advances the position.- Parameters:
value
- The short to write.- Returns:
- The receiver (for call chaining).
-
putAddress
public AionBuffer putAddress(Address value)
Stores an Aion address into the buffer and advances the position.- Parameters:
value
- The address to write.- Returns:
- The receiver (for call chaining).
-
put32ByteInt
public AionBuffer put32ByteInt(java.math.BigInteger value)
Stores a 32-byte signed BigInteger into the buffer and advances the position. Note that this BigInteger is always stored as 32 bytes, even if its internal representation may be smaller.- Parameters:
value
- The BigInteger to write.- Returns:
- The receiver (for call chaining).
-
getArray
public byte[] getArray()
Allows access to the byte array under the buffer. Note that this will be a shared instance so changes to one will be observable in the other.- Returns:
- The byte array underneath the receiver.
-
getCapacity
public int getCapacity()
- Returns:
- The total capacity of the receiver.
-
getPosition
public int getPosition()
- Returns:
- The offset into the underlying byte array which the receiver will read/write its next byte.
-
getLimit
public int getLimit()
- Returns:
- The end of the buffer which will currently be used by a read/write operation.
-
clear
public AionBuffer clear()
Resets the position to 0 and the limit to the full capacity of the buffer. Used when discarding state associated with a previous use of the buffer.- Returns:
- The receiver (for call chaining).
-
flip
public AionBuffer flip()
Sets the limit to the current position and resets the position to 0. Primarily used when switching between writing and reading modes: write(X), write(Y), write(Z), flip(), read(X), read(Y), read(Z).- Returns:
- The receiver (for call chaining).
-
rewind
public AionBuffer rewind()
Sets the position back to 0. Useful for cases where the previously processed contents want to be reprocessed.- Returns:
- The receiver (for call chaining).
-
equals
public boolean equals(java.lang.Object ob)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-