public enum BytesUtil extends Enum<BytesUtil>
This class provides a collection of static methods to manipulate and interact with Bytes objects. These methods include transformations, checks, and complex calculations not provided in the Bytes class itself.
Some of the operations provided in this class include:
All methods in this class throw NullPointerException
if the objects provided to them are null
.
Bytes
Modifier and Type | Method and Description |
---|---|
static void |
appendBytesFromStart(@NotNull Bytes<?> bytes,
long startPosition,
@NotNull StringBuilder sb)
Appends bytes from a specified start position of a Bytes object to a StringBuilder.
|
static void |
appendUtf8(@NotNull StreamingDataOutput out,
@NotNull CharSequence cs)
Writes a CharSequence as a UTF-8 string to a StreamingDataOutput.
|
static int |
asInt(@NotNull String str)
Converts a string to an integer using ISO_8859_1 encoding.
|
static boolean |
bytesEqual(@Nullable CharSequence cs,
@NotNull RandomDataInput bs,
long offset,
int length)
Compares a CharSequence with bytes from a RandomDataInput object to check if they are equal.
|
static boolean |
bytesEqual(@NotNull RandomDataInput a,
long offset,
@NotNull RandomDataInput second,
long secondOffset,
long len)
Compares bytes from two RandomDataInput objects to check if they are equal.
|
static boolean |
byteToBoolean(byte b)
Converts a byte to a boolean.
|
static void |
combineDoubleNewline(Bytes<?> bytes)
Checks the last two characters of the given Bytes object.
|
static void |
copy8bit(BytesStore bs,
long addressForWrite,
long length)
Copies 8-bit data from a BytesStore object to a specified address.
|
static BytesStore<Bytes<Void>,Void> |
copyOf(@NotNull Bytes<?> bytes)
Returns a copy of the given Bytes object.
|
static boolean |
equals(Object o1,
Object o2)
Compares two objects for equality, with special handling for CharSequences.
|
static String |
findFile(@NotNull String name)
Finds the absolute path of a file specified by name.
|
static boolean |
isTriviallyCopyable(@NotNull Class<?> clazz)
Checks if the given class is trivially copyable.
|
static boolean |
isTriviallyCopyable(Class clazz,
int offset,
int length)
Checks if all the fields in the specified range of the given class are trivially copyable.
|
static long |
padOffset(long from)
Pads an offset to align it to the nearest multiple of 4.
|
static void |
parseUtf8(@NotNull StreamingDataInput in,
Appendable appendable,
int utflen)
Parses a UTF-8 string from a StreamingDataInput and appends it to an Appendable object.
|
static void |
read8ByteAlignPadding(Bytes<?> bytes)
Reads padding bytes from a Bytes object to align the read position to the nearest 8-byte boundary.
|
static Bytes<?> |
readFile(@NotNull String name)
Reads the content of a file specified by name into a Bytes object.
|
static void |
readMarshallable(@NotNull ReadBytesMarshallable marshallable,
BytesIn<?> bytes)
Reads a Marshallable object from a BytesIn object.
|
static long |
readStopBit(@NotNull StreamingDataInput in)
Reads a variable-length integer from a StreamingDataInput using the stop bit encoding.
|
static void |
reverse(Bytes<?> text,
int start)
Reverses the contents of a Bytes object from a specified starting position.
|
static long |
roundNup(double d,
long factor)
Rounds up a double value to the nearest multiple of a specified factor.
|
static long |
roundUpTo64ByteAlign(long x)
Rounds up a long value to the nearest multiple of 64.
|
static long |
roundUpTo8ByteAlign(long x)
Rounds up a long value to the nearest multiple of 8.
|
static int |
stopBitLength(long n)
Calculates the number of bytes required to store a variable-length integer
using the stop bit encoding.
|
static @NotNull char[] |
toCharArray(@NotNull Bytes<?> bytes)
Converts the bytes from a Bytes object into a character array.
|
static @NotNull char[] |
toCharArray(@NotNull Bytes<?> bytes,
long position,
int length)
Converts a specific range of bytes from a Bytes object into a character array.
|
static String |
toDebugString(@NotNull RandomDataInput bytes,
long start,
long maxLength)
Returns a debug string representation of a portion of a RandomDataInput object.
|
static int |
triviallyCopyableLength(Class clazz)
Returns the length of the trivially copyable fields in a given class.
|
static int[] |
triviallyCopyableRange(Class clazz)
Returns the range within which a class is trivially copyable.
|
static int |
triviallyCopyableStart(Class clazz)
Returns the offset of the first byte of the trivially copyable fields of a given class.
|
static BytesUtil |
valueOf(String name)
Returns the enum constant of this type with the specified name.
|
static BytesUtil[] |
values()
Returns an array containing the constants of this enum type, in
the order they are declared.
|
static void |
write8ByteAlignPadding(Bytes<?> bytes)
Writes padding bytes to a Bytes object to align the write position to the nearest 8-byte boundary.
|
static void |
writeFile(String file,
Bytes<byte[]> bytes)
Writes the content of a Bytes object to a file specified by name.
|
static void |
writeMarshallable(@NotNull WriteBytesMarshallable marshallable,
BytesOut<?> bytes)
Writes a Marshallable object to a BytesOut object.
|
static long |
writeStopBit(BytesStore bs,
long offset,
long n)
Writes a variable-length integer to a specific position in a BytesStore using the stop bit encoding.
|
static long |
writeStopBit(long addr,
long n)
Writes a variable-length integer to a specific memory address using the stop bit encoding.
|
static void |
writeStopBit(@NotNull StreamingDataOutput out,
long n)
Writes a variable-length integer to a StreamingDataOutput using the stop bit encoding.
|
public static BytesUtil[] values()
for (BytesUtil c : BytesUtil.values()) System.out.println(c);
public static BytesUtil valueOf(String name)
name
- the name of the enum constant to be returned.IllegalArgumentException
- if this enum type has no constant with the specified nameNullPointerException
- if the argument is nullpublic static boolean isTriviallyCopyable(@NotNull @NotNull Class<?> clazz)
clazz
- Class to check.public static boolean isTriviallyCopyable(Class clazz, int offset, int length)
clazz
- Class to check.offset
- Start of the field area.length
- Length of the field area.public static int[] triviallyCopyableRange(Class clazz)
clazz
- Class to get the range for.public static int triviallyCopyableStart(Class clazz)
clazz
- The class to examine.public static int triviallyCopyableLength(Class clazz)
clazz
- The class to examine.public static String findFile(@NotNull @NotNull String name) throws FileNotFoundException
name
- The name of the file to find.FileNotFoundException
- If the file does not exist.public static Bytes<?> readFile(@NotNull @NotNull String name) throws IOException
name
- The name of the file to read.IOException
- If an I/O error occurs.public static void writeFile(String file, Bytes<byte[]> bytes) throws IOException
file
- The name of the file to write to.bytes
- The Bytes object containing the data to write.IOException
- If an I/O error occurs.public static boolean bytesEqual(@NotNull @NotNull RandomDataInput a, long offset, @NotNull @NotNull RandomDataInput second, long secondOffset, long len) throws BufferUnderflowException, IllegalStateException
a
- The first RandomDataInput object.offset
- The starting position in the first object.second
- The second RandomDataInput object.secondOffset
- The starting position in the second object.len
- The number of bytes to compare.BufferUnderflowException
- If there is insufficient data.IllegalStateException
- If an illegal state is encountered.public static boolean bytesEqual(@Nullable @Nullable CharSequence cs, @NotNull @NotNull RandomDataInput bs, long offset, int length) throws IllegalStateException, BufferUnderflowException
cs
- The CharSequence to compare.bs
- The RandomDataInput object.offset
- The starting position in the RandomDataInput object.length
- The number of bytes to compare.IllegalStateException
- If an illegal state is encountered.BufferUnderflowException
- If there is insufficient data.public static boolean equals(Object o1, Object o2)
o1
- The first object to compare.o2
- The second object to compare.public static int asInt(@NotNull @NotNull String str)
str
- The string to convert.public static int stopBitLength(long n)
n
- The integer to calculate the length for.@NotNull public static @NotNull char[] toCharArray(@NotNull @NotNull Bytes<?> bytes) throws ArithmeticException, IllegalStateException, BufferUnderflowException
bytes
- The Bytes object to convert.ArithmeticException
- If there is an arithmetic error.IllegalStateException
- If an illegal state is encountered.BufferUnderflowException
- If there is insufficient data.@NotNull public static @NotNull char[] toCharArray(@NotNull @NotNull Bytes<?> bytes, long position, int length) throws IllegalStateException, BufferUnderflowException
bytes
- The Bytes object to convert.position
- The starting position in the Bytes object.length
- The number of bytes to convert.IllegalStateException
- If an illegal state is encountered.BufferUnderflowException
- If there is insufficient data.public static long readStopBit(@NotNull @NotNull StreamingDataInput in) throws net.openhft.chronicle.core.io.IORuntimeException, IllegalStateException
in
- The StreamingDataInput to read from.net.openhft.chronicle.core.io.IORuntimeException
- If an IO error occurs.IllegalStateException
- If an illegal state is encountered.public static void writeStopBit(@NotNull @NotNull StreamingDataOutput out, long n) throws IllegalStateException, BufferOverflowException
out
- The StreamingDataOutput to write to.n
- The integer to write.IllegalStateException
- If an illegal state is encountered.BufferOverflowException
- If there is insufficient space.public static long writeStopBit(BytesStore bs, long offset, long n) throws IllegalStateException, BufferOverflowException
bs
- The BytesStore to write to.offset
- The position in the BytesStore to start writing.n
- The integer to write.IllegalStateException
- If an illegal state is encountered.BufferOverflowException
- If there is insufficient space.public static long writeStopBit(long addr, long n) throws IllegalStateException, BufferOverflowException
addr
- The memory address to write to.n
- The integer to write.IllegalStateException
- If an illegal state is encountered.BufferOverflowException
- If there is insufficient space.public static void parseUtf8(@NotNull @NotNull StreamingDataInput in, Appendable appendable, int utflen) throws UTFDataFormatRuntimeException, IllegalStateException, BufferUnderflowException
in
- The StreamingDataInput to read from.appendable
- The Appendable to append to.utflen
- The length of the UTF-8 string.UTFDataFormatRuntimeException
- If the UTF-8 format is invalid.IllegalStateException
- If an illegal state is encountered.BufferUnderflowException
- If there is insufficient data.public static void appendUtf8(@NotNull @NotNull StreamingDataOutput out, @NotNull @NotNull CharSequence cs) throws IndexOutOfBoundsException
out
- The StreamingDataOutput to write to.cs
- The CharSequence to write.IndexOutOfBoundsException
- If the CharSequence length is out of bounds.public static void appendBytesFromStart(@NotNull @NotNull Bytes<?> bytes, long startPosition, @NotNull @NotNull StringBuilder sb) throws IllegalStateException
bytes
- The Bytes object.startPosition
- The start position in the Bytes object.sb
- The StringBuilder to append to.IllegalStateException
- If an illegal state is encountered.public static void readMarshallable(@NotNull @NotNull ReadBytesMarshallable marshallable, BytesIn<?> bytes) throws net.openhft.chronicle.core.io.InvalidMarshallableException
marshallable
- The Marshallable object to read.bytes
- The BytesIn object to read from.net.openhft.chronicle.core.io.InvalidMarshallableException
- If the Marshallable object is invalid.public static void writeMarshallable(@NotNull @NotNull WriteBytesMarshallable marshallable, BytesOut<?> bytes) throws IllegalStateException, BufferOverflowException, ArithmeticException, BufferUnderflowException, net.openhft.chronicle.core.io.InvalidMarshallableException
marshallable
- The Marshallable object to write.bytes
- The BytesOut object to write to.IllegalStateException
- If an illegal state is encountered.BufferOverflowException
- If there is insufficient space.ArithmeticException
- If an arithmetic error occurs.BufferUnderflowException
- If there is insufficient data.net.openhft.chronicle.core.io.InvalidMarshallableException
- If the Marshallable object is invalid.public static boolean byteToBoolean(byte b)
b
- The byte to convert.public static long roundUpTo64ByteAlign(long x)
x
- The value to round up.public static long roundUpTo8ByteAlign(long x)
x
- The value to round up.public static void read8ByteAlignPadding(Bytes<?> bytes) throws IllegalStateException, BufferUnderflowException
bytes
- The Bytes object.IllegalStateException
- If an illegal state is encountered.BufferUnderflowException
- If there is insufficient data.public static void write8ByteAlignPadding(Bytes<?> bytes) throws BufferOverflowException, IllegalStateException
bytes
- The Bytes object.BufferOverflowException
- If there is insufficient space.IllegalStateException
- If an illegal state is encountered.public static String toDebugString(@NotNull @NotNull RandomDataInput bytes, long start, long maxLength) throws IllegalStateException, BufferUnderflowException, ArithmeticException
bytes
- The RandomDataInput object.start
- The starting position.maxLength
- The maximum length.IllegalStateException
- If an illegal state is encountered.BufferUnderflowException
- If there is insufficient data.ArithmeticException
- If an arithmetic error occurs.public static void copy8bit(BytesStore bs, long addressForWrite, long length)
bs
- The BytesStore object.addressForWrite
- The address for writing.length
- The length of data to copy.public static void reverse(Bytes<?> text, int start)
text
- The Bytes object.start
- The starting position.public static long roundNup(double d, long factor)
d
- The value to round up.factor
- The factor.public static long padOffset(long from)
from
- The original offset.public static void combineDoubleNewline(Bytes<?> bytes)
bytes
- to check and trim as needed.public static BytesStore<Bytes<Void>,Void> copyOf(@NotNull @NotNull Bytes<?> bytes) throws IllegalStateException
bytes
- The Bytes object to copy.IllegalStateException
- If the Bytes object has been released.Copyright © 2023. All rights reserved.