U
- Underlying typepublic interface Bytes<U> extends BytesStore<Bytes<U>,U>, BytesIn<U>, BytesOut<U>, net.openhft.chronicle.core.io.SingleThreadedChecked
BytesStore
with 63-bit addressing capability (~8 EiB) and separate read and write cursors.
Java's built-in ByteBuffer
can only handle 31-bits addresses and has only a single cursor.
A Bytes object can be for a fixed region of memory or an "elastic" buffer which can be resized on demand.
A Bytes is mutable and not thread-safe.
The cursors consist of a write-position and read-position which must follow these constraints
start() <= readPosition() <= writePosition() <= writeLimit() <= capacity()
Also readLimit() == writePosition() && readPosition() <= safeLimit()
Generally, a Bytes object is a resource that is ReferenceCounted and certain operations invoked on a Bytes
object that has been released, might result in an IllegalStateException
being thrown.
BytesStore
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_BYTE_BUFFER_CAPACITY
The default initial size of an elastic Bytes backed by a ByteBuffer
|
static long |
MAX_CAPACITY
The max capacity a Bytes can ever have.
|
static int |
MAX_HEAP_CAPACITY
The max capacity a Bytes can ever have if it is allocated on heap.
|
charToString
JAVA9_STRING_CODER_LATIN, JAVA9_STRING_CODER_UTF16
Modifier and Type | Method and Description |
---|---|
static @NotNull VanillaBytes |
allocateDirect(byte[] bytes)
Creates and returns a new fix sized wrapper for native (64-bit address)
memory with the contents copied from the given
bytes array. |
static @NotNull VanillaBytes<Void> |
allocateDirect(long capacity)
Creates and returns a new fix sized wrapper for native (64-bit address)
memory with the provided
capacity . |
static @NotNull NativeBytes<Void> |
allocateElasticDirect()
Creates and returns a new elastic wrapper for native (64-bit address)
memory with zero initial capacity which will be resized as required.
|
static @NotNull NativeBytes<Void> |
allocateElasticDirect(long initialCapacity)
Creates and returns a new elastic wrapper for native (64-bit address)
memory with the given
initialCapacity which will be resized as required. |
static @NotNull OnHeapBytes |
allocateElasticOnHeap()
Creates and returns a new elastic wrapper for on heap memory with the
initialCapacity 32 bytes which will be resized as required. |
static @NotNull OnHeapBytes |
allocateElasticOnHeap(int initialCapacity)
Creates and returns a new elastic wrapper for on heap memory with the provided
initialCapacity which will be resized as required. |
default @NotNull Bytes<U> |
bytesForRead()
Creates and returns a new slice of this Bytes object whereby the start is moved to the readPosition and the
current limit determines the capacity.
|
@Nullable BytesStore |
bytesStore()
Returns the backing ByteStore this Bytes object wraps, or null.
|
@NotNull Bytes<U> |
clear()
Set the readPosition= writePosition = start, writeLimit = capacity
|
@NotNull Bytes<U> |
compact()
Compact these Bytes by moving the readPosition to the start.
|
BytesStore<Bytes<U>,U> |
copy()
Creates and returns a new copy of this Bytes object from
RandomCommon.readPosition()
to RandomCommon.readLimit() . |
default long |
copyTo(@NotNull BytesStore targetByteStore)
Copies bytes from this Bytes object to another provided
targetByteStore . |
default void |
copyTo(@NotNull OutputStream outputStream)
Copies bytes from this Bytes object to the provided
outputStream . |
static @NotNull Bytes<byte[]> |
directFrom(@NotNull String text)
Creates and returns a new ISO-8859-1 coded Bytes object from the
text . |
static @NotNull Bytes<ByteBuffer> |
elasticByteBuffer()
Creates and returns a new elastic wrapper for a direct (off-heap) ByteBuffer with a default capacity
which will be resized as required.
|
static @NotNull Bytes<ByteBuffer> |
elasticByteBuffer(int initialCapacity)
Creates and returns a new elastic wrapper for a direct (off-heap) ByteBuffer with
the given
initialCapacity which will be resized as required. |
static @NotNull Bytes<ByteBuffer> |
elasticByteBuffer(int initialCapacity,
int maxCapacity)
Creates and returns a new elastic wrapper for a direct (off-heap) ByteBuffer with
the given
initialCapacity which will be resized as required up
to the given maxSize . |
static @NotNull Bytes<ByteBuffer> |
elasticHeapByteBuffer()
Creates and returns a new elastic wrapper for a heap ByteBuffer with
the
initialCapacity 128 bytes which will be resized as required. |
static @NotNull Bytes<ByteBuffer> |
elasticHeapByteBuffer(int initialCapacity)
Creates and returns a new elastic wrapper for a heap ByteBuffer with
the given
initialCapacity which will be resized as required. |
static Bytes<?> |
empty() |
default void |
ensureCapacity(long desiredCapacity)
Grows the buffer if the buffer is elastic, if the buffer is not elastic and there is not
enough capacity then this method will throw an
DecoratedBufferOverflowException |
static <T> Bytes<T> |
forFieldGroup(T object,
@NotNull String groupName)
Creates and returns a new Bytes view of fields in the provided
object in a FieldGroup named as
the provided groupName effectively turning the groups of fields into a memory segment. |
static @NotNull Bytes<byte[]> |
from(@NotNull CharSequence text)
Creates and returns a new ISO-8859-1 coded Bytes object from the
text . |
static @NotNull Bytes<byte[]> |
from(@NotNull String text)
Creates and returns a new ISO-8859-1 coded Bytes object from the
text . |
static Bytes<Void> |
fromDirect(@NotNull CharSequence text)
Creates and returns a new ISO-8859-1 coded Bytes object from the
text . |
static @NotNull Bytes<?> |
fromHexString(@NotNull String s) |
default long |
indexOf(@NotNull Bytes source)
Returns the lowest index value for which the contents of this Bytes object equals the provided
source ,
or -1 if no such index value exists. |
default int |
indexOf(@NotNull BytesStore source,
int fromIndex)
Returns the lowest index value starting from the provided
fromIndex for which the contents of this
Bytes object equals the provided source , or -1 if no such index value exists. |
default boolean |
isClear()
Returns if the
readPosition is at the start and
the writeLimit is at the end . |
boolean |
isElastic()
Returns if this Bytes object is elastic.
|
default boolean |
isEqual(@Nullable String other)
Returns if this Bytes object contents equals the provided
other . |
default @NotNull BigDecimal |
readBigDecimal()
Creates and returns a new BigDecimal representing the contents of this Bytes object.
|
default @NotNull BigInteger |
readBigInteger()
Creates and returns a new BigInteger representing the contents of this Bytes object or
BigInteger.ZERO
if this Bytes object is empty. |
default <T extends ReadBytesMarshallable> |
readMarshallableLength16(@NotNull Class<T> clazz,
T using)
Reads the content of this Bytes object converting it to an object of the provided
clazz using the
provided using , or if null is provided for using , creates a new object, eventually
returning whichever object was updated. |
default void |
readWithLength(long length,
@NotNull BytesOut<U> bytesOut)
Writes the content of this Bytes object into the provided
bytesOut writing the provided
length bytes. |
default boolean |
readWrite()
Returns if this ByteStore can be both read from and written to.
|
default long |
realCapacity()
Returns the actual capacity that can be potentially read.
|
default long |
safeLimit() |
default boolean |
sharedMemory()
Returns if this Bytes object is using memory that can be shared across processes.
|
default @NotNull String |
toHexString()
Creates and returns a new String representing the contents of this Bytes object in hexadecimal form encoding
at most 1024 bytes.
|
default @NotNull String |
toHexString(long maxLength)
Creates and returns a new String representing the contents of this Bytes object in hexadecimal form encoding
at most the provided
maxLength bytes. |
default @NotNull String |
toHexString(long offset,
long maxLength)
Creates and returns a new String representing the contents of this Bytes object in hexadecimal form
starting at the provided
offset encoding at most the provided maxLength bytes. |
static @NotNull String |
toString(@NotNull Bytes<?> buffer)
Creates and returns a String extracted from the provided
buffer
staring from the provided readPosition to the
provided readLimit . |
static @NotNull String |
toString(@NotNull Bytes<?> buffer,
long maxLen)
Creates and returns a string from the
readPosition to the readLimit with a specified maximum length, The buffer is not modified
by this call. |
static @NotNull String |
toString(@NotNull Bytes<?> buffer,
long position,
long length)
Creates and returns a String from the bytes of the provided
buffer with the provided length
staring from the provided offset . |
default boolean |
unchecked()
Returns if this Bytes object is unchecked.
|
default @NotNull Bytes<U> |
unchecked(boolean unchecked)
Creates and returns a new Bytes which is optionally unchecked as indicated by the provided
unchecked . |
default void |
unwrite(long fromOffset,
int count)
Will un-write a specified number of bytes from an offset from this Bytes object.
|
static Bytes<byte[]> |
valueOf(String text) |
static @NotNull Bytes<byte[]> |
wrapForRead(byte[] byteArray)
Creates and returns a new Bytes wrapping the provided
byteArray . |
static @NotNull Bytes<ByteBuffer> |
wrapForRead(@NotNull ByteBuffer byteBuffer)
Creates and returns a new Bytes wrapping the provided
byteBuffer . |
static @NotNull Bytes<byte[]> |
wrapForWrite(byte[] byteArray)
Creates and returns a new Bytes wrapping the provided
byteArray . |
static @NotNull Bytes<ByteBuffer> |
wrapForWrite(@NotNull ByteBuffer byteBuffer)
Creates and returns a new Bytes wrapping the provided
byteBuffer . |
default Bytes |
write(@NotNull InputStream inputStream)
Writes the contents of the provided `inputStream` to the contents of this Bytes object.
|
default void |
writeMarshallableLength16(@NotNull WriteBytesMarshallable marshallable)
Writes the content of the provided
marshallable to this Bytes object converting from an object
to a number of bytes. |
addAndGetDouble, addAndGetDoubleNotAtomic, addAndGetFloat, addAndGetFloatNotAtomic, addAndGetInt, addAndGetIntNotAtomic, addAndGetLong, addAndGetShortNotAtomic, addAndGetUnsignedByteNotAtomic, byteCheckSum, byteCheckSum, bytesForWrite, capacity, charAt, cipher, cipher, compareAndSwapDouble, compareAndSwapFloat, compareAndSwapInt, compareAndSwapLong, contentEquals, elasticByteBuffer, endsWith, equalBytes, forFields, from, hash, inside, inside, isDirectMemory, isEmpty, isEqual, isImmutableEmptyByteStore, lazyNativeBytesStoreWithFixedCapacity, length, move, nativePointer, nativeStore, nativeStoreFrom, nativeStoreWithFixedCapacity, startsWith, startsWith, subSequence, to8bitString, toDebugString, toDebugString, underlyingObject, wrap, wrap, wrap, writeMaxInt, writeMaxLong, zeroOut
append, append, nativeWrite, testAndSetInt, write, write, write, write, write, write8bit, write8bit, writeBoolean, writeByte, writeByte, writeDouble, writeFloat, writeInt, writeInt24, writeLong, writeOrderedDouble, writeOrderedFloat, writeOrderedInt, writeOrderedLong, writeShort, writeUnsignedByte, writeUnsignedInt, writeUnsignedShort, writeUtf8, writeUtf8Limited, writeVolatileByte, writeVolatileDouble, writeVolatileFloat, writeVolatileInt, writeVolatileLong, writeVolatileShort
chars, codePoints, toString
bytesMethodReader, bytesMethodReaderBuilder, readObject
canReadDirect, canReadDirect, compareUtf8, copyTo, copyTo, createCharToString, fastHash, findByte, nativeRead, parseLong, peekUnsignedByte, peekVolatileInt, printable, read, readBoolean, readByte, readDouble, readFloat, readIncompleteLong, readInt, readLong, readShort, readUnsignedByte, readUnsignedInt, readUnsignedInt24, readUnsignedShort, readUtf8, readUtf8Limited, readUtf8Limited, readVolatileByte, readVolatileDouble, readVolatileFloat, readVolatileInt, readVolatileLong, readVolatileShort, subBytes, testAndSetInt, toByteArray, toTemporaryDirectByteBuffer
lastDecimalPlaces, lastDecimalPlaces, lastNumberHadDigits, lastNumberHadDigits, parse8bit, parse8bit, parse8bit, parse8bit, parse8bit, parseBigDecimal, parseBoolean, parseBoolean, parseDouble, parseFlexibleLong, parseFloat, parseInt, parseLong, parseLongDecimal, parseUtf8, parseUtf8, parseUtf8, reader, skipTo
inputStream, lenient, lenient, parseHexLong, parseUtf8, parseUtf8, peekUnsignedByte, rawReadByte, rawReadInt, rawReadLong, read, read, read, read, read, read, read8bit, read8bit, read8bit, readBoolean, readByte, readChar, readDouble, readEnum, readFloat, readHistogram, readIncompleteLong, readInt, readInt24, readLimit, readLimitToCapacity, readLong, readPosition, readPositionForHeader, readPositionRemaining, readPositionUnlimited, readShort, readSkip, readStopBit, readStopBitChar, readStopBitDecimal, readStopBitDouble, readUnsignedByte, readUnsignedInt, readUnsignedInt24, readUnsignedShort, readUtf8, readUtf8, readUtf8, readUtf8, readVolatileInt, readVolatileLong, readWithLength, readWithLength, readWithLength0, uncheckedReadSkipBackOne, uncheckedReadSkipOne, uncheckedReadUnsignedByte, unsafeRead, unsafeReadObject, unsafeReadObject
bytesMethodWriter, writeObject
append, append, append, append, append, append, append, append, append, append, append8bit, append8bit, append8bit, append8bit, append8bit, appendBase, appendBase16, appendBase16, appendDateMillis, appendDecimal, appendTimeMillis, writer
appendUtf8, appendUtf8, appendUtf8, appendUtf8, appendUtf8, appendUtf8, canWriteDirect, copyFrom, outputStream, rawWriteByte, rawWriteInt, rawWriteLong, unsafeWrite, unsafeWriteObject, unsafeWriteObject, write, write, write, write, write, write, write, write, write8bit, write8bit, write8bit, write8bit, writeBigDecimal, writeBigInteger, writeBoolean, writeByte, writeChar, writeDouble, writeDoubleAndInt, writeEnum, writeFloat, writeHistogram, writeInt, writeInt24, writeIntAdv, writeLimit, writeLong, writeLongAdv, writeOrderedInt, writeOrderedLong, writePosition, writePositionForHeader, writePositionRemaining, writeShort, writeSkip, writeSome, writeSome, writeStopBit, writeStopBit, writeStopBit, writeStopBitDecimal, writeUnsignedByte, writeUnsignedInt, writeUnsignedInt24, writeUnsignedShort, writeUtf8, writeUtf8, writeWithLength
clearAndPad, prepend, prewrite, prewrite, prewriteByte, prewriteInt, prewriteLong, prewriteShort
comment, indent, retainsComments
static final long MAX_CAPACITY
static final int MAX_HEAP_CAPACITY
static final int DEFAULT_BYTE_BUFFER_CAPACITY
@NotNull static @NotNull Bytes<ByteBuffer> elasticByteBuffer()
@NotNull static @NotNull Bytes<ByteBuffer> elasticByteBuffer(int initialCapacity)
initialCapacity
which will be resized as required.initialCapacity
- the initial non-negative capacity given in bytesIllegalArgumentException
- if the provided initialCapacity
negative.@NotNull static @NotNull Bytes<ByteBuffer> elasticByteBuffer(int initialCapacity, int maxCapacity)
initialCapacity
which will be resized as required up
to the given maxSize
.initialCapacity
- the initial non-negative capacity given in bytesmaxCapacity
- the max capacity given in bytes equal or greater than initialCapacityIllegalArgumentException
- if the provided initialCapacity
or provided maxCapacity
is negative.@NotNull static @NotNull Bytes<ByteBuffer> elasticHeapByteBuffer(int initialCapacity)
initialCapacity
which will be resized as required.initialCapacity
- the initial non-negative capacity given in bytesIllegalArgumentException
- if the provided initialCapacity
is negative.IllegalStateException
- if the underlying resource has already been freed.@NotNull static @NotNull Bytes<ByteBuffer> elasticHeapByteBuffer()
initialCapacity
128 bytes which will be resized as required.static <T> Bytes<T> forFieldGroup(@NotNull T object, @NotNull @NotNull String groupName)
object
in a FieldGroup
named as
the provided groupName
effectively turning the groups of fields into a memory segment.
Here is an example of field groups:
static class Padding extends Parent {
{@literal @}FieldGroup("p")
// 128 bytes
transient long p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15;
}
NOTE: the length of the data has to be recorded at the start as an unsigned byte. This consumes one byte and limits it's maximum size to 255 usable bytes.
object
- non-null object whose fields are to be reflectedgroupName
- of the field group, non-nullNullPointerException
- if the provided object
or the provided groupName
is null
IllegalStateException
- if the underlying resource has already been freed.@NotNull static @NotNull Bytes<ByteBuffer> wrapForRead(@NotNull @NotNull ByteBuffer byteBuffer)
byteBuffer
.
The returned Bytes is ready for reading.
Method for convenience only - might not be ideal for performance (creates garbage). To avoid garbage, use something like this example:
import net.openhft.chronicle.bytes.Bytes;
import java.nio.ByteBuffer;
public class ChronicleBytesWithByteBufferExampleTest {
private static final String HELLO_WORLD = "hello world";
public static void main(String[] args)
throws InterruptedException {
//setup Bytes and ByteBuffer to write from
Bytes b = Bytes.elasticByteBuffer();
ByteBuffer toWriteFrom = ByteBuffer.allocate(HELLO_WORLD.length());
toWriteFrom.put(HELLO_WORLD.getBytes(), 0, HELLO_WORLD.length());
toWriteFrom.flip();
byte[] toReadTo = new byte[HELLO_WORLD.length()];
doWrite(b, toWriteFrom);
ByteBuffer byteBuffer = doRead(b);
//check result
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < HELLO_WORLD.length(); i++) {
sb.append((char) byteBuffer.get());
}
assert sb.toString().equals(HELLO_WORLD): "Failed - strings not equal!";
}
private static void doWrite(Bytes b, ByteBuffer toWrite) {
//no garbage when writing to Bytes from ByteBuffer
b.clear();
b.write(b.writePosition(), toWrite, toWrite.position(), toWrite.limit());
}
private static ByteBuffer doRead(Bytes b) {
//no garbage when getting the underlying ByteBuffer
assert b.underlyingObject() instanceof ByteBuffer;
ByteBuffer byteBuffer = (ByteBuffer) b.underlyingObject();
return byteBuffer;
}
}
byteBuffer
- to wrap, non-nullbyteBuffer
NullPointerException
- if the provided byteBuffer
is null
ReadOnlyBufferException
- if the provided byteBuffer
is read-only@NotNull static @NotNull Bytes<ByteBuffer> wrapForWrite(@NotNull @NotNull ByteBuffer byteBuffer)
byteBuffer
.
The returned Bytes is ready for writing.
Method for convenience only - might not be ideal for performance (creates garbage). To avoid garbage, use something like this example:
import net.openhft.chronicle.bytes.Bytes;
import java.nio.ByteBuffer;
public class ChronicleBytesWithByteBufferExampleTest {
private static final String HELLO_WORLD = "hello world";
public static void main(String[] args)
throws InterruptedException {
//setup Bytes and ByteBuffer to write from
Bytes b = Bytes.elasticByteBuffer();
ByteBuffer toWriteFrom = ByteBuffer.allocate(HELLO_WORLD.length());
toWriteFrom.put(HELLO_WORLD.getBytes(), 0, HELLO_WORLD.length());
toWriteFrom.flip();
byte[] toReadTo = new byte[HELLO_WORLD.length()];
doWrite(b, toWriteFrom);
ByteBuffer byteBuffer = doRead(b);
//check result
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < HELLO_WORLD.length(); i++) {
sb.append((char) byteBuffer.get());
}
assert sb.toString().equals(HELLO_WORLD): "Failed - strings not equal!";
}
private static void doWrite(Bytes b, ByteBuffer toWrite) {
//no garbage when writing to Bytes from ByteBuffer
b.clear();
b.write(b.writePosition(), toWrite, toWrite.position(), toWrite.limit());
}
private static ByteBuffer doRead(Bytes b) {
//no garbage when getting the underlying ByteBuffer
assert b.underlyingObject() instanceof ByteBuffer;
ByteBuffer byteBuffer = (ByteBuffer) b.underlyingObject();
return byteBuffer;
}
}
byteBuffer
- to wrap, non-nullbyteBuffer
NullPointerException
- if the provided byteBuffer
is null
ReadOnlyBufferException
- if the provided byteBuffer
is read-only@NotNull static @NotNull Bytes<byte[]> wrapForRead(byte[] byteArray)
byteArray
.
The returned Bytes is ready for reading.
Method for convenience only - might not be ideal for performance (creates garbage). To avoid garbage, use something like this example:
import net.openhft.chronicle.bytes.Bytes;
import java.nio.charset.Charset;
public class ChronicleBytesWithPrimByteArrayExampleTest {
private static final Charset ISO_8859 = Charset.forName("ISO-8859-1");
private static final String HELLO_WORLD = "hello world";
public static void main(String[] args) {
//setup Bytes and byte[]s to write from and read to
Bytes b = Bytes.elasticByteBuffer();
byte[] toWriteFrom = HELLO_WORLD.getBytes(ISO_8859);
byte[] toReadTo = new byte[HELLO_WORLD.length()];
doWrite(b, toWriteFrom);
doRead(b, toReadTo);
//check result
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < HELLO_WORLD.length(); i++) {
sb.append((char) toReadTo[i]);
}
assert sb.toString().equals(HELLO_WORLD): "Failed - strings not equal!";
}
private static void doWrite(Bytes b, byte[] toWrite) {
//no garbage when writing to Bytes from byte[]
b.clear();
b.write(toWrite);
}
private static void doRead(Bytes b, byte[] toReadTo) {
//no garbage when reading from Bytes into byte[]
b.read( toReadTo, 0, HELLO_WORLD.length());
}
}
byteArray
- to wrap, non-nullbyteArray
IllegalStateException
- if the provided byteArray
is null
@NotNull static @NotNull Bytes<byte[]> wrapForWrite(byte[] byteArray)
byteArray
.
The returned Bytes is ready for writing.
Method for convenience only - might not be ideal for performance (creates garbage). To avoid garbage, use something like this example:
import net.openhft.chronicle.bytes.Bytes;
import java.nio.charset.Charset;
public class ChronicleBytesWithPrimByteArrayExampleTest {
private static final Charset ISO_8859 = Charset.forName("ISO-8859-1");
private static final String HELLO_WORLD = "hello world";
public static void main(String[] args) {
//setup Bytes and byte[]s to write from and read to
Bytes b = Bytes.elasticByteBuffer();
byte[] toWriteFrom = HELLO_WORLD.getBytes(ISO_8859);
byte[] toReadTo = new byte[HELLO_WORLD.length()];
doWrite(b, toWriteFrom);
doRead(b, toReadTo);
//check result
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < HELLO_WORLD.length(); i++) {
sb.append((char) toReadTo[i]);
}
assert sb.toString().equals(HELLO_WORLD): "Failed - strings not equal!";
}
private static void doWrite(Bytes b, byte[] toWrite) {
//no garbage when writing to Bytes from byte[]
b.clear();
b.write(toWrite);
}
private static void doRead(Bytes b, byte[] toReadTo) {
//no garbage when reading from Bytes into byte[]
b.read( toReadTo, 0, HELLO_WORLD.length());
}
}
byteArray
- to wrap, non-nullbyteArray
NullPointerException
- if the provided byteArray
is null
@NotNull static @NotNull Bytes<byte[]> from(@NotNull @NotNull CharSequence text)
text
.
The returned Bytes is ready for reading.
The returned Bytes is allocated on the heap.
from
in interface BytesStore<Bytes<U>,U>
text
- non-null text to convertNullPointerException
- if the provided text
is null
static Bytes<Void> fromDirect(@NotNull @NotNull CharSequence text)
text
.
The returned Bytes is ready for reading.
The returned Bytes is allocated using native memory.
text
- non-null text to convertNullPointerException
- if the provided text
is null
@NotNull static @NotNull Bytes<byte[]> directFrom(@NotNull @NotNull String text)
text
.
The returned Bytes is ready for reading.
The returned Bytes is allocated using native memory.
text
- non-null text to convertNullPointerException
- if the provided text
is null
@NotNull static @NotNull Bytes<byte[]> from(@NotNull @NotNull String text)
text
.
The returned Bytes is ready for reading.
The returned Bytes is allocated on the heap.
from
in interface BytesStore<Bytes<U>,U>
text
- non-null text to convertNullPointerException
- if the provided text
is null
static Bytes<?> empty()
empty
in interface BytesStore<Bytes<U>,U>
@NotNull static @NotNull VanillaBytes<Void> allocateDirect(long capacity) throws IllegalArgumentException
capacity
.
capacity
- the non-negative capacity given in bytesIllegalArgumentException
- if the provided capacity
is negative.@NotNull static @NotNull NativeBytes<Void> allocateElasticDirect()
@NotNull static @NotNull NativeBytes<Void> allocateElasticDirect(long initialCapacity) throws IllegalArgumentException
initialCapacity
which will be resized as required.initialCapacity
- the initial non-negative capacity given in bytesIllegalArgumentException
- if the provided initialCapacity
is negative@NotNull static @NotNull OnHeapBytes allocateElasticOnHeap()
initialCapacity
32 bytes which will be resized as required.@NotNull static @NotNull OnHeapBytes allocateElasticOnHeap(int initialCapacity)
initialCapacity
which will be resized as required.initialCapacity
- the initial capacity of the wrapper in bytesIllegalArgumentException
- if the provided initialCapacity
is negative@NotNull static @NotNull String toString(@NotNull @NotNull Bytes<?> buffer) throws BufferUnderflowException, IllegalStateException, IllegalArgumentException
buffer
staring from the provided readPosition
to the
provided readLimit
. The buffer is not modified by this call.buffer
- the non-null buffer to usereadLimit
NullPointerException
- if the provided buffer
is null
BufferUnderflowException
IllegalStateException
IllegalArgumentException
@NotNull static @NotNull String toString(@NotNull @NotNull Bytes<?> buffer, long maxLen) throws BufferUnderflowException, IllegalStateException, IllegalArgumentException
readPosition
to the readLimit
with a specified maximum length, The buffer is not modified
by this call.
If the length of the string between readPosition
to readLimit
is greater than the specified maximum length,
the "..." will be appended to the resulting string; in this case the length of the resulting string will be the specified maximum length+3.
buffer
- the non-null buffer to usemaxLen
- the maximum length from the buffer, used to create a new stringNullPointerException
- if the provided buffer
is null
or if the provided maxLen
is negativeBufferUnderflowException
IllegalStateException
IllegalArgumentException
@NotNull static @NotNull String toString(@NotNull @NotNull Bytes<?> buffer, long position, long length)
buffer
with the provided length
staring from the provided offset
.
The buffer is not modified by this call.
buffer
- the non-null buffer to useposition
- the offset position to create the string fromlength
- the number of characters to include in the stringNullPointerException
- if the provided buffer
is null
orIllegalArgumentException
- if the provided position
or provided length
is negative@NotNull static @NotNull VanillaBytes allocateDirect(byte[] bytes) throws IllegalArgumentException
bytes
array.
Changes in the given bytes
will not be affected by writes in
the returned wrapper or vice versa.
bytes
- array to copy, the array reference must not be nullNullPointerException
- if the provided bytes
is null
IllegalArgumentException
@NotNull default @NotNull Bytes<U> unchecked(boolean unchecked) throws IllegalStateException
unchecked
.
This allows bounds checks to be turned off.
Note: this means that the result is no longer elastic, even if this
is elastic.
Note: It is only possible to go from a checked Bytes to an unchecked bytes and not vice versa.
unchecked
- if true, minimal bounds checks will be performedIllegalStateException
- if the underlying BytesStore has been releaseddefault boolean unchecked()
An unchecked Bytes object performs little or no bounds checking
default long safeLimit()
safeLimit
in interface BytesStore<Bytes<U>,U>
default boolean isClear()
BytesStore
readPosition
is at the start
and
the writeLimit
is at the end
.
I.e start() == readPosition() && writeLimit() == capacity()
isClear
in interface BytesStore<Bytes<U>,U>
readPosition
is at the start
and
the writeLimit
is at the end
default long realCapacity()
RandomDataInput
realCapacity
in interface BytesStore<Bytes<U>,U>
realCapacity
in interface RandomDataInput
realCapacity
in interface StreamingDataOutput<Bytes<U>>
BytesStore<Bytes<U>,U> copy() throws IllegalStateException
RandomCommon.readPosition()
to RandomCommon.readLimit()
.copy
in interface BytesStore<Bytes<U>,U>
IllegalStateException
@NotNull default @NotNull String toHexString()
@NotNull default @NotNull String toHexString(long maxLength)
maxLength
bytes.
Displays the hex data of Bytes
from the readPosition
up to the specified length.
maxLength
- limit the number of bytes to be dumpedIllegalArgumentException
- if the provided maxLength
is negative.@NotNull default @NotNull String toHexString(long offset, long maxLength)
offset
encoding at most the provided maxLength
bytes.offset
- the specified offset to start frommaxLength
- limit the number of bytes to be encodedIllegalArgumentException
- if the provided maxLength
or provided maxLength
is negative.boolean isElastic()
realCapacity()
.default void ensureCapacity(long desiredCapacity) throws DecoratedBufferOverflowException, IllegalStateException
DecoratedBufferOverflowException
desiredCapacity
- the capacity that you requiredIllegalStateException
- if closed and it needs a resizeDecoratedBufferOverflowException
- if the buffer is not elastic and there is not enough space or if the
provided desiredCapacity
is negative;@NotNull default @NotNull Bytes<U> bytesForRead() throws IllegalStateException
As a sub-section of a Bytes, it cannot be elastic.
bytesForRead
in interface BytesStore<Bytes<U>,U>
IllegalStateException
- if the underlying BytesStore has been released@Nullable @Nullable BytesStore bytesStore()
bytesStore
in interface BytesStore<Bytes<U>,U>
default boolean isEqual(@Nullable @Nullable String other) throws IllegalStateException
other
.other
- to test for equality, nullableother
IllegalStateException
- if this Bytes object has been previously released@NotNull @NotNull Bytes<U> compact() throws IllegalStateException
IllegalStateException
- if this Bytes object has been previously releaseddefault long copyTo(@NotNull @NotNull BytesStore targetByteStore) throws IllegalStateException
targetByteStore
.copyTo
in interface BytesStore<Bytes<U>,U>
copyTo
in interface StreamingDataInput<Bytes<U>>
targetByteStore
- the non-null BytesStore to copy toNullPointerException
- if the provided targetByteStore
is null
IllegalStateException
- if this BytesStore has been releaseddefault void copyTo(@NotNull @NotNull OutputStream outputStream) throws IOException, IllegalStateException
outputStream
.copyTo
in interface BytesStore<Bytes<U>,U>
copyTo
in interface StreamingDataInput<Bytes<U>>
outputStream
- the specified non-null OutputStream that this Bytes object is copied toIllegalStateException
- if this Bytes object has been previously releasedIOException
- if an I/O error occurs writing to the provided outputStream
NullPointerException
- if the provided outputStream
is null
OutputStream
default boolean sharedMemory()
A Bytes object that is backed by a memory mapped file can be shared whereas a Bytes object that is backed by heap memory cannot be shared across processes.
default void unwrite(long fromOffset, int count) throws BufferUnderflowException, BufferOverflowException, IllegalStateException
Calling this method will update the cursors of this Bytes object.
fromOffset
- the offset from the target bytescount
- the number of bytes to un-writeBufferUnderflowException
BufferOverflowException
IllegalStateException
@NotNull default @NotNull BigDecimal readBigDecimal() throws ArithmeticException, BufferUnderflowException, IllegalStateException
If this Byte object is empty, an object equal to BigDecimal.ZERO
is returned.
ArithmeticException
- if the content of this Bytes object could not be successfully convertedBufferUnderflowException
- if the content of this Bytes object is insufficient to be successfully convertedIllegalStateException
- if this Bytes object was previously released@NotNull default @NotNull BigInteger readBigInteger() throws ArithmeticException, BufferUnderflowException, IllegalStateException
BigInteger.ZERO
if this Bytes object is empty.ArithmeticException
- if the content of this Bytes object could not be successfully convertedBufferUnderflowException
- if the content of this Bytes object is insufficient to be successfully convertedIllegalStateException
- if this Bytes object was previously releaseddefault long indexOf(@NotNull @NotNull Bytes source) throws IllegalStateException
source
,
or -1 if no such index value exists.
On other words, returns the index within this Bytes object of the first occurrence of the
provided source
The returned index is the smallest value k for which:
If no such value of k exists, thenthis.startsWith(bytes, k)
-1
is returned.source
- the non-null sub-bytes to search for.NullPointerException
- if the provided source
is null
IllegalStateException
default int indexOf(@NotNull @NotNull BytesStore source, int fromIndex) throws IllegalStateException
fromIndex
for which the contents of this
Bytes object equals the provided source
, or -1 if no such index value exists.
On other words, returns the index within this Bytes object of the first occurrence of the
provided source
The returned index is the smallest value k for which:
If no such value of k exists, thenthis.startsWith(bytes, k)
-1
is returned.source
- the non-null sub-bytes to search for.fromIndex
- to start searching fromNullPointerException
- if the provided source
is null
IllegalStateException
@NotNull @NotNull Bytes<U> clear() throws IllegalStateException
StreamingCommon
clear
in interface StreamingCommon<Bytes<U>>
IllegalStateException
- if this resource has been previously releaseddefault boolean readWrite()
BytesStore
This is in contrast to a ByteStore than can only be read.
readWrite
in interface BytesStore<Bytes<U>,U>
default void readWithLength(long length, @NotNull @NotNull BytesOut<U> bytesOut) throws BufferUnderflowException, net.openhft.chronicle.core.io.IORuntimeException, BufferOverflowException, IllegalStateException
bytesOut
writing the provided
length
bytes.
The readLimit and readPosition is not affected by this operation.
length
- indicating the number of bytes to writebytesOut
- non-null target to write the content of this Bytes object toBufferUnderflowException
- if the provided length
is greater than the remaining bytes readable.net.openhft.chronicle.core.io.IORuntimeException
- if there is an error reading or writing dataBufferOverflowException
- if the provided bytesOut
cannot hold the bytes to be writtenIllegalStateException
- if this Bytes object or the provided bytesOut
has been previously released.IllegalArgumentException
- if the provided length
is negative.NullPointerException
- if the provided bytesOut
is null
.default <T extends ReadBytesMarshallable> T readMarshallableLength16(@NotNull @NotNull Class<T> clazz, @Nullable T using) throws BufferUnderflowException, IllegalStateException
clazz
using the
provided using
, or if null
is provided for using
, creates a new object, eventually
returning whichever object was updated.
The content of this Bytes object is assumed to have a 16-bit length indicator preceding the actual object.
readMarshallableLength16
in interface BytesIn<U>
T
- type of the provided usingclazz
- non-null type to readusing
- to update, nullableBufferUnderflowException
- if the content of this Bytes object is insufficient.IllegalStateException
- if this Bytes object or the provided bytesOut
has been previously released.NullPointerException
- if the provided clazz
is null
writeMarshallableLength16(WriteBytesMarshallable)
default void writeMarshallableLength16(@NotNull @NotNull WriteBytesMarshallable marshallable) throws IllegalArgumentException, BufferOverflowException, IllegalStateException, BufferUnderflowException
marshallable
to this Bytes object converting from an object
to a number of bytes.
The content of this Bytes object will have a 16-bit length indicator preceding the actual object.
writeMarshallableLength16
in interface BytesOut<U>
marshallable
- non-null object to write to this Bytes objectBufferOverflowException
- if the capacity of this Bytes object is insufficient.IllegalStateException
- if this Bytes object or the provided bytesOut
has been previously released
or if the contents of this Bytes object cannot fit under 16-bit addressingNullPointerException
- if the provided marshallable
is null
IllegalArgumentException
BufferUnderflowException
readMarshallableLength16(Class, ReadBytesMarshallable)
default Bytes write(@NotNull @NotNull InputStream inputStream) throws IOException, BufferOverflowException, IllegalStateException
inputStream
- non-null to read fromIOException
- if an I/O error occur on the inputStreamBufferOverflowException
- if this Bytes object lacks capacity to write the entire provided inputStream
IllegalStateException
- if this Bytes object or the provided bytesOut
has been previously releasedNullPointerException
- if the provided InputStream
is null
.Copyright © 2022. All rights reserved.