Class HexEncoder

java.lang.Object
org.cryptacular.codec.HexEncoder
All Implemented Interfaces:
Encoder

public class HexEncoder extends Object implements Encoder
Stateless hexadecimal byte-to-character encoder.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final boolean
    Flag indicating whether to delimit every two characters with ':' as in key fingerprints, etc.
    private static final char[]
    Lowercase hex character encoding table.
    private final char[]
    Encoding table to use.
    private static final char[]
    Uppercase hex character encoding table.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new instance that does not delimit bytes in the output hex string.
    HexEncoder(boolean delimitBytes)
    Creates a new instance with optional colon-delimiting of bytes.
    HexEncoder(boolean delimitBytes, boolean uppercase)
    Creates a new instance with optional colon-delimiting of bytes and uppercase output.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    encode(ByteBuffer input, CharBuffer output)
    Encodes bytes in input buffer into characters placed in the output buffer.
    void
    Performs final output encoding (e.g.
    private static void
    initTable(String charset, char[] table)
    Initializes the encoding table for the given character set.
    int
    outputSize(int inputSize)
    Expected number of characters in the output buffer for an input buffer of the given size.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • LC_ENCODING_TABLE

      private static final char[] LC_ENCODING_TABLE
      Lowercase hex character encoding table.
    • UC_ENCODING_TABLE

      private static final char[] UC_ENCODING_TABLE
      Uppercase hex character encoding table.
    • delimit

      private final boolean delimit
      Flag indicating whether to delimit every two characters with ':' as in key fingerprints, etc.
    • table

      private final char[] table
      Encoding table to use.
  • Constructor Details

    • HexEncoder

      public HexEncoder()
      Creates a new instance that does not delimit bytes in the output hex string.
    • HexEncoder

      public HexEncoder(boolean delimitBytes)
      Creates a new instance with optional colon-delimiting of bytes.
      Parameters:
      delimitBytes - True to delimit every two characters (i.e. every byte) with ':' character.
    • HexEncoder

      public HexEncoder(boolean delimitBytes, boolean uppercase)
      Creates a new instance with optional colon-delimiting of bytes and uppercase output.
      Parameters:
      delimitBytes - True to delimit every two characters (i.e. every byte) with ':' character.
      uppercase - True to output uppercase alphabetic characters, false for lowercase.
  • Method Details

    • encode

      public void encode(ByteBuffer input, CharBuffer output) throws EncodingException
      Description copied from interface: Encoder
      Encodes bytes in input buffer into characters placed in the output buffer. This method may be called multiple times, followed by Encoder.finalize(java.nio.CharBuffer) after all input bytes have been provided.
      Specified by:
      encode in interface Encoder
      Parameters:
      input - Input byte buffer.
      output - Output character buffer.
      Throws:
      EncodingException - on encoding errors.
    • finalize

      public void finalize(CharBuffer output) throws EncodingException
      Description copied from interface: Encoder
      Performs final output encoding (e.g. padding) after all input bytes have been provided.
      Specified by:
      finalize in interface Encoder
      Parameters:
      output - Output character buffer.
      Throws:
      EncodingException - on encoding errors.
    • outputSize

      public int outputSize(int inputSize)
      Description copied from interface: Encoder
      Expected number of characters in the output buffer for an input buffer of the given size.
      Specified by:
      outputSize in interface Encoder
      Parameters:
      inputSize - Size of input buffer in bytes.
      Returns:
      Minimum character buffer size required to store all encoded input bytes.
    • initTable

      private static void initTable(String charset, char[] table)
      Initializes the encoding table for the given character set.
      Parameters:
      charset - Character set.
      table - Encoding table.