Class HttpRequest.Base64

java.lang.Object
com.github.kevinsawicki.http.HttpRequest.Base64
Enclosing class:
HttpRequest

public static class HttpRequest.Base64 extends Object

Encodes and decodes to and from Base64 notation.

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/base64 periodically to check for updates or to contribute improvements.

Version:
2.3.7
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final byte[]
    The 64 valid Base64 values.
    private static final byte
    The equals sign (=) as a byte.
    private static final String
    Preferred encoding.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Defeats instantiation.
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    encode(String string)
    Encode string as a byte array in Base64 annotation.
    private static byte[]
    encode3to4(byte[] source, int srcOffset, int numSigBytes, byte[] destination, int destOffset)
    Encodes up to three bytes of the array source and writes the resulting four Base64 bytes to destination.
    static String
    encodeBytes(byte[] source)
    Encodes a byte array into Base64 notation.
    static String
    encodeBytes(byte[] source, int off, int len)
    Encodes a byte array into Base64 notation.
    static byte[]
    encodeBytesToBytes(byte[] source, int off, int len)
    Similar to encodeBytes(byte[], int, int) but returns a byte array instead of instantiating a String.

    Methods inherited from class java.lang.Object

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

    • EQUALS_SIGN

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

      private static final String PREFERRED_ENCODING
      Preferred encoding.
      See Also:
    • _STANDARD_ALPHABET

      private static final byte[] _STANDARD_ALPHABET
      The 64 valid Base64 values.
  • Constructor Details

    • Base64

      private Base64()
      Defeats instantiation.
  • Method Details

    • encode3to4

      private static byte[] encode3to4(byte[] source, int srcOffset, int numSigBytes, byte[] destination, int destOffset)

      Encodes up to three bytes of the array source and writes the resulting four Base64 bytes to destination. The source and destination arrays can be manipulated anywhere along their length by specifying srcOffset and destOffset. This method does not check to make sure your arrays are large enough to accomodate srcOffset + 3 for the source array or destOffset + 4 for the destination array. The actual number of significant bytes in your array is given by numSigBytes.

      This is the lowest level of the encoding methods with all possible parameters.

      Parameters:
      source - the array to convert
      srcOffset - the index where conversion begins
      numSigBytes - the number of significant bytes in your array
      destination - the array to hold the conversion
      destOffset - the index where output will be put
      Returns:
      the destination array
      Since:
      1.3
    • encode

      public static String encode(String string)
      Encode string as a byte array in Base64 annotation.
      Parameters:
      string -
      Returns:
      The Base64-encoded data as a string
    • encodeBytes

      public static String encodeBytes(byte[] source)
      Encodes a byte array into Base64 notation.
      Parameters:
      source - The data to convert
      Returns:
      The Base64-encoded data as a String
      Throws:
      NullPointerException - if source array is null
      IllegalArgumentException - if source array, offset, or length are invalid
      Since:
      2.0
    • encodeBytes

      public static String encodeBytes(byte[] source, int off, int len)
      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
      Returns:
      The Base64-encoded data as a String
      Throws:
      NullPointerException - if source array is null
      IllegalArgumentException - if source array, offset, or length are invalid
      Since:
      2.0
    • encodeBytesToBytes

      public static byte[] encodeBytesToBytes(byte[] source, int off, int len)
      Similar to encodeBytes(byte[], int, int) but returns a byte array instead of instantiating a String. This is more efficient if you're working with I/O streams and have large data sets to encode.
      Parameters:
      source - The data to convert
      off - Offset in array where conversion should begin
      len - Length of data to convert
      Returns:
      The Base64-encoded data as a String if there is an error
      Throws:
      NullPointerException - if source array is null
      IllegalArgumentException - if source array, offset, or length are invalid
      Since:
      2.3.1