Class Base64


  • public class Base64
    extends java.lang.Object
    Base64 provides Base64 encoding/decoding of strings and streams.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static long calcEncodedLength​(long dataLength)
      Calculates the size (i.e.
      static void decode​(char[] chars, int off, int len, java.io.OutputStream out)
      Decode base64 encoded data.
      static void decode​(char[] chars, java.io.OutputStream out)
      Decode base64 encoded data.
      static void decode​(java.io.InputStream in, java.io.OutputStream out)
      Decode base64 encoded data.
      static void decode​(java.io.Reader reader, java.io.OutputStream out)
      Decode base64 encoded data.
      static java.lang.String decode​(java.lang.String data)
      Decodes a base64-encoded string using the UTF-8 character encoding.
      static void decode​(java.lang.String data, java.io.OutputStream out)
      Decode base64 encoded data.
      static java.lang.String decodeIfEncoded​(java.lang.String data)
      Decodes a base64-encoded string marked by a "{base64}" prefix.
      static java.lang.String decodeOrEncode​(java.lang.String data)
      Base64-decodes or -encodes the given string, depending on whether or not it contains a "{base64}" prefix.
      static void encode​(byte[] data, int off, int len, java.io.Writer writer)
      Outputs base64 representation of the specified data to a Writer.
      static void encode​(java.io.InputStream in, java.io.OutputStream out)
      Outputs base64 representation of the specified stream data to an OutputStream.
      static void encode​(java.io.InputStream in, java.io.Writer writer)
      Outputs base64 representation of the specified stream data to a Writer.
      static java.lang.String encode​(java.lang.String data)
      Returns the base64 representation of UTF-8 encoded string.
      static long guessDecodedLength​(long encLength)
      Pessimistically guesses the size (i.e.
      static void main​(java.lang.String[] args)
      Base64-decodes or -encodes (see decodeOrEncode(String) all the given arguments and prints the results on separate lines in standard output.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • main

        public static void main​(java.lang.String[] args)
        Base64-decodes or -encodes (see decodeOrEncode(String) all the given arguments and prints the results on separate lines in standard output.
        Parameters:
        args - command line arguments to be decoded or encoded
        Since:
        Apache Jackrabbit 2.3
      • decodeOrEncode

        public static java.lang.String decodeOrEncode​(java.lang.String data)
        Base64-decodes or -encodes the given string, depending on whether or not it contains a "{base64}" prefix. If the string gets encoded, the "{base64}" prefix is added to it.
        Parameters:
        data - string to be decoded or encoded
        Returns:
        decoded or encoded string
        Since:
        Apache Jackrabbit 2.3
      • decodeIfEncoded

        public static java.lang.String decodeIfEncoded​(java.lang.String data)
        Decodes a base64-encoded string marked by a "{base64}" prefix. If the prefix is not found, then the string is returned as-is. If the given string is null, then null is returned.
        Parameters:
        data - string to be decoded, can be null
        Returns:
        the given string, possibly decoded
        Since:
        Apache Jackrabbit 2.3
      • calcEncodedLength

        public static long calcEncodedLength​(long dataLength)
        Calculates the size (i.e. number of bytes) of the base64 encoded output given the length (i.e. number of bytes) of the data to be encoded.
        Parameters:
        dataLength - length (i.e. number of bytes) of the data to be encoded
        Returns:
        size (i.e. number of bytes) of the base64 encoded output
      • guessDecodedLength

        public static long guessDecodedLength​(long encLength)
        Pessimistically guesses the size (i.e. number of bytes) of the decoded output given the length (i.e. number of bytes) of the base64 encoded data.
        Parameters:
        encLength - length (i.e. number of bytes) of the base64 encoded data
        Returns:
        size (i.e. number of bytes) of the decoded output
      • encode

        public static void encode​(java.io.InputStream in,
                                  java.io.Writer writer)
                           throws java.io.IOException
        Outputs base64 representation of the specified stream data to a Writer.
        Parameters:
        in - stream data to be encoded
        writer - writer to output the encoded data
        Throws:
        java.io.IOException - if an i/o error occurs
      • encode

        public static void encode​(java.io.InputStream in,
                                  java.io.OutputStream out)
                           throws java.io.IOException
        Outputs base64 representation of the specified stream data to an OutputStream.
        Parameters:
        in - stream data to be encoded
        out - stream where the encoded data should be written to
        Throws:
        java.io.IOException - if an i/o error occurs
      • encode

        public static void encode​(byte[] data,
                                  int off,
                                  int len,
                                  java.io.Writer writer)
                           throws java.io.IOException
        Outputs base64 representation of the specified data to a Writer.
        Parameters:
        data - data to be encoded
        off - offset within data at which to start encoding
        len - length of data to encode
        writer - writer to output the encoded data
        Throws:
        java.io.IOException - if an i/o error occurs
      • encode

        public static java.lang.String encode​(java.lang.String data)
        Returns the base64 representation of UTF-8 encoded string.
        Parameters:
        data - the string to be encoded
        Returns:
        base64-encoding of the string
        Since:
        Apache Jackrabbit 2.3
      • decode

        public static java.lang.String decode​(java.lang.String data)
        Decodes a base64-encoded string using the UTF-8 character encoding. The given string is returned as-is if it doesn't contain a valid base64 encoding.
        Parameters:
        data - the base64-encoded data to be decoded
        Returns:
        decoded string
        Since:
        Apache Jackrabbit 2.3
      • decode

        public static void decode​(java.io.Reader reader,
                                  java.io.OutputStream out)
                           throws java.io.IOException
        Decode base64 encoded data.
        Parameters:
        reader - reader for the base64 encoded data to be decoded
        out - stream where the decoded data should be written to
        Throws:
        java.io.IOException - if an i/o error occurs
      • decode

        public static void decode​(java.io.InputStream in,
                                  java.io.OutputStream out)
                           throws java.io.IOException
        Decode base64 encoded data. The data read from the inputstream is assumed to be of charset "US-ASCII".
        Parameters:
        in - inputstream of the base64 encoded data to be decoded
        out - stream where the decoded data should be written to
        Throws:
        java.io.IOException - if an i/o error occurs
      • decode

        public static void decode​(java.lang.String data,
                                  java.io.OutputStream out)
                           throws java.io.IOException
        Decode base64 encoded data.
        Parameters:
        data - the base64 encoded data to be decoded
        out - stream where the decoded data should be written to
        Throws:
        java.io.IOException - if an i/o error occurs
      • decode

        public static void decode​(char[] chars,
                                  java.io.OutputStream out)
                           throws java.io.IOException
        Decode base64 encoded data.
        Parameters:
        chars - the base64 encoded data to be decoded
        out - stream where the decoded data should be written to
        Throws:
        java.io.IOException - if an i/o error occurs
      • decode

        public static void decode​(char[] chars,
                                  int off,
                                  int len,
                                  java.io.OutputStream out)
                           throws java.io.IOException
        Decode base64 encoded data.
        Parameters:
        chars - the base64 encoded data to be decoded
        off - offset within data at which to start decoding
        len - length of data to decode
        out - stream where the decoded data should be written to
        Throws:
        java.io.IOException - if an i/o error occurs