Class FixedCodePointStringCodec

java.lang.Object
io.bytestreams.codec.core.FixedCodePointStringCodec
All Implemented Interfaces:
Codec<String>

public class FixedCodePointStringCodec extends Object implements Codec<String>
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 Details

    • encode

      public EncodeResult encode(String value, OutputStream output) throws IOException
      Encodes the given value and writes it to the output stream.
      Specified by:
      encode in interface Codec<String>
      Parameters:
      value - the value to encode
      output - 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 exactly length code points
      IOException - if an I/O error occurs during encoding
    • decode

      public String decode(InputStream input) throws IOException
      Decodes a value from the input stream.
      Specified by:
      decode in interface Codec<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 read
      IOException - if an I/O error occurs during decoding