Package io.pravega.common.util
Class BitConverter
- java.lang.Object
-
- io.pravega.common.util.BitConverter
-
public final class BitConverter extends java.lang.ObjectHelper methods for various Number to Bit conversions. IMPORTANT: This class is closed to new additions. While not deprecated (it's good to have similar methods in one place), consider using one of the following instead: -StructuredWritableBufferorStructuredReadableBufferimplementations, such asByteArraySegment). These provide the most efficient serialization implementations available. -DirectDataOutputimplementations, such asByteBufferOutputStream). This provides an efficient serialization implementation as well. -DataOutputStreamif none of the above are useful (this is the classic Java serializer).
-
-
Constructor Summary
Constructors Constructor Description BitConverter()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static intmakeInt(int b1, int b2, int b3, int b4)Composes a 32-bit integer from the given byte components (Big-Endian order).static longmakeLong(int b1, int b2, int b3, int b4, int b5, int b6, int b7, int b8)Composes 64-bit long from the given byte components (in Big Endian order).static intreadInt(byte[] source, int position)Reads a 32-bit integer from the given byte array starting at the given position.static intreadInt(java.io.InputStream source)Reads a 32-bit integer from the given InputStream that was encoded using BitConverter.writeInt.static longreadLong(byte[] source, int position)Reads a 64-bit long from the given byte array starting at the given position.static java.util.UUIDreadUUID(byte[] source, int position)Reads a 128-bit UUID from the given byte array starting at the given position.static intwriteInt(byte[] target, int offset, int value)Writes the given 32-bit Integer to the given byte array at the given offset.static intwriteInt(java.io.OutputStream target, int value)Writes the given 32-bit Integer to the given OutputStream.static intwriteLong(byte[] target, int offset, long value)Writes the given 64-bit Long to the given byte array at the given offset.static intwriteLong(java.io.OutputStream target, long value)Writes the given 64-bit Long to the given OutputStream.static intwriteShort(java.io.OutputStream target, short value)Writes the given 16-bit Short to the given OutputStream.static voidwriteUUID(StructuredWritableBuffer b, java.util.UUID value)Writes the given 128-bit UUID to the givenStructuredWritableBuffer.
-
-
-
Method Detail
-
writeShort
public static int writeShort(java.io.OutputStream target, short value) throws java.io.IOExceptionWrites the given 16-bit Short to the given OutputStream.- Parameters:
target- The OutputStream to write to.value- The value to write.- Returns:
- The number of bytes written.
- Throws:
java.io.IOException- If an error occurred.
-
writeInt
public static int writeInt(byte[] target, int offset, int value)Writes the given 32-bit Integer to the given byte array at the given offset.- Parameters:
target- The byte array to write to.offset- The offset within the byte array to write at.value- The value to write.- Returns:
- The number of bytes written.
-
writeInt
public static int writeInt(java.io.OutputStream target, int value) throws java.io.IOExceptionWrites the given 32-bit Integer to the given OutputStream.- Parameters:
target- The OutputStream to write to.value- The value to write.- Returns:
- The number of bytes written.
- Throws:
java.io.IOException- If an exception got thrown.
-
readInt
public static int readInt(byte[] source, int position)Reads a 32-bit integer from the given byte array starting at the given position.- Parameters:
source- The byte array to read from.position- The position in the byte array to start reading at.- Returns:
- The read number.
-
readInt
public static int readInt(java.io.InputStream source) throws java.io.IOExceptionReads a 32-bit integer from the given InputStream that was encoded using BitConverter.writeInt.- Parameters:
source- The InputStream to read from.- Returns:
- The read number.
- Throws:
java.io.IOException- If an exception got thrown.
-
makeInt
public static int makeInt(int b1, int b2, int b3, int b4)Composes a 32-bit integer from the given byte components (Big-Endian order).- Parameters:
b1- Byte #1.b2- Byte #2.b3- Byte #3.b4- Byte #4.- Returns:
- The composed number.
-
writeLong
public static int writeLong(byte[] target, int offset, long value)Writes the given 64-bit Long to the given byte array at the given offset.- Parameters:
target- The byte array to write to.offset- The offset within the byte array to write at.value- The value to write.- Returns:
- The number of bytes written.
-
writeLong
public static int writeLong(java.io.OutputStream target, long value) throws java.io.IOExceptionWrites the given 64-bit Long to the given OutputStream.- Parameters:
target- The OutputStream to write to.value- The value to write.- Returns:
- The number of bytes written.
- Throws:
java.io.IOException- If an exception got thrown.
-
writeUUID
public static void writeUUID(StructuredWritableBuffer b, java.util.UUID value)
Writes the given 128-bit UUID to the givenStructuredWritableBuffer.- Parameters:
b- TheStructuredWritableBufferto write to.value- The value to write.
-
readUUID
public static java.util.UUID readUUID(byte[] source, int position)Reads a 128-bit UUID from the given byte array starting at the given position.- Parameters:
source- The byte array to read from.position- The position in the byte array to start reading at.- Returns:
- The read UUID.
-
readLong
public static long readLong(byte[] source, int position)Reads a 64-bit long from the given byte array starting at the given position.- Parameters:
source- The byte array to read from.position- The position in the byte array to start reading at.- Returns:
- The read number.
-
makeLong
public static long makeLong(int b1, int b2, int b3, int b4, int b5, int b6, int b7, int b8)Composes 64-bit long from the given byte components (in Big Endian order).- Parameters:
b1- Byte #1.b2- Byte #2.b3- Byte #3.b4- Byte #4.b5- Byte #5.b6- Byte #6.b7- Byte #7.b8- Byte #8.- Returns:
- The composed number.
-
-