java.lang.Object
net.i2p.data.Base32

public class Base32extends Object
Encodes and decodes to and from Base32 notation. Ref: RFC 3548

Don't bother with '=' padding characters on encode or accept them on decode (i.e. don't require 5-character groups). No whitespace allowed.

Decode accepts upper or lower case.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final char[]
    The 32 valid Base32 values.
    private static final byte
    Value marking an invalid encoding character.
    private static final byte[]
    Translates a Base32 value to either its 5-bit reconstruction value or a negative number indicating some other meaning.
    private static final byte[]
    Bit masks for reassembling bytes from groups.
    private static final byte[]
    Bit masks for pulling groups out of a byte.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Defeats instantiation.
  • Method Summary

    Modifier and Type
    Method
    Description
    private static byte[]
    decode(byte[] source)
    Decodes Base32 content in byte array format and returns the decoded byte array.
    private static void
    Writes the base32 text decoded.
    static byte[]
    Case-insensitive.
    static String
    Decodes data from Base32 notation and returns it as a string.
    static String
    encode(byte[] source)
    Returns lower case.
    private static void
    Writes the stream base32-encoded.
    static String
    encode(String source)
    Returns lower case.
    private static void
    encodeBytes(byte[] source, StringBuilder out)
    Encodes a byte array into Base32 notation.
    private static void
    Prints the command-line usage.
    static void
    main(String[] args)
    Run the Base32 command-line utility.
    private static byte[]
    Reads a stream fully.
    private static void
    runApp(String[] args)
    Runs the command-line encoder or decoder.

    Methods inherited from class Object

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

    • ALPHABET

      private static final char[] ALPHABET
      The 32 valid Base32 values.
    • DECODABET

      private static final byte[] DECODABET
      Translates a Base32 value to either its 5-bit reconstruction value or a negative number indicating some other meaning. Allow upper or lower case.
    • BAD_ENCODING

      private static final byte BAD_ENCODING
      Value marking an invalid encoding character.
      See Also:
    • emask

      private static final byte[] emask
      Bit masks for pulling groups out of a byte.
    • dmask

      private static final byte[] dmask
      Bit masks for reassembling bytes from groups.
  • Constructor Details

    • Base32

      private Base32()
      Defeats instantiation.
  • Method Details

    • main

      public static void main(String[] args)
      Run the Base32 command-line utility.
      Parameters:
      args - the subcommand and its operands, as documented by the usage message printed when args is empty
    • runApp

      private static void runApp(String[] args)
      Runs the command-line encoder or decoder.
      Parameters:
      args - the command-line arguments
    • read

      private static byte[] read(InputStream in) throws IOException
      Reads a stream fully.
      Parameters:
      in - the stream to read
      Returns:
      the bytes read
      Throws:
      IOException - if the stream cannot be read
    • encode

      private static void encode(InputStream in, OutputStream out) throws IOException
      Writes the stream base32-encoded.
      Parameters:
      in - the bytes to encode
      out - where the text is written
      Throws:
      IOException - if either stream fails
    • decode

      private static void decode(InputStream in, OutputStream out) throws IOException
      Writes the base32 text decoded.
      Parameters:
      in - the text to decode
      out - where the bytes are written
      Throws:
      IOException - if either stream fails
    • help

      private static void help()
      Prints the command-line usage.
    • encode

      public static String encode(String source)
      Returns lower case. Does not add trailing '='.
      Parameters:
      source - if null will return ""
      Returns:
      the Base32-encoded string, or "" if source is null
    • encode

      public static String encode(byte[] source)
      Returns lower case. Does not add trailing '='.
      Parameters:
      source - The data to convert non-null
      Returns:
      the Base32-encoded string
    • encodeBytes

      private static void encodeBytes(byte[] source, StringBuilder out)
      Encodes a byte array into Base32 notation.
      Parameters:
      source - The data to convert non-null
      out - the output buffer
    • decodeToString

      public static String decodeToString(String s)
      Decodes data from Base32 notation and returns it as a string. Case-insensitive. Does not allow trailing '='.
      Parameters:
      s - the string to decode, if null returns null
      Returns:
      The data as a string or null on failure
    • decode

      public static byte[] decode(String s)
      Case-insensitive. Does not allow trailing '='.
      Parameters:
      s - non-null
      Returns:
      decoded data, null on error
    • decode

      private static byte[] decode(byte[] source)
      Decodes Base32 content in byte array format and returns the decoded byte array.
      Parameters:
      source - The Base32 encoded data non-null
      Returns:
      decoded data, null on error