Class Util
Design rationale: Using static methods allows developers to perform
encoding and decoding operations directly on existing InputStream and
OutputStream instances without the need for additional stream
wrappers such as FilterInputStream or FilterOutputStream.
This approach eliminates extra object creation and method call overhead,
potentially improving performance, especially in high-throughput scenarios.
Usage: These methods can be used independently with any I/O stream to achieve custom serialization formats, such as length-prefixed UTF-8 strings, packed integers, or custom binary structures.
-
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]read(InputStream in, int maxLength) Reads a byte array from the input stream.static longreadLong(InputStream in) Reads a long value from the input stream in big-endian order (8 bytes).static intReads an integer from the input stream using packed 7-bit encoding (variable length).static longReads a long value from the input stream using packed 7-bit encoding (variable length).static StringreadUTF8(InputStream in, int maxLength) Reads a UTF-8 encoded string from the input stream.static UUIDreadUUID(InputStream in) Reads a UUID from the input stream, composed of two big-endian long values.static byte[]readWithPackedLengthPrefix(InputStream in, int maxSize) Reads up tomaxSizebytes from the givenInputStream, encodes the total number of bytes using 7-bit variable-length encoding (similar towritePack7L(OutputStream, long)), and returns a single byte array containing the encoded length prefix followed by the actual data read.static voidwrite(OutputStream out, byte[] buf) Writes a byte array to the output stream with its length encoded as a packed 7-bit integer.static voidwrite(OutputStream out, UUID uuid) Writes a UUID to the output stream as two big-endian long values.static voidwriteLong(OutputStream out, long val) Writes a long value to the output stream in big-endian order (8 bytes).static voidwritePack7I(OutputStream out, int val) Writes an integer to the output stream using packed 7-bit encoding (variable length).static voidwritePack7L(OutputStream out, long val) Writes a long value to the output stream using packed 7-bit encoding (variable length).static voidwriteUTF8(OutputStream out, String str) Writes a UTF-8 encoded string to the output stream with its length as a packed 7-bit integer.
-
Method Details
-
write
Writes a byte array to the output stream with its length encoded as a packed 7-bit integer.- Parameters:
out- the output streambuf- the byte array to write- Throws:
IOException- if an I/O error occurs
-
write
Writes a UUID to the output stream as two big-endian long values.- Parameters:
out- the output streamuuid- the UUID to write- Throws:
IOException- if an I/O error occurs
-
writeUTF8
Writes a UTF-8 encoded string to the output stream with its length as a packed 7-bit integer.- Parameters:
out- the output streamstr- the string to write- Throws:
IOException- if an I/O error occurs
-
writeLong
Writes a long value to the output stream in big-endian order (8 bytes).- Parameters:
out- the output streamval- the long value to write- Throws:
IOException- if an I/O error occurs
-
readUUID
Reads a UUID from the input stream, composed of two big-endian long values.- Parameters:
in- the input stream- Returns:
- the UUID read from the stream
- Throws:
IOException- if an I/O error occurs or if the stream ends prematurely
-
readUTF8
Reads a UTF-8 encoded string from the input stream. The string is prefixed with a packed 7-bit integer indicating its length. To prevent excessive allocation, the maximum allowable length must be specified.- Parameters:
in- the input streammaxLength- the maximum allowable length of the string in bytes- Returns:
- the string read from the stream
- Throws:
IOException- if an I/O error occurs, the length exceeds maxLength, or if the stream ends prematurely
-
readLong
Reads a long value from the input stream in big-endian order (8 bytes).- Parameters:
in- the input stream- Returns:
- the long value read
- Throws:
IOException- if an I/O error occurs or if the stream ends prematurely
-
writePack7I
Writes an integer to the output stream using packed 7-bit encoding (variable length).- Parameters:
out- the output streamval- the integer value to write- Throws:
IOException- if an I/O error occurs
-
writePack7L
Writes a long value to the output stream using packed 7-bit encoding (variable length).- Parameters:
out- the output streamval- the long value to write- Throws:
IOException- if an I/O error occurs
-
read
Reads a byte array from the input stream. The length of the array is specified as a packed 7-bit integer prefix. To prevent excessive allocation, the maximum allowable length must be specified.- Parameters:
in- the input streammaxLength- the maximum allowable length of the byte array- Returns:
- the byte array read
- Throws:
IOException- if an I/O error occurs, the length exceeds maxLength, or if the stream ends prematurely
-
readPack7I
Reads an integer from the input stream using packed 7-bit encoding (variable length).- Parameters:
in- the input stream- Returns:
- the integer value read
- Throws:
IOException- if an I/O error occurs or if the stream ends prematurely
-
readPack7L
Reads a long value from the input stream using packed 7-bit encoding (variable length). Payload groups are stored most-significant first, and the high bit marks the final byte. At most ten bytes are accepted. In a ten-byte representation, the first payload group is limited to one bit so that the encoded value fits exactly in the 64-bitlongbit pattern.Both non-negative and negative
longvalues produced bywritePack7L(OutputStream, long)are supported. The method consumes exactly one complete packed value and performs at most ten stream reads.- Parameters:
in- the input stream- Returns:
- the long value read
- Throws:
EOFException- if the stream ends before a terminating byteIOException- if an I/O error occurs, the encoding exceeds ten bytes, or the payload exceeds 64 bits
-
readWithPackedLengthPrefix
Reads up tomaxSizebytes from the givenInputStream, encodes the total number of bytes using 7-bit variable-length encoding (similar towritePack7L(OutputStream, long)), and returns a single byte array containing the encoded length prefix followed by the actual data read.Input is retained in chunks until the full size is known, then copied into one result array. For n input bytes, time and auxiliary memory are O(n); the retained chunks coexist with the result during the final copy. The size limit bounds accepted input, not peak allocation to a single buffer.
The size prefix uses a packed 7-bit encoding, where the most significant bit of the last byte is set to 1. This format is useful for compactly representing lengths in binary protocols.
- Parameters:
in- theInputStreamto read data from; must not benullmaxSize- the maximum number of bytes allowed to be read from the stream; must be non-negative and less than or equal toInteger.MAX_VALUE - 10- Returns:
- a
byte[]array where the first bytes are the encoded length, followed by the data read - Throws:
IOException- if an I/O error occurs or if the data read exceedsmaxSizeIllegalArgumentException- ifmaxSizeis negative or too large
-