Class ByteUtils

java.lang.Object
com.inesm.KeyManager.utils.ByteUtils

public class ByteUtils extends Object
Provides byte conversion methods.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Charset
    Encoding used for string conversion
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static byte[]
    Converts a Base64 encoded string into a byte array.
    static String
    bytes2base64(byte[] bytes)
    Converts a byte array into a Base64 encoded string.
    static int
    bytes2int(byte[] i)
    Converts a byte array into an integer.
    static long
    bytes2long(byte[] bytes)
    Converts a byte array to a long.
    static Object
    bytes2object(byte[] msgBytes)
    Converts a byte array into an object.
    static String
    bytes2string(byte[] bytes)
    Converts a byte array into a string.
    static byte[]
    int2bytes(int i)
    Converts an integer into a byte array
    static byte[]
    long2bytes(long x)
    Converts a long to a byte array.
    static byte[]
    Converts an object into a byte array.
    static byte[]
    Converts a string into a byte array.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • ENCODING

      public static final Charset ENCODING
      Encoding used for string conversion
  • Constructor Details

    • ByteUtils

      public ByteUtils()
  • Method Details

    • long2bytes

      public static byte[] long2bytes(long x)
      Converts a long to a byte array.
      Parameters:
      x - the long value
      Returns:
      the byte array representation of the long value received
    • bytes2long

      public static long bytes2long(byte[] bytes)
      Converts a byte array to a long.
      Parameters:
      bytes - the byte array
      Returns:
      the first long value contained in the byte array
    • object2bytes

      public static byte[] object2bytes(Object obj) throws IOException
      Converts an object into a byte array.
      Parameters:
      obj - the object to be converted
      Returns:
      the byte array representing the object
      Throws:
      IOException - if an I/O occurs during the process
    • bytes2object

      public static Object bytes2object(byte[] msgBytes) throws ClassNotFoundException, IOException
      Converts a byte array into an object.
      Parameters:
      msgBytes - the byte array containing the object information
      Returns:
      the object represented by the byte array
      Throws:
      ClassNotFoundException - if no definition for the class can be found
      IOException - if an I/O error occurs during the process
    • string2bytes

      public static byte[] string2bytes(String str)
      Converts a string into a byte array. Uses string encoding defined in ENCODING.
      Parameters:
      str - the string
      Returns:
      the byte array representing the string value
    • bytes2string

      public static String bytes2string(byte[] bytes)
      Converts a byte array into a string. Uses string encoding defined in ENCODING
      Parameters:
      bytes - the byte array
      Returns:
      the string with the value represented by the byte array
    • int2bytes

      public static byte[] int2bytes(int i)
      Converts an integer into a byte array
      Parameters:
      i - the integer
      Returns:
      the byte array representing the integer value
    • bytes2int

      public static int bytes2int(byte[] i)
      Converts a byte array into an integer.
      Parameters:
      i - the byte array
      Returns:
      the first integer contained within the byte array
    • bytes2base64

      public static String bytes2base64(byte[] bytes)
      Converts a byte array into a Base64 encoded string.
      Parameters:
      bytes - the byte array
      Returns:
      the string containing the value represented by the byte array
    • base642bytes

      public static byte[] base642bytes(String msg)
      Converts a Base64 encoded string into a byte array.
      Parameters:
      msg - the Base64 encoded string
      Returns:
      the byte array containing the value represented by the string