Class Base32Codec

java.lang.Object
com.helger.base.codec.impl.Base32Codec
All Implemented Interfaces:
IByteArrayCodec, IByteArrayDecoder, IByteArrayEncoder, IByteArrayStreamDecoder, IByteArrayStreamEncoder, ICodec<byte[]>, IDecoder<byte[],byte[]>, IEncoder<byte[],byte[]>

public class Base32Codec extends Object implements IByteArrayCodec
Base32 encoder and decoder based on Apache Commons Codec Base32. Defined in RFC 4648. BASE32 characters are 5 bits in length. They are formed by taking a block of five octets to form a 40-bit string, which is converted into eight BASE32 characters.
RFC 3548 and defines only the "regular encoding". RFC 4648 adds the "hex encoding". So when using the "regular encoding" it is compliant to both RFCs.
Source: https://tools.ietf.org/html/rfc4648
Source: https://tools.ietf.org/html/rfc3548
Author:
Philip Helger
  • Constructor Details

    • Base32Codec

      public Base32Codec()
      Creates a Base32 codec used for decoding and encoding.
    • Base32Codec

      public Base32Codec(boolean bUseHex)
      Creates a Base32 codec used for decoding and encoding.
      Parameters:
      bUseHex - true to use hex encoding, false to use regular encoding.
  • Method Details

    • isHexEncoding

      public boolean isHexEncoding()
      Returns:
      true if hex encoding is used, false if regular encoding is used.
    • getPad

      public byte getPad()
      Returns:
      The byte used for padding. Defaults to '='.
    • setPad

      public @NonNull Base32Codec setPad(byte nPad)
      Set the byte to use for padding.
      Parameters:
      nPad - The padding byte to use. Must not be in the alphabet or whitespace.
      Returns:
      this for chaining
    • isAddPadding

      public boolean isAddPadding()
      Returns:
      true if padding is added during encoding, false otherwise.
    • setAddPaddding

      public @NonNull Base32Codec setAddPaddding(boolean bAddPadding)
      Set whether to add padding during encoding.
      Parameters:
      bAddPadding - true to add padding, false to omit it.
      Returns:
      this for chaining
    • getMaximumEncodedLength

      public int getMaximumEncodedLength(int nLen)
      Description copied from interface: IByteArrayEncoder
      Get the maximum encoded length based on the provided decoded length. This is purely for performance reasons. The name of the method would be better called "getMaximumEncodedLength".
      Specified by:
      getMaximumEncodedLength in interface IByteArrayEncoder
      Specified by:
      getMaximumEncodedLength in interface IByteArrayStreamEncoder
      Parameters:
      nLen - The decoded length. Always ≥ 0.
      Returns:
      The maximum encoded length. Always ≥ 0.
    • encode

      public void encode(byte @Nullable [] aDecodedBuffer, @Nonnegative int nOfs, @Nonnegative int nLen, @WillNotClose @NonNull OutputStream aOS)
      Encode the passed decoded buffer to Base32 and write it to the output stream.
      Specified by:
      encode in interface IByteArrayStreamEncoder
      Parameters:
      aDecodedBuffer - The buffer to be encoded. May be null.
      nOfs - The offset in the buffer to start encoding from.
      nLen - The number of bytes to encode.
      aOS - The output stream to write the encoded data to. May not be null.
    • getMaximumDecodedLength

      public int getMaximumDecodedLength(int nLen)
      Description copied from interface: IByteArrayDecoder
      Get the maximum decoded length based on the provided encoded length. This is purely for performance reasons.
      Specified by:
      getMaximumDecodedLength in interface IByteArrayDecoder
      Parameters:
      nLen - The encoded length. Always ≥ 0.
      Returns:
      The maximum decoded length. Always ≥ 0.
    • decode

      public void decode(byte @Nullable [] aEncodedBuffer, @Nonnegative int nOfs, @Nonnegative int nLen, @WillNotClose @NonNull OutputStream aOS)
      Decode the passed Base32 encoded buffer and write the decoded bytes to the output stream.
      Specified by:
      decode in interface IByteArrayStreamDecoder
      Parameters:
      aEncodedBuffer - The Base32 encoded buffer to be decoded. May be null.
      nOfs - The offset in the buffer to start decoding from.
      nLen - The number of bytes to decode.
      aOS - The output stream to write the decoded data to. May not be null.