java.lang.Object
net.i2p.data.Base64

public class Base64extends Object
Encodes and decodes to and from Base64 notation.

Change Log:

  • v1.3.6 - Fixed OutputStream.flush() so that 'position' is reset.
  • v1.3.5 - Added flag to turn on and off line breaks. Fixed bug in input stream where last buffer being read, if not completely full, was not returned.
  • v1.3.4 - Fixed when "improperly padded stream" error was thrown at the wrong time.
  • v1.3.3 - Fixed I/O streams which were totally messed up.

I am placing this code in the Public Domain. Do with it as you will. This software comes with no guarantees or warranties but with plenty of well-wishing instead! Please visit http://iharder.net/xmlizable periodically to check for updates or to contribute improvements.

Modified by jrandom for i2p, using safeEncode / safeDecode to create filesystem and URL safe base64 values (replacing / with ~, and + with -).

  • Field Summary

    Modifier and Type
    Field
    Description
    private static final byte[]
    The 64 valid Base64 values.
    private static final byte[]
    The 64 valid Base64 values for I2P.
    static final String
    The I2P Alphabet.
    private static final byte[]
    Translates a Base64 value to either its 6-bit reconstruction value or a negative number indicating some other meaning.
    private static final byte
    The DECODABET value of the equals sign.
    private static final byte
    The equals sign (=) as a byte.
    private static final int
    Maximum line length (76) of Base64 output.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Defeats instantiation.
  • Method Summary

    Modifier and Type
    Method
    Description
    private static void
    Base64-decodes one stream into another.
    static byte[]
    Decodes data from Base64 notation using the I2P alphabet.
    static byte[]
    decode(String s, boolean useStandardAlphabet)
    Decodes data from Base64 notation using the I2P alphabet.
    private static byte[]
    decode(String source, int off, int len)
    Decodes Base64 content in string format and returns the decoded byte array.
    private static int
    decode4to3(byte d0, byte d1, byte d2, byte d3, byte[] destination, int destOffset)
    Decodes four Base64 chars, already mapped through DECODABET, and writes the resulting bytes (up to three of them) to destination at destOffset.
    static String
    Decodes data from Base64 notation and returns it as a string.
    static String
    encode(byte[] source)
    Output will be a multiple of 4 chars, including 0-2 trailing '='.
    static String
    encode(byte[] source, boolean useStandardAlphabet)
    Output will be a multiple of 4 chars, including 0-2 trailing '='.
    static String
    encode(byte[] source, int off, int len)
    Output will be a multiple of 4 chars, including 0-2 trailing '='.
    static String
    encode(byte[] source, int off, int len, boolean useStandardAlphabet)
    Output will be a multiple of 4 chars, including 0-2 trailing '='.
    private static void
    Base64-encodes one stream into another.
    static String
    encode(String source)
    Output will be a multiple of 4 chars, including 0-2 trailing '=' As of 0.9.14, encodes the UTF-8 encoding of source.
    private static void
    encode3to4(byte[] source, int srcOffset, int numSigBytes, StringBuilder buf, byte[] alpha)
    Appends the encoding of the significant source bytes to buf, using alpha.
    private static void
    encodeBytes(byte[] source, int off, int len, boolean breakLines, StringBuilder out, byte[] alpha)
    Encodes a byte array into Base64 notation.
    private static void
    Exits 1, never returns.
    static void
    main(String[] args)
    Command-line tool for encoding and decoding Base64.
    private static byte[]
    Reads a whole stream.
    private static void
    runApp(String[] args)
    Runs the command line encoder and decoder.
    private static byte[]
    safeDecode(String source, boolean useStandardAlphabet)
    Same as decode, except from a filesystem / URL friendly set of characters, replacing / with ~, and + with -.
    private static String
    safeEncode(byte[] source, int off, int len, boolean useStandardAlphabet)
    Same as encodeBytes, except uses a filesystem / URL friendly set of characters, replacing / with ~, and + with -.
    private static byte[]
    Decodes data from Base64 notation.

    Methods inherited from class Object

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

    • MAX_LINE_LENGTH

      private static final int MAX_LINE_LENGTH
      Maximum line length (76) of Base64 output.
      See Also:
    • EQUALS_SIGN

      private static final byte EQUALS_SIGN
      The equals sign (=) as a byte.
      See Also:
    • ALPHABET

      private static final byte[] ALPHABET
      The 64 valid Base64 values.
    • ALPHABET_I2P

      public static final String ALPHABET_I2P
      The I2P Alphabet.
      See Also:
    • ALPHABET_ALT

      private static final byte[] ALPHABET_ALT
      The 64 valid Base64 values for I2P.
    • DECODABET

      private static final byte[] DECODABET
      Translates a Base64 value to either its 6-bit reconstruction value or a negative number indicating some other meaning. As of 0.9.14 this is the decoding for the I2P alphabet. See safeDecode(String, boolean).
    • DECODABET_EQUALS

      private static final byte DECODABET_EQUALS
      The DECODABET value of the equals sign. No other character maps to it, so a decoded value of -1 means the char was '='.
      See Also:
  • Constructor Details

    • Base64

      private Base64()
      Defeats instantiation.
  • Method Details

    • encode

      public static String encode(String source)
      Output will be a multiple of 4 chars, including 0-2 trailing '=' As of 0.9.14, encodes the UTF-8 encoding of source. Prior to that, used the platform's encoding.
      Parameters:
      source - the string to encode, if null will return ""
      Returns:
      the Base64 encoded string
    • encode

      public static String encode(byte[] source)
      Output will be a multiple of 4 chars, including 0-2 trailing '='.
      Parameters:
      source - the byte array to encode, if null will return ""
      Returns:
      the Base64 encoded string
    • encode

      public static String encode(byte[] source, int off, int len)
      Output will be a multiple of 4 chars, including 0-2 trailing '='.
      Parameters:
      source - the byte array to encode, if null will return ""
      off - the offset in the source array
      len - the length of data to encode
      Returns:
      the Base64 encoded string
    • encode

      public static String encode(byte[] source, boolean useStandardAlphabet)
      Output will be a multiple of 4 chars, including 0-2 trailing '='.
      Parameters:
      source - the byte array to encode, if null will return ""
      useStandardAlphabet - Warning, must be false for I2P compatibility
      Returns:
      the Base64 encoded string
    • encode

      public static String encode(byte[] source, int off, int len, boolean useStandardAlphabet)
      Output will be a multiple of 4 chars, including 0-2 trailing '='.
      Parameters:
      source - the byte array to encode, if null will return ""
      off - the offset in the source array
      len - the length of data to encode
      useStandardAlphabet - Warning, must be false for I2P compatibility
      Returns:
      the Base64 encoded string
    • decode

      public static byte[] decode(String s)
      Decodes data from Base64 notation using the I2P alphabet.

      As of 0.9.14, does not require trailing '=' if remaining bits are zero. Prior to that, trailing 1, 2, or 3 chars were ignored.

      As of 0.9.14, trailing garbage after an '=' will cause an error. Prior to that, it was ignored.

      As of 0.9.14, whitespace will cause an error. Prior to that, it was ignored.

      Parameters:
      s - Base 64 encoded string using the I2P alphabet A-Z, a-z, 0-9, -, ~
      Returns:
      the decoded data, null on error
    • decode

      public static byte[] decode(String s, boolean useStandardAlphabet)
      Decodes data from Base64 notation using the I2P alphabet.
      Parameters:
      s - the Base64 encoded string
      useStandardAlphabet - Warning, must be false for I2P compatibility
      Returns:
      the decoded data, null on error
    • main

      public static void main(String[] args)
      Command-line tool for encoding and decoding Base64.
      Parameters:
      args - a subcommand and its arguments; empty prints the help
    • runApp

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

      private static byte[] read(InputStream in) throws IOException
      Reads a whole stream.
      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
      Base64-encodes one stream into another.
      Parameters:
      in - the bytes to encode
      out - where the encoded text is written
      Throws:
      IOException - if either stream fails
    • decode

      private static void decode(InputStream in, OutputStream out) throws IOException
      Base64-decodes one stream into another.
      Parameters:
      in - the encoded text to decode
      out - where the bytes are written
      Throws:
      IOException - if either stream fails
    • help

      private static void help()
      Exits 1, never returns.
    • encode3to4

      private static void encode3to4(byte[] source, int srcOffset, int numSigBytes, StringBuilder buf, byte[] alpha)
      Appends the encoding of the significant source bytes to buf, using alpha.
      Parameters:
      source - the array containing the bytes to encode
      srcOffset - the index in source where the bytes to encode begin
      numSigBytes - number of significant bytes to encode, 1-3, any other value appends nothing
      buf - the StringBuilder the four encoded characters are appended to
      alpha - the 64 character alphabet to index, plus its '=' padding
    • safeEncode

      private static String safeEncode(byte[] source, int off, int len, boolean useStandardAlphabet)
      Same as encodeBytes, except uses a filesystem / URL friendly set of characters, replacing / with ~, and + with -.
      Parameters:
      source - the byte array to encode
      off - index in source where encoding begins
      len - number of bytes to encode
      useStandardAlphabet - true for the standard +/ alphabet, false for the I2P -~ alphabet
      Returns:
      the Base64 encoded string
    • safeDecode

      private static byte[] safeDecode(String source, boolean useStandardAlphabet)
      Same as decode, except from a filesystem / URL friendly set of characters, replacing / with ~, and + with -.
      Parameters:
      source - the encoded string to decode, may be null
      useStandardAlphabet - true if source uses the standard +/ alphabet, false for the I2P -~ alphabet
      Returns:
      the decoded data, null on error
    • encodeBytes

      private static void encodeBytes(byte[] source, int off, int len, boolean breakLines, StringBuilder out, byte[] alpha)
      Encodes a byte array into Base64 notation.
      Parameters:
      source - The data to convert
      off - Offset in array where conversion should begin
      len - Length of data to convert
      breakLines - Break lines at 80 characters or less.
      out - the StringBuilder the encoded text is appended to
      alpha - the 64 character alphabet to index
    • decode4to3

      private static int decode4to3(byte d0, byte d1, byte d2, byte d3, byte[] destination, int destOffset)
      Decodes four Base64 chars, already mapped through DECODABET, and writes the resulting bytes (up to three of them) to destination at destOffset. This method does not check that destination is large enough for destOffset + 3.
      Parameters:
      d0 - the DECODABET value of the first source char
      d1 - the DECODABET value of the second source char
      d2 - the DECODABET value of the third char, or '=' padding
      d3 - the DECODABET value of the fourth char, or '=' padding
      destination - the array to hold the conversion
      destOffset - the index where output will be put
      Returns:
      the number of decoded bytes converted 1-3, or -1 on error, never zero
    • standardDecode

      private static byte[] standardDecode(String s)
      Decodes data from Base64 notation. As of 0.9.14, this uses the I2P alphabet, so it is not "standard".
      Parameters:
      s - the string to decode
      Returns:
      the decoded data, null on error
    • decodeToString

      public static String decodeToString(String s)
      Decodes data from Base64 notation and returns it as a string. Equivalent to calling new String( decode( s ) )

      As of 0.9.14, decodes as UTF-8. Prior to that, it used the platform's encoding. For best results, decoded data should be 7 bit.

      As of 0.9.14, does not require trailing '=' if remaining bits are zero. Prior to that, trailing 1, 2, or 3 chars were ignored.

      As of 0.9.14, trailing garbage after an '=' will cause an error. Prior to that, it was ignored.

      As of 0.9.14, whitespace will cause an error. Prior to that, it was ignored.

      Parameters:
      s - the string to decode
      Returns:
      The data as a string, or null on error
    • decode

      private static byte[] decode(String source, int off, int len)
      Decodes Base64 content in string format and returns the decoded byte array. The chars are looked up in the DECODABET directly, so no UTF-8 byte[] copy of the string is made. The caller must have verified that every char is ASCII.

      As of 0.9.14, does not require trailing '=' if remaining bits are zero. Prior to that, trailing 1, 2, or 3 chars were ignored.

      As of 0.9.14, trailing garbage after an '=' will cause an error. Prior to that, it was ignored.

      As of 0.9.14, whitespace will cause an error. Prior to that, it was ignored.

      Parameters:
      source - The Base64 encoded data
      off - The offset of where to begin decoding
      len - The length of characters to decode
      Returns:
      decoded data, null on error