Class XTablesByteUtils

java.lang.Object
org.kobe.xbot.Utilities.XTablesByteUtils

public class XTablesByteUtils extends Object
XTablesByteUtils - A utility class for converting between byte arrays, ByteString, and various data types.

This class provides static methods for converting byte arrays and ByteString objects into common Java types such as int, long, double, String, and boolean. These conversions are useful for handling raw byte data in a protocol-agnostic manner.

Author: Kobe Lei Version: 1.0 Package: org.kobe.xbot.Utilities

This class is a part of the XTABLES project and provides essential utility functions for byte conversion in data processing.

  • Constructor Details

    • XTablesByteUtils

      public XTablesByteUtils()
  • Method Details

    • convertXTableUpdateToJsonString

      public static String convertXTableUpdateToJsonString(XTableProto.XTableMessage.XTableUpdate node)
    • convertJsonStringToTypeValue

      public static Map.Entry<XTableProto.XTableMessage.Type,byte[]> convertJsonStringToTypeValue(String json)
    • getCoordinate

      public static XTableValues.Coordinate getCoordinate(int x, int y)
    • getCoordinateList

      public static List<XTableValues.Coordinate> getCoordinateList(XTableValues.Coordinate... coordinate)
    • getCoordinateListProto

      public static XTableValues.CoordinateList getCoordinateListProto(XTableValues.Coordinate... coordinate)
    • coordinateListToString

      public static String coordinateListToString(XTableValues.CoordinateList coordinateList)
      Converts a list of Coordinate protobuf objects into a human-readable string.
      Parameters:
      coordinateList - The CoordinateList protobuf object.
      Returns:
      A formatted string representing the list of coordinates.
    • unpack_coordinates_list

      public static List<XTableValues.Coordinate> unpack_coordinates_list(byte[] bytes)
      Converts a byte array into a list of Coordinate objects.
      Parameters:
      bytes - The byte array representing a serialized CoordinateList.
      Returns:
      A list of Coordinate objects, or null if parsing fails.
    • unpack_string_list

      public static List<String> unpack_string_list(com.google.protobuf.ByteString bytes)
    • unpack_double_list

      public static List<Double> unpack_double_list(com.google.protobuf.ByteString bytes)
    • unpack_integer_list

      public static List<Integer> unpack_integer_list(com.google.protobuf.ByteString bytes)
    • unpack_long_list

      public static List<Long> unpack_long_list(com.google.protobuf.ByteString bytes)
    • unpack_float_list

      public static List<Float> unpack_float_list(com.google.protobuf.ByteString bytes)
    • unpack_boolean_list

      public static List<Boolean> unpack_boolean_list(com.google.protobuf.ByteString bytes)
    • unpack_bytes_list

      public static List<com.google.protobuf.ByteString> unpack_bytes_list(com.google.protobuf.ByteString bytes)
    • unpack_coordinates_list

      public static List<XTableValues.Coordinate> unpack_coordinates_list(com.google.protobuf.ByteString bytes)
      Converts a byte array into a list of Coordinate objects.
      Parameters:
      bytes - The byte array representing a serialized CoordinateList.
      Returns:
      A list of Coordinate objects, or null if parsing fails.
    • coordinateListToString

      public static String coordinateListToString(byte[] rawData)
      Converts a raw protobuf byte array into a human-readable string of coordinates.
      Parameters:
      rawData - The byte array representing a serialized CoordinateList.
      Returns:
      A formatted string representation of the list of coordinates, or an error message if parsing fails.
    • convertTypeValueToJsonString

      public static String convertTypeValueToJsonString(XTableProto.XTableMessage.Type type, byte[] value)
    • to_Primitive_Int

      public static int to_Primitive_Int(byte[] bytes)
      Converts a byte array into an integer.

      This method interprets the first four bytes of the byte array as an integer. It expects the byte array to have a length of 4.

      Parameters:
      bytes - The byte array to convert.
      Returns:
      The integer value represented by the byte array.
      Throws:
      IllegalArgumentException - If the byte array is null or does not have a length of 4.
    • toLong

      public static long toLong(byte[] bytes)
      Converts a byte array into a long.

      This method interprets the first eight bytes of the byte array as a long. It expects the byte array to have a length of 8.

      Parameters:
      bytes - The byte array to convert.
      Returns:
      The long value represented by the byte array.
      Throws:
      IllegalArgumentException - If the byte array is null or does not have a length of 8.
    • to_Primitive_Double

      public static double to_Primitive_Double(byte[] bytes)
      Converts a byte array into a double.

      This method interprets the first eight bytes of the byte array as a double. It expects the byte array to have a length of 8.

      Parameters:
      bytes - The byte array to convert.
      Returns:
      The double value represented by the byte array.
      Throws:
      IllegalArgumentException - If the byte array is null or does not have a length of 8.
    • toString

      public static String toString(byte[] bytes)
      Converts a byte array into a string.

      This method decodes the byte array into a string using UTF-8 encoding.

      Parameters:
      bytes - The byte array to convert.
      Returns:
      The string represented by the byte array.
      Throws:
      IllegalArgumentException - If the byte array is null.
    • to_Primitive_Boolean

      public static boolean to_Primitive_Boolean(byte[] bytes)
      Converts a byte array into a boolean.

      This method checks if the first byte of the byte array is equal to 0x01, which represents true in boolean form.

      Parameters:
      bytes - The byte array to convert.
      Returns:
      The boolean value represented by the byte array.
      Throws:
      IllegalArgumentException - If the byte array is null or does not have a length of 1.
    • to_Primitive_Integer

      public static int to_Primitive_Integer(com.google.protobuf.ByteString byteString)
      Converts a ByteString into an integer.

      This method interprets the first four bytes of the ByteString as an integer. It expects the ByteString to have a size of 4.

      Parameters:
      byteString - The ByteString to convert.
      Returns:
      The integer value represented by the ByteString.
      Throws:
      IllegalArgumentException - If the ByteString is null or does not have a size of 4.
    • toInteger

      public static Integer toInteger(com.google.protobuf.ByteString byteString)
    • toInteger

      public static Integer toInteger(byte[] byteString)
    • to_Primitive_Long

      public static long to_Primitive_Long(com.google.protobuf.ByteString byteString)
      Converts a ByteString into a long.

      This method interprets the first eight bytes of the ByteString as a long. It expects the ByteString to have a size of 8.

      Parameters:
      byteString - The ByteString to convert.
      Returns:
      The long value represented by the ByteString.
      Throws:
      IllegalArgumentException - If the ByteString is null or does not have a size of 8.
    • toLong

      public static Long toLong(com.google.protobuf.ByteString byteString)
    • toDouble

      public static Double toDouble(com.google.protobuf.ByteString byteString)
      Converts a ByteString into a double.

      This method interprets the first eight bytes of the ByteString as a double. It expects the ByteString to have a size of 8.

      Parameters:
      byteString - The ByteString to convert.
      Returns:
      The double value represented by the ByteString.
      Throws:
      IllegalArgumentException - If the ByteString is null or does not have a size of 8.
    • toString

      public static String toString(com.google.protobuf.ByteString byteString)
      Converts a ByteString into a string.

      This method decodes the ByteString into a string using UTF-8 encoding.

      Parameters:
      byteString - The ByteString to convert.
      Returns:
      The string represented by the ByteString.
      Throws:
      IllegalArgumentException - If the ByteString is null.
    • toBoolean

      public static Boolean toBoolean(com.google.protobuf.ByteString byteString)
      Converts a ByteString into a boolean.

      This method checks if the first byte of the ByteString is equal to 0x01, which represents true in boolean form.

      Parameters:
      byteString - The ByteString to convert.
      Returns:
      The boolean value represented by the ByteString.
      Throws:
      IllegalArgumentException - If the ByteString is null or does not have a size of 1.
    • fromInteger

      public static byte[] fromInteger(int i)
      Converts an integer into a byte array.

      This method converts the provided integer into a 4-byte array.

      Parameters:
      i - The integer to convert.
      Returns:
      The byte array representing the integer.
    • fromLong

      public static byte[] fromLong(long i)
      Converts a long into a byte array.

      This method converts the provided long into an 8-byte array.

      Parameters:
      i - The long to convert.
      Returns:
      The byte array representing the long.
    • fromDouble

      public static byte[] fromDouble(double i)
      Converts a double into a byte array.

      This method converts the provided double into an 8-byte array.

      Parameters:
      i - The double to convert.
      Returns:
      The byte array representing the double.
    • fromBoolean

      public static byte[] fromBoolean(boolean i)
      Converts a boolean into a byte array.

      This method converts the provided boolean into a byte array. True is represented by 0x01, and false by 0x00.

      Parameters:
      i - The boolean to convert.
      Returns:
      The byte array representing the boolean.
    • fromString

      public static byte[] fromString(String i)
      Converts a string into a byte array.

      This method converts the provided string into a byte array using UTF-8 encoding.

      Parameters:
      i - The string to convert.
      Returns:
      The byte array representing the string.
    • fromObject

      public static byte[] fromObject(Object v)
      Converts an object into a byte array.

      This method serializes the provided object into a JSON string using Gson, then converts the JSON string into a byte array using UTF-8 encoding.

      Parameters:
      v - The object to convert.
      Returns:
      A byte array representing the object in JSON format.
    • toObject

      public static <T> T toObject(byte[] v, Class<T> clazz)
      Converts a byte array back into an object of the specified class type.

      This method deserializes the byte array into a JSON string, then uses Gson to convert the JSON string into an object of the specified type.

      Type Parameters:
      T - The type of the object.
      Parameters:
      v - The byte array to convert.
      clazz - The class type of the object to return.
      Returns:
      The deserialized object of type T.
      Throws:
      XTablesException - If there is an error during deserialization.
    • packPose3d

      public static byte[] packPose3d(edu.wpi.first.math.geometry.Pose3d pose)
      Packs a Pose3d object into a byte array.
      Parameters:
      pose - The Pose3d object to pack.
      Returns:
      A byte array representation of the Pose3d object.
    • pose3dToString

      public static String pose3dToString(byte[] data)
      Converts a Pose3d byte array into a human-readable string. If deserialization fails, it returns "Invalid Pose3d Data".
      Parameters:
      data - The byte array representing a serialized Pose3d.
      Returns:
      A human-readable string representation of the Pose3d.
    • pose3dToString

      public static String pose3dToString(edu.wpi.first.math.geometry.Pose3d pose)
      Converts a Pose3d object into a human-readable string. If deserialization fails, it returns "Invalid Pose3d Data".
      Parameters:
      pose - The Pose3d object.
      Returns:
      A human-readable string representation of the Pose3d.
    • unpackPose3d

      public static edu.wpi.first.math.geometry.Pose3d unpackPose3d(byte[] data)
      Unpacks a byte array into a Pose3d object.
      Parameters:
      data - The byte array to unpack.
      Returns:
      A Pose3d object if successful, or null if unpacking fails.
    • packPose2d

      public static byte[] packPose2d(edu.wpi.first.math.geometry.Pose2d pose)
      Packs a Pose2d object into a byte array.
      Parameters:
      pose - The Pose2d object to pack.
      Returns:
      A byte array representation of the Pose2d object.
    • pose2dToString

      public static String pose2dToString(byte[] data)
      Converts a Pose2d byte array into a human-readable string. If deserialization fails, it returns "Invalid Pose2d Data".
      Parameters:
      data - The byte array representing a serialized Pose2d.
      Returns:
      A human-readable string representation of the Pose2d.
    • pose2dToString

      public static String pose2dToString(edu.wpi.first.math.geometry.Pose2d pose)
      Converts a Pose2d byte array into a human-readable string. If deserialization fails, it returns "Invalid Pose2d Data".
      Parameters:
      pose - The pose
      Returns:
      A human-readable string representation of the Pose2d.
    • unpackPose2d

      public static edu.wpi.first.math.geometry.Pose2d unpackPose2d(byte[] data)
      Unpacks a byte array into a Pose2d object.
      Parameters:
      data - The byte array to unpack.
      Returns:
      A Pose2d object if successful, or null if unpacking fails.