public class ByteBufferInput extends Input
Modifier and Type | Field and Description |
---|---|
protected static ByteOrder |
nativeOrder |
protected ByteBuffer |
niobuffer |
protected boolean |
varIntsEnabled |
Constructor and Description |
---|
ByteBufferInput()
Creates an uninitialized Input.
|
ByteBufferInput(byte[] buffer) |
ByteBufferInput(ByteBuffer buffer)
Creates a new Input for reading from a ByteBuffer.
|
ByteBufferInput(InputStream inputStream)
Creates a new Input for reading from an InputStream with a buffer size of 4096.
|
ByteBufferInput(InputStream inputStream,
int bufferSize)
Creates a new Input for reading from an InputStream.
|
ByteBufferInput(int bufferSize)
Creates a new Input for reading from a byte array.
|
ByteBufferInput(long address,
int size)
This constructor allows for creation of a direct ByteBuffer of a given size at a given address.
|
Modifier and Type | Method and Description |
---|---|
boolean |
canReadInt()
Returns true if enough bytes are available to read an int with
readInt(boolean) . |
boolean |
canReadLong()
Returns true if enough bytes are available to read a long with
readLong(boolean) . |
void |
close()
Closes the underlying InputStream, if any.
|
protected int |
fill(ByteBuffer buffer,
int offset,
int count)
Fills the buffer with more bytes.
|
ByteBuffer |
getByteBuffer() |
InputStream |
getInputStream() |
boolean |
getVarIntsEnabled()
Return current setting for variable length encoding of integers
|
ByteOrder |
order() |
void |
order(ByteOrder byteOrder) |
int |
read()
Reads a single byte as an int from 0 to 255, or -1 if there are no more bytes are available.
|
int |
read(byte[] bytes)
Reads bytes.length bytes or less and writes them to the specified byte[], starting at 0, and returns the number of bytes
read.
|
int |
read(byte[] bytes,
int offset,
int count)
Reads count bytes or less and writes them to the specified byte[], starting at offset, and returns the number of bytes read
or -1 if no more bytes are available.
|
boolean |
readBoolean()
Reads a 1 byte boolean.
|
byte |
readByte()
Reads a single byte.
|
void |
readBytes(byte[] bytes)
Reads bytes.length bytes and writes them to the specified byte[], starting at index 0.
|
void |
readBytes(byte[] bytes,
int offset,
int count)
Reads count bytes and writes them to the specified byte[], starting at offset.
|
byte[] |
readBytes(int length)
Reads the specified number of bytes into a new byte[].
|
int |
readByteUnsigned()
Reads a byte as an int from 0 to 255.
|
char |
readChar()
Reads a 2 byte char.
|
char[] |
readChars(int length)
Bulk input of a char array.
|
double |
readDouble()
Reads an 8 bytes double.
|
double |
readDouble(double precision,
boolean optimizePositive)
Reads a 1-9 byte double with reduced precision.
|
double[] |
readDoubles(int length)
Bulk input of a double array.
|
float |
readFloat()
Reads a 4 byte float.
|
float |
readFloat(float precision,
boolean optimizePositive)
Reads a 1-5 byte float with reduced precision.
|
float[] |
readFloats(int length)
Bulk input of a float array.
|
int |
readInt()
Reads a 4 byte int.
|
int |
readInt(boolean optimizePositive)
Reads a 1-5 byte int.
|
int[] |
readInts(int length)
Bulk input of an int array.
|
long |
readLong()
Reads an 8 byte long.
|
long |
readLong(boolean optimizePositive)
Reads a 1-9 byte long.
|
long[] |
readLongs(int length)
Bulk input of a long array.
|
short |
readShort()
Reads a 2 byte short.
|
short[] |
readShorts(int length)
Bulk input of a short array.
|
int |
readShortUnsigned()
Reads a 2 byte short as an int from 0 to 65535.
|
String |
readString()
Reads the length and string of UTF8 characters, or null.
|
StringBuilder |
readStringBuilder()
Reads the length and string of UTF8 characters, or null.
|
int |
readVarInt(boolean optimizePositive)
Reads a 1-5 byte int.
|
long |
readVarLong(boolean optimizePositive)
Reads a 1-9 byte long.
|
void |
release()
Releases a direct buffer.
|
protected int |
require(int required) |
void |
rewind()
Sets the position and total to zero.
|
void |
setBuffer(byte[] bytes)
Sets a new buffer, discarding any previous buffer.
|
void |
setBuffer(ByteBuffer buffer)
Sets a new buffer, discarding any previous buffer.
|
void |
setInputStream(InputStream inputStream)
Sets a new InputStream.
|
void |
setLimit(int limit)
Sets the limit in the buffer.
|
void |
setPosition(int position)
Sets the current position in the buffer.
|
void |
setVarIntsEnabled(boolean varIntsEnabled)
Controls if a variable length encoding for integer types should be used when serializers suggest it.
|
void |
skip(int count)
Discards the specified number of bytes.
|
long |
skip(long count)
Discards the specified number of bytes.
|
available, eof, fill, getBuffer, limit, position, readInts, readLongs, setBuffer, setTotal, total
mark, markSupported, reset
protected ByteBuffer niobuffer
protected boolean varIntsEnabled
protected static final ByteOrder nativeOrder
public ByteBufferInput()
setBuffer(ByteBuffer)
public ByteBufferInput(int bufferSize)
bufferSize
- The size of the buffer. An exception is thrown if more bytes than this are read.public ByteBufferInput(byte[] buffer)
public ByteBufferInput(ByteBuffer buffer)
public ByteBufferInput(InputStream inputStream)
public ByteBufferInput(InputStream inputStream, int bufferSize)
public ByteBufferInput(long address, int size)
Typical usage could look like this snippet:
// Explicitly allocate memory long bufAddress = UnsafeUtil.unsafe().allocateMemory(4096); // Create a ByteBufferInput using the allocated memory region ByteBufferInput buffer = new ByteBufferInput(bufAddress, 4096); // Do some operations on this buffer here // Say that ByteBuffer won't be used anymore buffer.release(); // Release the allocated region UnsafeUtil.unsafe().freeMemory(bufAddress);
address
- starting address of a memory region pre-allocated using Unsafe.allocateMemory()public ByteOrder order()
public void order(ByteOrder byteOrder)
public void setBuffer(byte[] bytes)
public void setBuffer(ByteBuffer buffer)
InputStream
is set to null.public void release()
setBuffer(ByteBuffer)
must be called before any write operations can be performed.public ByteBuffer getByteBuffer()
public InputStream getInputStream()
getInputStream
in class Input
public void setInputStream(InputStream inputStream)
setInputStream
in class Input
inputStream
- May be null.public void rewind()
Input
protected int fill(ByteBuffer buffer, int offset, int count) throws KryoException
KryoException
protected final int require(int required) throws KryoException
require
in class Input
required
- Must be > 0. The buffer is filled until it has at least this many bytes.KryoException
- if EOS is reached before required bytes are read (buffer underflow).public int read() throws KryoException
read
in class Input
KryoException
public int read(byte[] bytes) throws KryoException
read
in class Input
KryoException
public int read(byte[] bytes, int offset, int count) throws KryoException
read
in class Input
KryoException
public void setPosition(int position)
Input
setPosition
in class Input
public void setLimit(int limit)
public void skip(int count) throws KryoException
Input
skip
in class Input
KryoException
public long skip(long count) throws KryoException
skip
in class Input
KryoException
public void close() throws KryoException
close
in interface Closeable
close
in interface AutoCloseable
close
in class Input
KryoException
public byte readByte() throws KryoException
readByte
in class Input
KryoException
public int readByteUnsigned() throws KryoException
readByteUnsigned
in class Input
KryoException
public byte[] readBytes(int length) throws KryoException
readBytes
in class Input
KryoException
public void readBytes(byte[] bytes) throws KryoException
readBytes
in class Input
KryoException
public void readBytes(byte[] bytes, int offset, int count) throws KryoException
readBytes
in class Input
KryoException
public int readInt() throws KryoException
Input
readInt
in class Input
KryoException
public int readInt(boolean optimizePositive) throws KryoException
Input
readInt
in class Input
KryoException
public int readVarInt(boolean optimizePositive) throws KryoException
Input
readVarInt
in class Input
KryoException
public boolean canReadInt() throws KryoException
readInt(boolean)
.canReadInt
in class Input
KryoException
public boolean canReadLong() throws KryoException
readLong(boolean)
.canReadLong
in class Input
KryoException
public String readString()
Output.writeString(String)
, Output.writeString(CharSequence)
, and Output.writeAscii(String)
.readString
in class Input
public StringBuilder readStringBuilder()
Output.writeString(String)
, Output.writeString(CharSequence)
, and Output.writeAscii(String)
.readStringBuilder
in class Input
public float readFloat() throws KryoException
readFloat
in class Input
KryoException
public float readFloat(float precision, boolean optimizePositive) throws KryoException
readFloat
in class Input
KryoException
public short readShort() throws KryoException
readShort
in class Input
KryoException
public int readShortUnsigned() throws KryoException
readShortUnsigned
in class Input
KryoException
public long readLong() throws KryoException
readLong
in class Input
KryoException
public long readLong(boolean optimizePositive) throws KryoException
readLong
in class Input
KryoException
public long readVarLong(boolean optimizePositive) throws KryoException
readVarLong
in class Input
KryoException
public boolean readBoolean() throws KryoException
readBoolean
in class Input
KryoException
public char readChar() throws KryoException
readChar
in class Input
KryoException
public double readDouble() throws KryoException
readDouble
in class Input
KryoException
public double readDouble(double precision, boolean optimizePositive) throws KryoException
readDouble
in class Input
KryoException
public int[] readInts(int length) throws KryoException
readInts
in class Input
KryoException
public long[] readLongs(int length) throws KryoException
readLongs
in class Input
KryoException
public float[] readFloats(int length) throws KryoException
readFloats
in class Input
KryoException
public short[] readShorts(int length) throws KryoException
readShorts
in class Input
KryoException
public char[] readChars(int length) throws KryoException
readChars
in class Input
KryoException
public double[] readDoubles(int length) throws KryoException
readDoubles
in class Input
KryoException
public boolean getVarIntsEnabled()
public void setVarIntsEnabled(boolean varIntsEnabled)
varIntsEnabled
- Copyright © 2018. All rights reserved.