Class AbstractBaseNEncoder

java.lang.Object
org.cryptacular.codec.AbstractBaseNEncoder
All Implemented Interfaces:
Encoder
Direct Known Subclasses:
Base32Encoder, Base64Encoder

public abstract class AbstractBaseNEncoder extends Object implements Encoder
Base encoder class for encoding schemes described in RFC 3548.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final int
    Number of bits encoding a single character.
    private long
    Holds a block of bytes to encode.
    private final int
    Number of bits in a block.
    private final char[]
    Encoding character set.
    private final long
    Initial bit mask for selecting characters in a block.
    protected final int
    Number of base64 characters per line.
    private static final String
    Platform-specific line terminator string, e.g.
    private int
    Number of characters written.
    private final boolean
    Flag indicating whether output is padded.
    private int
    Number of bits in encode block remaining.
  • Constructor Summary

    Constructors
    Constructor
    Description
    AbstractBaseNEncoder(char[] characterSet, int charactersPerLine)
    Creates a new instance with given parameters.
    AbstractBaseNEncoder(char[] charset, int charactersPerLine, boolean paddedOutput)
    Creates a new instance with given parameters.
  • 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.
    protected static char[]
    encodingTable(String alphabet, int n)
    Converts the given alphabet into a base-N encoding table.
    void
    Performs final output encoding (e.g.
    protected abstract int
     
    protected abstract int
     
    boolean
     
    int
    outputSize(int inputSize)
    Expected number of characters in the output buffer for an input buffer of the given size.
    private void
    writeOutput(CharBuffer output, int stop)
    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

    • NEWLINE

      private static final String NEWLINE
      Platform-specific line terminator string, e.g. LF (Unix), CRLF (Windows).
    • lineLength

      protected final int lineLength
      Number of base64 characters per line.
    • charset

      private final char[] charset
      Encoding character set.
    • blockLength

      private final int blockLength
      Number of bits in a block.
    • bitsPerChar

      private final int bitsPerChar
      Number of bits encoding a single character.
    • initialBitMask

      private final long initialBitMask
      Initial bit mask for selecting characters in a block.
    • paddedOutput

      private final boolean paddedOutput
      Flag indicating whether output is padded. True by default.
    • block

      private long block
      Holds a block of bytes to encode.
    • remaining

      private int remaining
      Number of bits in encode block remaining.
    • outCount

      private int outCount
      Number of characters written.
  • Constructor Details

    • AbstractBaseNEncoder

      public AbstractBaseNEncoder(char[] characterSet, int charactersPerLine)
      Creates a new instance with given parameters.
      Parameters:
      characterSet - Encoding character set.
      charactersPerLine - Number of characters per line.
    • AbstractBaseNEncoder

      public AbstractBaseNEncoder(char[] charset, int charactersPerLine, boolean paddedOutput)
      Creates a new instance with given parameters.
      Parameters:
      charset - Encoding character set.
      charactersPerLine - Number of characters per line.
      paddedOutput - True to enable padded output, false otherwise.
  • Method Details

    • isPaddedOutput

      public boolean isPaddedOutput()
      Returns:
      True if padded output is enabled (default), false otherwise.
    • 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.
    • 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.
    • encodingTable

      protected static char[] encodingTable(String alphabet, int n)
      Converts the given alphabet into a base-N encoding table.
      Parameters:
      alphabet - Encoding alphabet to use.
      n - Encoding base.
      Returns:
      Encoding table of N elements.
    • writeOutput

      private void writeOutput(CharBuffer output, int stop)
      Writes bytes in the current encoding block to the output buffer.
      Parameters:
      output - Output buffer.
      stop - Bit shift stop position where data in current encoding block ends.