Files
flatbuffers-bigfoot/java/com/google/flatbuffers/ReadWriteBuf.java
Paulo Pinheiro cd88e6b2aa [Java][FlexBuffers] Abstract buffer access from ByteBuffer (#5743)
To read and build flexbuffers on Java, one needs to wrap the data
using  ByteBuffer. But for the common case of having ByteBuffers
backed by arrays, accessing from a ByteBuffer might be inefficient.

So this change introduces two interfaces: ReadBuf and ReadWriteBuf.
It allows one to read and writes data directly on an array. It also allow
 other buffer implementations to be used with flexbuffers.

Another change is that FlexBuffersBuilder backed by array allows
the buffer to grow with the increase of the message size. Something
that could not be done with ByteBuffer.
2020-02-21 11:46:40 -08:00

136 lines
3.8 KiB
Java

package com.google.flatbuffers;
/**
* Interface to represent a read-write buffer. This interface will be used to access and write
* FlexBuffers message.
*/
interface ReadWriteBuf extends ReadBuf {
/**
* Put a boolean into the buffer at {@code writePosition()} . Booleans as stored as single
* byte. Write position will be incremented.
* @return boolean element
*/
void putBoolean(boolean value);
/**
* Put an array of bytes into the buffer at {@code writePosition()}. Write position will be
* incremented.
* @param value the data to be copied
* @param start initial position on value to be copied
* @param length amount of bytes to be copied
*/
void put (byte[] value, int start, int length);
/**
* Write a byte into the buffer at {@code writePosition()}. Write position will be
* incremented.
*/
void put(byte value);
/**
* Write a 16-bit into in the buffer at {@code writePosition()}. Write position will be
* incremented.
*/
void putShort(short value);
/**
* Write a 32-bit into in the buffer at {@code writePosition()}. Write position will be
* incremented.
*/
void putInt(int value);
/**
* Write a 64-bit into in the buffer at {@code writePosition()}. Write position will be
* incremented.
*/
void putLong(long value);
/**
* Write a 32-bit float into the buffer at {@code writePosition()}. Write position will be
* incremented.
*/
void putFloat(float value);
/**
* Write a 64-bit float into the buffer at {@code writePosition()}. Write position will be
* incremented.
*/
void putDouble(double value);
/**
* Write boolean into a given position on the buffer. Booleans as stored as single byte.
* @param index position of the element in buffer
*/
void setBoolean(int index, boolean value);
/**
* Read a byte from data.
* @param index position of the element in the buffer
* @return a byte
*/
void set(int index, byte value);
/**
* Write an array of bytes into the buffer.
* @param index initial position of the buffer to be written
* @param value the data to be copied
* @param start initial position on value to be copied
* @param length amount of bytes to be copied
*/
void set(int index, byte[] value, int start, int length);
/**
* Read a short from data.
* @param index position of the element in ReadBuf
* @return a short
*/
void setShort(int index, short value);
/**
* Read a 32-bit int from data.
* @param index position of the element in ReadBuf
* @return an int
*/
void setInt(int index, int value);
/**
* Read a 64-bit long from data.
* @param index position of the element in ReadBuf
* @return a long
*/
void setLong(int index, long value);
/**
* Read a 32-bit float from data.
* @param index position of the element in ReadBuf
* @return a float
*/
void setFloat(int index, float value);
/**
* Read a 64-bit float from data.
* @param index position of the element in ReadBuf
* @return a double
*/
void setDouble(int index, double value);
int writePosition();
/**
* Defines the size of the message in the buffer. It also determines last position that buffer
* can be read or write. Last byte to be accessed is in position {@code limit() -1}.
* @return indicate last position
*/
int limit();
/**
* Request capacity of the buffer. In case buffer is already larger
* than the requested, this method will just return true. Otherwise
* It might try to resize the buffer.
*
* @return true if buffer is able to offer
* the requested capacity
*/
boolean requestCapacity(int capacity);
}