FileDocCategorySizeDatePackage
Base64.javaAPI DocAndroid 5.1 API28599Thu Mar 12 22:22:10 GMT 2015android.util

Base64

public class Base64 extends Object
Utilities for encoding and decoding the Base64 representation of binary data. See RFCs 2045 and 3548.

Fields Summary
public static final int
DEFAULT
Default values for encoder/decoder flags.
public static final int
NO_PADDING
Encoder flag bit to omit the padding '=' characters at the end of the output (if any).
public static final int
NO_WRAP
Encoder flag bit to omit all line terminators (i.e., the output will be on one long line).
public static final int
CRLF
Encoder flag bit to indicate lines should be terminated with a CRLF pair instead of just an LF. Has no effect if {@code NO_WRAP} is specified as well.
public static final int
URL_SAFE
Encoder/decoder flag bit to indicate using the "URL and filename safe" variant of Base64 (see RFC 3548 section 4) where {@code -} and {@code _} are used in place of {@code +} and {@code /}.
public static final int
NO_CLOSE
Flag to pass to {@link Base64OutputStream} to indicate that it should not close the output stream it is wrapping when it itself is closed.
Constructors Summary
private Base64()

 
Methods Summary
public static byte[]decode(java.lang.String str, int flags)
Decode the Base64-encoded data in input and return the data in a new byte array.

The padding '=' characters at the end are considered optional, but if any are present, there must be the correct number of them.

param
str the input String to decode, which is converted to bytes using the default charset
param
flags controls certain features of the decoded output. Pass {@code DEFAULT} to decode standard Base64.
throws
IllegalArgumentException if the input contains incorrect padding


                                                                                                                            
                  

                                          
            
    

    //  --------------------------------------------------------
    //  decoding
    //  --------------------------------------------------------

                                                                                                                       
           
        return decode(str.getBytes(), flags);
    
public static byte[]decode(byte[] input, int flags)
Decode the Base64-encoded data in input and return the data in a new byte array.

The padding '=' characters at the end are considered optional, but if any are present, there must be the correct number of them.

param
input the input array to decode
param
flags controls certain features of the decoded output. Pass {@code DEFAULT} to decode standard Base64.
throws
IllegalArgumentException if the input contains incorrect padding

        return decode(input, 0, input.length, flags);
    
public static byte[]decode(byte[] input, int offset, int len, int flags)
Decode the Base64-encoded data in input and return the data in a new byte array.

The padding '=' characters at the end are considered optional, but if any are present, there must be the correct number of them.

param
input the data to decode
param
offset the position within the input array at which to start
param
len the number of bytes of input to decode
param
flags controls certain features of the decoded output. Pass {@code DEFAULT} to decode standard Base64.
throws
IllegalArgumentException if the input contains incorrect padding

        // Allocate space for the most data the input could represent.
        // (It could contain less if it contains whitespace, etc.)
        Decoder decoder = new Decoder(flags, new byte[len*3/4]);

        if (!decoder.process(input, offset, len, true)) {
            throw new IllegalArgumentException("bad base-64");
        }

        // Maybe we got lucky and allocated exactly enough output space.
        if (decoder.op == decoder.output.length) {
            return decoder.output;
        }

        // Need to shorten the array, so allocate a new one of the
        // right size and copy.
        byte[] temp = new byte[decoder.op];
        System.arraycopy(decoder.output, 0, temp, 0, decoder.op);
        return temp;
    
public static byte[]encode(byte[] input, int flags)
Base64-encode the given data and return a newly allocated byte[] with the result.

param
input the data to encode
param
flags controls certain features of the encoded output. Passing {@code DEFAULT} results in output that adheres to RFC 2045.

        return encode(input, 0, input.length, flags);
    
public static byte[]encode(byte[] input, int offset, int len, int flags)
Base64-encode the given data and return a newly allocated byte[] with the result.

param
input the data to encode
param
offset the position within the input array at which to start
param
len the number of bytes of input to encode
param
flags controls certain features of the encoded output. Passing {@code DEFAULT} results in output that adheres to RFC 2045.

        Encoder encoder = new Encoder(flags, null);

        // Compute the exact length of the array we will produce.
        int output_len = len / 3 * 4;

        // Account for the tail of the data and the padding bytes, if any.
        if (encoder.do_padding) {
            if (len % 3 > 0) {
                output_len += 4;
            }
        } else {
            switch (len % 3) {
                case 0: break;
                case 1: output_len += 2; break;
                case 2: output_len += 3; break;
            }
        }

        // Account for the newlines, if any.
        if (encoder.do_newline && len > 0) {
            output_len += (((len-1) / (3 * Encoder.LINE_GROUPS)) + 1) *
                (encoder.do_cr ? 2 : 1);
        }

        encoder.output = new byte[output_len];
        encoder.process(input, offset, len, true);

        assert encoder.op == output_len;

        return encoder.output;
    
public static java.lang.StringencodeToString(byte[] input, int flags)
Base64-encode the given data and return a newly allocated String with the result.

param
input the data to encode
param
flags controls certain features of the encoded output. Passing {@code DEFAULT} results in output that adheres to RFC 2045.

        try {
            return new String(encode(input, flags), "US-ASCII");
        } catch (UnsupportedEncodingException e) {
            // US-ASCII is guaranteed to be available.
            throw new AssertionError(e);
        }
    
public static java.lang.StringencodeToString(byte[] input, int offset, int len, int flags)
Base64-encode the given data and return a newly allocated String with the result.

param
input the data to encode
param
offset the position within the input array at which to start
param
len the number of bytes of input to encode
param
flags controls certain features of the encoded output. Passing {@code DEFAULT} results in output that adheres to RFC 2045.

        try {
            return new String(encode(input, offset, len, flags), "US-ASCII");
        } catch (UnsupportedEncodingException e) {
            // US-ASCII is guaranteed to be available.
            throw new AssertionError(e);
        }