Package io.bytestreams.codec.core
Class FixedCodePointStringCodec
java.lang.Object
io.bytestreams.codec.core.FixedCodePointStringCodec
A codec for fixed-length character strings.
The length is specified in code points, not bytes or chars. For single-byte charsets (e.g.,
ISO-8859-1, US-ASCII), decoding uses simple byte-to-string conversion. For multi-byte charsets
(e.g., UTF-8, UTF-16), decoding uses CodePointReader to read exactly the required number
of code points.
Example usage:
// Fixed-length ASCII string
Codec<String> ascii = Codecs.ascii(5);
// Fixed-length UTF-8 string
Codec<String> utf8 = Codecs.utf8(5);
-
Method Summary
Modifier and TypeMethodDescriptiondecode(InputStream input) Decodes a value from the input stream.encode(String value, OutputStream output) Encodes the given value and writes it to the output stream.
-
Method Details
-
encode
Encodes the given value and writes it to the output stream.- Specified by:
encodein interfaceCodec<String>- Parameters:
value- the value to encodeoutput- the output stream to write the encoded bytes to- Returns:
- the encode result containing logical count and bytes written
- Throws:
IllegalArgumentException- if the value does not have exactlylengthcode pointsIOException- if an I/O error occurs during encoding
-
decode
Decodes a value from the input stream.- Specified by:
decodein interfaceCodec<String>- Parameters:
input- the input stream to read the encoded bytes from- Returns:
- the decoded value
- Throws:
EOFException- if the stream ends before the required code points are readIOException- if an I/O error occurs during decoding
-