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 voidCloses a resource, which may throw a new exception.static IOExceptioncloseOnFailure(Closeable resource, Throwable cause) Closes the given resource, passing the cause if the resource implementsCauseCloseable.static voidcloseQuietly(Closeable resource) Closes a resource without throwing another exception.static voidcloseQuietly(Closeable resource, Throwable cause) Closes a resource without throwing another exception.static IOExceptioncloseQuietly(IOException first, Closeable resource) Closes a resource without throwing another exception.static IOExceptioncloseQuietly(IOException first, Closeable resource, Throwable cause) Closes a resource without throwing another exception.static intdecodeIntBE(byte[] b, int offset) Decodes a 32-bit integer, in big-endian format.static intdecodeIntLE(byte[] b, int offset) Decodes a 32-bit integer, in little-endian format.static longdecodeLongBE(byte[] b, int offset) Decodes a 64-bit integer, in big-endian format.static longdecodeLongLE(byte[] b, int offset) Decodes a 64-bit integer, in little-endian format.static shortdecodeShortBE(byte[] b, int offset) Decodes a 16-bit integer, in big-endian format.static shortdecodeShortLE(byte[] b, int offset) Decodes a 16-bit integer, in little-endian format.static longdecodeUnsignedInt48BE(byte[] b, int offset) Decodes a 48-bit unsigned integer, in big-endian format.static longdecodeUnsignedInt48LE(byte[] b, int offset) Decodes a 48-bit unsigned integer, in little-endian format.static longdecodeUnsignedIntBE(byte[] b, int offset) Decodes a 32-bit unsigned integer, in big-endian format.static longdecodeUnsignedIntLE(byte[] b, int offset) Decodes a 32-bit unsigned integer, in little-endian format.static intdecodeUnsignedShortBE(byte[] b, int offset) Decodes a 16-bit unsigned integer, in big-endian format.static intdecodeUnsignedShortLE(byte[] b, int offset) Decodes a 16-bit unsigned integer, in little-endian format.static booleandecrement(byte[] value, int start, int end) Subtracts one from an unsigned integer, represented as a byte array.static voidAttempt to delete file, throwing an IOException if the delete failed and the file still exists.static voidencodeInt48BE(byte[] b, int offset, long v) Encodes a 48-bit integer, in big-endian format.static voidencodeInt48LE(byte[] b, int offset, long v) Encodes a 48-bit integer, in little-endian format.static voidencodeIntBE(byte[] b, int offset, int v) Encodes a 32-bit integer, in big-endian format.static voidencodeIntLE(byte[] b, int offset, int v) Encodes a 32-bit integer, in little-endian format.static voidencodeLongBE(byte[] b, int offset, long v) Encodes a 64-bit integer, in big-endian format.static voidencodeLongLE(byte[] b, int offset, long v) Encodes a 64-bit integer, in little-endian format.static voidencodeShortBE(byte[] b, int offset, int v) Encodes a 16-bit integer, in big-endian format.static voidencodeShortLE(byte[] b, int offset, int v) Encodes a 16-bit integer, in little-endian format.static booleanincrement(byte[] value, int start, int end) Adds one to an unsigned integer, represented as a byte array.static voidreadFully(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 RuntimeExceptionRethrows the given exception without the compiler complaining about it being checked or not.static RuntimeExceptionRethrows the given exception without the compiler complaining about it being checked or not.static ThrowableReturns the root cause of the given exception.static voidAdd a suppressed exception without creating a circular reference or throwing a new exception.static voidConvenience 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 byCorruptDatabaseExceptionif not anIOExceptionor 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
-