Class Utils

java.lang.Object
org.cojen.tupl.io.Utils

public class Utils extends Object
Generic data and I/O utility methods.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    close(Closeable resource, Throwable cause)
    Closes a resource, which may throw a new exception.
    Closes the given resource, passing the cause if the resource implements CauseCloseable.
    static void
    Closes a resource without throwing another exception.
    static void
    closeQuietly(Closeable resource, Throwable cause)
    Closes a resource without throwing another exception.
    Closes a resource without throwing another exception.
    closeQuietly(IOException first, Closeable resource, Throwable cause)
    Closes a resource without throwing another exception.
    static int
    decodeIntBE(byte[] b, int offset)
    Decodes a 32-bit integer, in big-endian format.
    static int
    decodeIntLE(byte[] b, int offset)
    Decodes a 32-bit integer, in little-endian format.
    static long
    decodeLongBE(byte[] b, int offset)
    Decodes a 64-bit integer, in big-endian format.
    static long
    decodeLongLE(byte[] b, int offset)
    Decodes a 64-bit integer, in little-endian format.
    static short
    decodeShortBE(byte[] b, int offset)
    Decodes a 16-bit integer, in big-endian format.
    static short
    decodeShortLE(byte[] b, int offset)
    Decodes a 16-bit integer, in little-endian format.
    static long
    decodeUnsignedInt48BE(byte[] b, int offset)
    Decodes a 48-bit unsigned integer, in big-endian format.
    static long
    decodeUnsignedInt48LE(byte[] b, int offset)
    Decodes a 48-bit unsigned integer, in little-endian format.
    static long
    decodeUnsignedIntBE(byte[] b, int offset)
    Decodes a 32-bit unsigned integer, in big-endian format.
    static long
    decodeUnsignedIntLE(byte[] b, int offset)
    Decodes a 32-bit unsigned integer, in little-endian format.
    static int
    decodeUnsignedShortBE(byte[] b, int offset)
    Decodes a 16-bit unsigned integer, in big-endian format.
    static int
    decodeUnsignedShortLE(byte[] b, int offset)
    Decodes a 16-bit unsigned integer, in little-endian format.
    static boolean
    decrement(byte[] value, int start, int end)
    Subtracts one from an unsigned integer, represented as a byte array.
    static void
    encodeInt48BE(byte[] b, int offset, long v)
    Encodes a 48-bit integer, in big-endian format.
    static void
    encodeInt48LE(byte[] b, int offset, long v)
    Encodes a 48-bit integer, in little-endian format.
    static void
    encodeIntBE(byte[] b, int offset, int v)
    Encodes a 32-bit integer, in big-endian format.
    static void
    encodeIntLE(byte[] b, int offset, int v)
    Encodes a 32-bit integer, in little-endian format.
    static void
    encodeLongBE(byte[] b, int offset, long v)
    Encodes a 64-bit integer, in big-endian format.
    static void
    encodeLongLE(byte[] b, int offset, long v)
    Encodes a 64-bit integer, in little-endian format.
    static void
    encodeShortBE(byte[] b, int offset, int v)
    Encodes a 16-bit integer, in big-endian format.
    static void
    encodeShortLE(byte[] b, int offset, int v)
    Encodes a 16-bit integer, in little-endian format.
    static boolean
    increment(byte[] value, int start, int end)
    Adds one to an unsigned integer, represented as a byte array.
    static void
    readFully(InputStream in, byte[] b, int off, int len)
    Fully reads the required length of bytes, throwing an EOFException if the end of stream is reached too soon.
    Rethrows the given exception without the compiler complaining about it being checked or not.
    Rethrows the given exception without the compiler complaining about it being checked or not.
    static Throwable
    Returns the root cause of the given exception.
    static void
    suppress(Throwable target, Throwable toSuppress)
    Add a suppressed exception without creating a circular reference or throwing a new exception.
    static void
    Convenience method to pass the given exception to the current thread's uncaught exception handler.

    Methods inherited from class java.lang.Object

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

    • Utils

      protected Utils()
  • Method Details

    • increment

      public static boolean increment(byte[] value, int start, int end)
      Adds one to an unsigned integer, represented as a byte array. If overflowed, value in byte array is 0x00, 0x00, 0x00...
      Parameters:
      value - unsigned integer to increment
      start - inclusive index
      end - exclusive index
      Returns:
      false if overflowed
    • decrement

      public static boolean decrement(byte[] value, int start, int end)
      Subtracts one from an unsigned integer, represented as a byte array. If overflowed, value in byte array is 0xff, 0xff, 0xff...
      Parameters:
      value - unsigned integer to decrement
      start - inclusive index
      end - exclusive index
      Returns:
      false if overflowed
    • encodeShortBE

      public static void encodeShortBE(byte[] b, int offset, int v)
      Encodes a 16-bit integer, in big-endian format.
      Parameters:
      b - encode destination
      offset - offset into byte array
      v - value to encode
    • encodeShortLE

      public static void encodeShortLE(byte[] b, int offset, int v)
      Encodes a 16-bit integer, in little-endian format.
      Parameters:
      b - encode destination
      offset - offset into byte array
      v - value to encode
    • encodeIntBE

      public static void encodeIntBE(byte[] b, int offset, int v)
      Encodes a 32-bit integer, in big-endian format.
      Parameters:
      b - encode destination
      offset - offset into byte array
      v - value to encode
    • encodeIntLE

      public static void encodeIntLE(byte[] b, int offset, int v)
      Encodes a 32-bit integer, in little-endian format.
      Parameters:
      b - encode destination
      offset - offset into byte array
      v - value to encode
    • encodeInt48BE

      public static void encodeInt48BE(byte[] b, int offset, long v)
      Encodes a 48-bit integer, in big-endian format.
      Parameters:
      b - encode destination
      offset - offset into byte array
      v - value to encode
    • encodeInt48LE

      public static void encodeInt48LE(byte[] b, int offset, long v)
      Encodes a 48-bit integer, in little-endian format.
      Parameters:
      b - encode destination
      offset - offset into byte array
      v - value to encode
    • encodeLongBE

      public static void encodeLongBE(byte[] b, int offset, long v)
      Encodes a 64-bit integer, in big-endian format.
      Parameters:
      b - encode destination
      offset - offset into byte array
      v - value to encode
    • encodeLongLE

      public static void encodeLongLE(byte[] b, int offset, long v)
      Encodes a 64-bit integer, in little-endian format.
      Parameters:
      b - encode destination
      offset - offset into byte array
      v - value to encode
    • decodeShortBE

      public static short decodeShortBE(byte[] b, int offset)
      Decodes a 16-bit integer, in big-endian format.
      Parameters:
      b - decode source
      offset - offset into byte array
      Returns:
      decoded value
    • decodeShortLE

      public static short decodeShortLE(byte[] b, int offset)
      Decodes a 16-bit integer, in little-endian format.
      Parameters:
      b - decode source
      offset - offset into byte array
      Returns:
      decoded value
    • decodeUnsignedShortBE

      public static int decodeUnsignedShortBE(byte[] b, int offset)
      Decodes a 16-bit unsigned integer, in big-endian format.
      Parameters:
      b - decode source
      offset - offset into byte array
      Returns:
      decoded value
    • decodeUnsignedShortLE

      public static int decodeUnsignedShortLE(byte[] b, int offset)
      Decodes a 16-bit unsigned integer, in little-endian format.
      Parameters:
      b - decode source
      offset - offset into byte array
      Returns:
      decoded value
    • decodeIntBE

      public static int decodeIntBE(byte[] b, int offset)
      Decodes a 32-bit integer, in big-endian format.
      Parameters:
      b - decode source
      offset - offset into byte array
      Returns:
      decoded value
    • decodeIntLE

      public static int decodeIntLE(byte[] b, int offset)
      Decodes a 32-bit integer, in little-endian format.
      Parameters:
      b - decode source
      offset - offset into byte array
      Returns:
      decoded value
    • decodeUnsignedIntBE

      public static long decodeUnsignedIntBE(byte[] b, int offset)
      Decodes a 32-bit unsigned integer, in big-endian format.
      Parameters:
      b - decode source
      offset - offset into byte array
      Returns:
      decoded value
    • decodeUnsignedIntLE

      public static long decodeUnsignedIntLE(byte[] b, int offset)
      Decodes a 32-bit unsigned integer, in little-endian format.
      Parameters:
      b - decode source
      offset - offset into byte array
      Returns:
      decoded value
    • decodeUnsignedInt48BE

      public static long decodeUnsignedInt48BE(byte[] b, int offset)
      Decodes a 48-bit unsigned integer, in big-endian format.
      Parameters:
      b - decode source
      offset - offset into byte array
      Returns:
      decoded value
    • decodeUnsignedInt48LE

      public static long decodeUnsignedInt48LE(byte[] b, int offset)
      Decodes a 48-bit unsigned integer, in little-endian format.
      Parameters:
      b - decode source
      offset - offset into byte array
      Returns:
      decoded value
    • decodeLongBE

      public static long decodeLongBE(byte[] b, int offset)
      Decodes a 64-bit integer, in big-endian format.
      Parameters:
      b - decode source
      offset - offset into byte array
      Returns:
      decoded value
    • decodeLongLE

      public static long decodeLongLE(byte[] b, int offset)
      Decodes a 64-bit integer, in little-endian format.
      Parameters:
      b - decode source
      offset - offset into byte array
      Returns:
      decoded value
    • readFully

      public static void readFully(InputStream in, byte[] b, int off, int len) throws IOException
      Fully reads the required length of bytes, throwing an EOFException if the end of stream is reached too soon.
      Throws:
      IOException
    • closeOnFailure

      public static IOException closeOnFailure(Closeable resource, Throwable cause) throws IOException
      Closes the given resource, passing the cause if the resource implements CauseCloseable. The cause is then rethrown, wrapped by CorruptDatabaseException if not an IOException or unchecked.
      Throws:
      IOException
    • closeQuietly

      public static void closeQuietly(Closeable resource)
      Closes a resource without throwing another exception.
      Parameters:
      resource - can be null
    • closeQuietly

      public static void closeQuietly(Closeable resource, Throwable cause)
      Closes a resource without throwing another exception.
      Parameters:
      resource - can be null
      cause - passed to resource if it implements CauseCloseable
    • closeQuietly

      public static IOException closeQuietly(IOException first, Closeable resource)
      Closes a resource without throwing another exception. If closing a chain of resources, pass in the first caught exception, and all others are discarded.
      Parameters:
      first - returned if non-null
      resource - can be null
      Returns:
      IOException which was caught, unless first was non-null
    • closeQuietly

      public static IOException closeQuietly(IOException first, Closeable resource, Throwable cause)
      Closes a resource without throwing another exception. If closing a chain of resources, pass in the first caught exception, and all others are discarded.
      Parameters:
      first - returned if non-null
      resource - can be null
      cause - passed to resource if it implements CauseCloseable
      Returns:
      IOException which was caught, unless first was non-null
    • close

      public static void close(Closeable resource, Throwable cause) throws IOException
      Closes a resource, which may throw a new exception.
      Parameters:
      cause - passed to resource if it implements CauseCloseable
      Throws:
      IOException
    • suppress

      public static void suppress(Throwable target, Throwable toSuppress)
      Add a suppressed exception without creating a circular reference or throwing a new exception.
      Parameters:
      target - exception to receive suppressed exception; can be null
      toSuppress - exception to suppress and add to target; can be null
    • rootCause

      public static Throwable rootCause(Throwable e)
      Returns the root cause of the given exception.
      Returns:
      non-null cause, unless given exception was null
    • uncaught

      public static void uncaught(Throwable e)
      Convenience method to pass the given exception to the current thread's uncaught exception handler.
    • rethrow

      public static RuntimeException rethrow(Throwable e)
      Rethrows the given exception without the compiler complaining about it being checked or not. Use as follows: throw rethrow(e)
    • rethrow

      public static RuntimeException rethrow(Throwable e, Throwable cause)
      Rethrows the given exception without the compiler complaining about it being checked or not. The exception can have a root cause initialized, which will be the root cause of the one given. Use as follows: throw rethrow(e, cause)
      Parameters:
      cause - initialize the exception's cause, unless it already has one