Class Encoder

java.lang.Object
org.aesh.terminal.io.Encoder
All Implemented Interfaces:
Consumer<int[]>

public class Encoder extends Object implements Consumer<int[]>
Encodes int arrays (unicode code points) to byte arrays using a specified charset.
Author:
Ståle W. Pedersen
  • Constructor Summary

    Constructors
    Constructor
    Description
    Encoder(Charset charset, Consumer<byte[]> out)
    Create a new Encoder with the specified charset and output consumer.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    accept(int[] input)
    Encode an array of Unicode code points and send the resulting bytes to the output consumer.
    void
    Encode a String directly to bytes, bypassing the int[] intermediary.
    void
    Set the charset to use for encoding.
    static CharBuffer
    toCharBuffer(int[] input)
    Convert an array of Unicode code points to a CharBuffer.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface java.util.function.Consumer

    andThen
  • Constructor Details

    • Encoder

      public Encoder(Charset charset, Consumer<byte[]> out)
      Create a new Encoder with the specified charset and output consumer.
      Parameters:
      charset - the charset to use for encoding, or null for the default charset
      out - the consumer to receive encoded byte arrays
  • Method Details

    • setCharset

      public void setCharset(Charset charset)
      Set the charset to use for encoding.
      Parameters:
      charset - the charset to use, ignored if null
    • accept

      public void accept(int[] input)
      Encode an array of Unicode code points and send the resulting bytes to the output consumer.
      Specified by:
      accept in interface Consumer<int[]>
      Parameters:
      input - the code points to encode
    • accept

      public void accept(String s)
      Encode a String directly to bytes, bypassing the int[] intermediary. For UTF-8 with ASCII-only strings (the common case for ANSI sequences), this converts directly from String chars to byte[] in a single pass.
      Parameters:
      s - the string to encode
    • toCharBuffer

      public static CharBuffer toCharBuffer(int[] input)
      Convert an array of Unicode code points to a CharBuffer. Handles surrogate pairs for code points outside the Basic Multilingual Plane.
      Parameters:
      input - the code points to convert
      Returns:
      a CharBuffer containing the character representation