public final class Serialization
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static int |
MAGIC_NUMBER
A magic number that is always the first entry in a recording file.
|
static int |
SIZE_OF_BOOL
The size of a serialized
boolean , in bytes. |
static int |
SIZE_OF_BYTE
The size of a serialized
byte , in bytes. |
static int |
SIZE_OF_DOUBLE
The size of a serialized
double , in bytes. |
static int |
SIZE_OF_INT
The size of a serialized
int , in bytes. |
static int |
SIZE_OF_LONG
The size of a serialized
short , in bytes. |
static int |
SIZE_OF_SHORT
The size of a serialized
short , in bytes. |
Modifier and Type | Method and Description |
---|---|
static <T> T |
decode(byte[] buffer,
int bufferPosition,
DataType<T> type)
Decodes a byte buffer as a value of the given data type.
|
static <T> byte[] |
encode(T value) |
static <T> byte[] |
encode(T value,
DataType<T> type)
Encodes a value as a byte array.
|
static java.util.List<java.lang.String> |
getAllSourceNames(java.util.List<TimestampedData> data)
Gets the names of all the sources represented in a data set.
|
static byte[] |
header(java.util.List<TimestampedData> data)
Generates a header for a serialized recording.
|
static Recording |
loadRecording(java.nio.file.Path file)
Loads the recording stored in the given file.
|
static void |
put(byte[] dst,
byte[] src,
int pos)
Puts
src into dst at the given position. |
static boolean |
readBoolean(byte[] array)
Parses a byte array as a boolean value.
|
static boolean |
readBoolean(byte[] array,
int pos)
Reads a boolean from a byte array at the given position.
|
static double |
readDouble(byte[] array,
int pos)
Reads a double-precision number from a big-endian byte array.
|
static int |
readInt(byte[] array)
Reads a 32-bit int from a big-endian byte array.
|
static int |
readInt(byte[] array,
int pos)
Reads a 32-bit int from a big-endian byte array.
|
static long |
readLong(byte[] array)
Reads a 64-bit int from a big-endian byte array.
|
static long |
readLong(byte[] array,
int pos)
Reads a 64-bit int from a big-endian byte array.
|
static short |
readShort(byte[] array)
Reads a
short (signed 16-bit integer) from a big-endian byte array. |
static short |
readShort(byte[] array,
int pos)
Reads a
short (signed 16-bit integer) from a big-endian byte array. |
static java.lang.String |
readString(byte[] array,
int pos)
Reads a string from the given byte array, starting at the given position.
|
static java.lang.String[] |
readStringArray(byte[] array,
int pos)
Reads a string array from the given byte array, starting at the given position.
|
static void |
saveRecording(Recording recording,
java.nio.file.Path file)
Saves a recording to the given file.
|
static int |
sizeOfStringArray(java.lang.String[] array)
Gets the size of a string array if it were encoded as a byte array with
toByteArray(String[]) . |
static byte[] |
subArray(byte[] raw,
int start,
int end)
Deprecated.
use
Arrays.copyOfRange(byte[], int, int) instead |
static byte[] |
toByteArray(boolean val)
Encodes a boolean as a 1-byte array.
|
static byte[] |
toByteArray(double val)
Encodes a double-precision number as an 8-byte big-endian byte array.
|
static byte[] |
toByteArray(int val)
Encodes a 32-bit int as a 4-byte big-endian byte array.
|
static byte[] |
toByteArray(long val)
Encodes a 64-bit int as an 8-byte big-endian byte array.
|
static byte[] |
toByteArray(short val)
Encodes a 16-bit int as a 2-byte big-endian byte array.
|
static byte[] |
toByteArray(java.lang.String string)
Encodes a string as a big-endian byte array.
|
static byte[] |
toByteArray(java.lang.String[] array)
Encodes a string array as a big-endian byte array.
|
static void |
updateRecordingSave(Recording recording,
java.nio.file.Path file)
Updates a saved recording file with the contents of the given recording.
|
public static final int MAGIC_NUMBER
public static final int SIZE_OF_BYTE
byte
, in bytes.public static final int SIZE_OF_BOOL
boolean
, in bytes.public static final int SIZE_OF_SHORT
short
, in bytes.public static final int SIZE_OF_INT
int
, in bytes.public static final int SIZE_OF_LONG
short
, in bytes.public static final int SIZE_OF_DOUBLE
double
, in bytes.public static void saveRecording(Recording recording, java.nio.file.Path file) throws java.io.IOException
recording
- the recording to savefile
- the file to save tojava.io.IOException
- if the recording could not be saved to the given filepublic static void updateRecordingSave(Recording recording, java.nio.file.Path file) throws java.io.IOException
recording
- the recording to update the save file withfile
- the path to the save file to updatejava.io.IOException
- if the save file could not be updatedpublic static <T> byte[] encode(T value)
public static <T> byte[] encode(T value, DataType<T> type)
public static <T> T decode(byte[] buffer, int bufferPosition, DataType<T> type)
buffer
- the buffer to read frombufferPosition
- the position in the buffer to start reading fromtype
- the type of the data to decodepublic static Recording loadRecording(java.nio.file.Path file) throws java.io.IOException
file
- the recording file to loadjava.io.IOException
- if the file could not be read, or if it is in an unexpected binary formatpublic static int sizeOfStringArray(java.lang.String[] array)
toByteArray(String[])
.public static byte[] header(java.util.List<TimestampedData> data)
MAGIC_NUMBER
magic number, to help confirm data integritypublic static java.util.List<java.lang.String> getAllSourceNames(java.util.List<TimestampedData> data)
public static byte[] toByteArray(boolean val)
public static byte[] toByteArray(short val)
public static byte[] toByteArray(int val)
val
- the int to encodepublic static byte[] toByteArray(long val)
val
- the int to encodepublic static byte[] toByteArray(double val)
val
- the double to encodepublic static byte[] toByteArray(java.lang.String string)
public static byte[] toByteArray(java.lang.String[] array)
readStringArray(byte[], int)
.@Deprecated public static byte[] subArray(byte[] raw, int start, int end)
Arrays.copyOfRange(byte[], int, int)
insteadraw
in the range (start, end]
. Note: the two arrays
are distinct; modifying one will not modify the other.raw
- the array to get a subarray fromstart
- the starting index, inclusive, of the subarray in the originalend
- the final index, exclusive, of the subarray in the originalraw
in the range (start, end]
public static void put(byte[] dst, byte[] src, int pos)
src
into dst
at the given position. This is a shortcut for
System.arraycopy(src, 0, dst, pos, stc.length)
.dst
- the array to be copied intosrc
- the array to copypos
- the position in dst
to copy src
public static boolean readBoolean(byte[] array)
public static boolean readBoolean(byte[] array, int pos)
public static short readShort(byte[] array)
short
(signed 16-bit integer) from a big-endian byte array.public static short readShort(byte[] array, int pos)
short
(signed 16-bit integer) from a big-endian byte array.public static int readInt(byte[] array)
public static int readInt(byte[] array, int pos)
public static long readLong(byte[] array)
public static long readLong(byte[] array, int pos)
public static double readDouble(byte[] array, int pos)
public static java.lang.String readString(byte[] array, int pos)
array
- the array to read frompos
- the starting position of the encoded stringpublic static java.lang.String[] readStringArray(byte[] array, int pos)
array
- the array to read frompos
- the starting position of the encoded string array