Class AbstractBaseNDecoder

java.lang.Object
org.cryptacular.codec.AbstractBaseNDecoder
All Implemented Interfaces:
Decoder
Direct Known Subclasses:
Base32Decoder, Base64Decoder

public abstract class AbstractBaseNDecoder extends Object implements Decoder
Base decoder class for encoding schemes described in RFC 3548.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final char[]
    Block of encoded characters.
    private int
    Current position in character block.
    private final byte[]
    Decoding table.
    private final boolean
    Flag indicating whether input is padded.
  • Constructor Summary

    Constructors
    Constructor
    Description
    AbstractBaseNDecoder(byte[] decodingTable)
    Creates a new instance with given parameters.
    AbstractBaseNDecoder(byte[] decodingTable, boolean paddedInput)
    Creates a new instance with given parameters.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    decode(CharBuffer input, ByteBuffer output)
    Decodes characters in input buffer into bytes placed in the output buffer.
    protected static byte[]
    decodingTable(String alphabet, int n)
    Converts the given alphabet into a base-N decoding table.
    void
    Performs final output decoding (e.g.
    protected abstract int
     
    protected abstract int
     
    boolean
     
    int
    outputSize(int inputSize)
    Expected number of bytes in the output buffer for an input buffer of the given size.
    private void
    writeOutput(ByteBuffer output, int len)
    Writes bytes in the current encoding block to the output buffer.

    Methods inherited from class java.lang.Object

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

    • block

      private final char[] block
      Block of encoded characters.
    • decodingTable

      private final byte[] decodingTable
      Decoding table.
    • paddedInput

      private final boolean paddedInput
      Flag indicating whether input is padded. True by default.
    • blockPos

      private int blockPos
      Current position in character block.
  • Constructor Details

    • AbstractBaseNDecoder

      public AbstractBaseNDecoder(byte[] decodingTable)
      Creates a new instance with given parameters.
      Parameters:
      decodingTable - Byte array indexed by characters in the character set encoding.
    • AbstractBaseNDecoder

      public AbstractBaseNDecoder(byte[] decodingTable, boolean paddedInput)
      Creates a new instance with given parameters.
      Parameters:
      decodingTable - Byte array indexed by characters in the character set encoding.
      paddedInput - True to enable support for padded input, false otherwise.
  • Method Details

    • isPaddedInput

      public boolean isPaddedInput()
      Returns:
      True if padded input is accepted (default), false otherwise.
    • decode

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

      public void finalize(ByteBuffer output) throws EncodingException
      Description copied from interface: Decoder
      Performs final output decoding (e.g. padding) after all input characters have been provided.
      Specified by:
      finalize in interface Decoder
      Parameters:
      output - Output byte buffer.
      Throws:
      EncodingException - on decoding errors.
    • outputSize

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

      protected abstract int getBlockLength()
      Returns:
      Number of bits in a block of encoded characters.
    • getBitsPerChar

      protected abstract int getBitsPerChar()
      Returns:
      Number of bits encoding a single character.
    • decodingTable

      protected static byte[] decodingTable(String alphabet, int n)
      Converts the given alphabet into a base-N decoding table.
      Parameters:
      alphabet - Decoding alphabet to use.
      n - Encoding base.
      Returns:
      Decoding table of 128 elements.
    • writeOutput

      private void writeOutput(ByteBuffer output, int len)
      Writes bytes in the current encoding block to the output buffer.
      Parameters:
      output - Output buffer.
      len - Number of characters to decode in current block.