net.i2p.data

## Class Base64

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

-     public class Base64
      extends 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 -)
  Version:
    1.3.4

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static String` | `ALPHABET_I2P` The I2P Alphabet.

  - ### Method Summary

    All Methods Static Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static byte[]` | `decode(String s)` 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.
    `static String` | `decodeToString(String s)` 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 '='
    `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.
    `static void` | `main(String[] args)` Command-line tool for encoding and decoding Base64.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### ALPHABET_I2P

          public static final String ALPHABET_I2P

      The I2P Alphabet.
      See Also:
        Constant Field Values

  - ### Method Detail

    - #### 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

    - #### decodeToString

          public static String decodeToString(String s)

      Decodes data from Base64 notation and returns it as a string. Equivlaent 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

    - #### 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,
                                      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)

      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,
                                      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

    - #### 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

    - #### main

          public static void main(String[] args)

      Command-line tool for encoding and decoding Base64.
      Parameters:
        `args` - command-line arguments