Class ByteUtil

java.lang.Object
org.cryptacular.util.ByteUtil

public final class ByteUtil extends Object
Utilities for working with bytes.
Author:
Middleware Services
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Charset
    ASCII charactr set.
    static final Charset
    Default character set for bytes is UTF-8.
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    Reads 4-bytes from the input stream and converts to a 32-bit integer.
    static long
    Reads 8-bytes from the input stream and converts to a 64-bit long integer.
    static byte[]
    Converts a byte buffer into a byte array.
    static ByteBuffer
    Converts a string into bytes in the UTF-8 character set.
    static byte[]
    toBytes(int value)
    Converts an integer into a 4-byte big endian array.
    static void
    toBytes(int value, byte[] output, int offset)
    Converts an integer into a 4-byte big endian array.
    static byte[]
    toBytes(long value)
    Converts a long integer into an 8-byte big endian array.
    static void
    toBytes(long value, byte[] output, int offset)
    Converts an integer into a 8-byte big endian array.
    static byte[]
    Converts a string into bytes in the UTF-8 character set.
    static CharBuffer
    Converts a byte buffer into a character buffer.
    static int
    toInt(byte unsigned)
    Converts an unsigned byte into an integer.
    static int
    toInt(byte[] data)
    Converts the big-endian representation of a 32-bit integer to the equivalent integer value.
    static long
    toLong(byte[] data)
    Converts the big-endian representation of a 64-bit integer to the equivalent long value.
    static String
    toString(byte[] bytes)
    Converts a byte array into a string in the UTF-8 character set.
    static String
    toString(byte[] bytes, int offset, int length)
    Converts a portion of a byte array into a string in the UTF-8 character set.
    static String
    Converts a byte buffer into a string in the UTF-8 character set.
    static byte
    Converts an integer into an unsigned byte.

    Methods inherited from class java.lang.Object

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

    • DEFAULT_CHARSET

      public static final Charset DEFAULT_CHARSET
      Default character set for bytes is UTF-8.
    • ASCII_CHARSET

      public static final Charset ASCII_CHARSET
      ASCII charactr set.
  • Method Details

    • toInt

      public static int toInt(byte[] data)
      Converts the big-endian representation of a 32-bit integer to the equivalent integer value.
      Parameters:
      data - 4-byte array in big-endian format.
      Returns:
      Integer value.
    • toInt

      public static int toInt(byte unsigned)
      Converts an unsigned byte into an integer.
      Parameters:
      unsigned - Unsigned byte.
      Returns:
      Integer value.
    • readInt

      public static int readInt(InputStream in) throws StreamException
      Reads 4-bytes from the input stream and converts to a 32-bit integer.
      Parameters:
      in - Stream from which to read 4 bytes.
      Returns:
      Integer value.
      Throws:
      StreamException - on stream IO errors.
    • toLong

      public static long toLong(byte[] data)
      Converts the big-endian representation of a 64-bit integer to the equivalent long value.
      Parameters:
      data - 8-byte array in big-endian format.
      Returns:
      Long integer value.
    • readLong

      public static long readLong(InputStream in) throws StreamException
      Reads 8-bytes from the input stream and converts to a 64-bit long integer.
      Parameters:
      in - Stream from which to read 8 bytes.
      Returns:
      Long integer value.
      Throws:
      StreamException - on stream IO errors.
    • toBytes

      public static byte[] toBytes(int value)
      Converts an integer into a 4-byte big endian array.
      Parameters:
      value - Integer value to convert.
      Returns:
      4-byte big-endian representation of integer value.
    • toBytes

      public static void toBytes(int value, byte[] output, int offset)
      Converts an integer into a 4-byte big endian array.
      Parameters:
      value - Integer value to convert.
      output - Array into which bytes are placed.
      offset - Offset into output array at which output bytes start.
    • toBytes

      public static byte[] toBytes(long value)
      Converts a long integer into an 8-byte big endian array.
      Parameters:
      value - Long integer value to convert.
      Returns:
      8-byte big-endian representation of long integer value.
    • toBytes

      public static void toBytes(long value, byte[] output, int offset)
      Converts an integer into a 8-byte big endian array.
      Parameters:
      value - Long value to convert.
      output - Array into which bytes are placed.
      offset - Offset into output array at which output bytes start.
    • toString

      public static String toString(byte[] bytes)
      Converts a byte array into a string in the UTF-8 character set.
      Parameters:
      bytes - Byte array to convert.
      Returns:
      UTF-8 string representation of bytes.
    • toString

      public static String toString(byte[] bytes, int offset, int length)
      Converts a portion of a byte array into a string in the UTF-8 character set.
      Parameters:
      bytes - Byte array to convert.
      offset - Offset into byte array where string content begins.
      length - Total number of bytes to convert.
      Returns:
      UTF-8 string representation of bytes.
    • toString

      public static String toString(ByteBuffer buffer)
      Converts a byte buffer into a string in the UTF-8 character set.
      Parameters:
      buffer - Byte buffer to convert.
      Returns:
      UTF-8 string representation of bytes.
    • toCharBuffer

      public static CharBuffer toCharBuffer(ByteBuffer buffer)
      Converts a byte buffer into a character buffer.
      Parameters:
      buffer - Byte buffer to convert.
      Returns:
      Character buffer containing UTF-8 string representation of bytes.
    • toByteBuffer

      public static ByteBuffer toByteBuffer(String s)
      Converts a string into bytes in the UTF-8 character set.
      Parameters:
      s - String to convert.
      Returns:
      Byte buffer containing byte representation of string.
    • toBytes

      public static byte[] toBytes(String s)
      Converts a string into bytes in the UTF-8 character set.
      Parameters:
      s - String to convert.
      Returns:
      Byte array containing byte representation of string.
    • toUnsignedByte

      public static byte toUnsignedByte(int b)
      Converts an integer into an unsigned byte. All bits above 1 byte are truncated.
      Parameters:
      b - Integer value.
      Returns:
      Unsigned byte as a byte.
    • toArray

      public static byte[] toArray(ByteBuffer buffer)
      Converts a byte buffer into a byte array.
      Parameters:
      buffer - Byte buffer to convert.
      Returns:
      Byte array corresponding to bytes of buffer from current position to limit.