Class Utils
java.lang.Object
org.cojen.tupl.io.Utils
Generic data and I/O utility methods.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
Closes a resource, which may throw a new exception.static IOException
closeOnFailure
(Closeable resource, Throwable cause) Closes the given resource, passing the cause if the resource implementsCauseCloseable
.static void
closeQuietly
(Closeable resource) Closes a resource without throwing another exception.static void
closeQuietly
(Closeable resource, Throwable cause) Closes a resource without throwing another exception.static IOException
closeQuietly
(IOException first, Closeable resource) Closes a resource without throwing another exception.static IOException
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
Attempt to delete file, throwing an IOException if the delete failed and the file still exists.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.static RuntimeException
Rethrows the given exception without the compiler complaining about it being checked or not.static RuntimeException
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
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.
-
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 incrementstart
- inclusive indexend
- 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 decrementstart
- inclusive indexend
- 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 destinationoffset
- offset into byte arrayv
- 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 destinationoffset
- offset into byte arrayv
- 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 destinationoffset
- offset into byte arrayv
- 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 destinationoffset
- offset into byte arrayv
- 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 destinationoffset
- offset into byte arrayv
- 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 destinationoffset
- offset into byte arrayv
- 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 destinationoffset
- offset into byte arrayv
- 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 destinationoffset
- offset into byte arrayv
- value to encode
-
decodeShortBE
public static short decodeShortBE(byte[] b, int offset) Decodes a 16-bit integer, in big-endian format.- Parameters:
b
- decode sourceoffset
- 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 sourceoffset
- 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 sourceoffset
- 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 sourceoffset
- 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 sourceoffset
- 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 sourceoffset
- 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 sourceoffset
- 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 sourceoffset
- 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 sourceoffset
- 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 sourceoffset
- 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 sourceoffset
- 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 sourceoffset
- offset into byte array- Returns:
- decoded value
-
readFully
Fully reads the required length of bytes, throwing an EOFException if the end of stream is reached too soon.- Throws:
IOException
-
closeOnFailure
Closes the given resource, passing the cause if the resource implementsCauseCloseable
. The cause is then rethrown, wrapped byCorruptDatabaseException
if not anIOException
or unchecked.- Throws:
IOException
-
closeQuietly
Closes a resource without throwing another exception.- Parameters:
resource
- can be null
-
closeQuietly
Closes a resource without throwing another exception.- Parameters:
resource
- can be nullcause
- passed to resource if it implementsCauseCloseable
-
closeQuietly
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-nullresource
- can be null- Returns:
- IOException which was caught, unless first was non-null
-
closeQuietly
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-nullresource
- can be nullcause
- passed to resource if it implementsCauseCloseable
- Returns:
- IOException which was caught, unless first was non-null
-
close
Closes a resource, which may throw a new exception.- Parameters:
cause
- passed to resource if it implementsCauseCloseable
- Throws:
IOException
-
delete
Attempt to delete file, throwing an IOException if the delete failed and the file still exists.- Parameters:
file
- file to delete; can be null- Throws:
IOException
-
suppress
Add a suppressed exception without creating a circular reference or throwing a new exception.- Parameters:
target
- exception to receive suppressed exception; can be nulltoSuppress
- exception to suppress and add to target; can be null
-
rootCause
-
uncaught
Convenience method to pass the given exception to the current thread's uncaught exception handler. -
rethrow
Rethrows the given exception without the compiler complaining about it being checked or not. Use as follows:throw rethrow(e)
-
rethrow
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
-