# Class: com.pnfsoftware.jeb.util.encoding.Base64

Encodes and decodes to and from Base64 notation. 

 Example: 

```

 String encoded = Base64.encode(myByteArray);
 byte[] myByteArray = Base64.decode(encoded);
 
```
 

 The `options` parameter, which appears in a few places, is used to pass several pieces of information to the encoder. In the "higher level" methods such as [#encodeBytes(byte[], int)](#encodeBytes(byte[], int)) the options parameter can be used to indicate such things as first gzipping the bytes before encoding them, not inserting linefeeds, and encoding using the URL\-safe and Ordered dialects. 

 Note, according to RFC3548, Section 2.1, implementations should not add line feeds unless explicitly told to do so. I've got Base64 set to this behavior now, although earlier versions broke lines by default. 

 The constants defined in Base64 can be OR\-ed together to combine options, so you might make a call like this: 

```

 String encoded = Base64.encodeBytes(mybytes, Base64.GZIP | Base64.DO_BREAK_LINES);
 
```
 

 to compress the data before encoding it and then making the output have newline characters.

## Static Field: AUTO_GUNZIP
Type: `int`

Constant value: `64`
Description: Automatically gunzip decoded data if a gzip header is detected. Value is 64.

## Static Field: DECODE
Type: `int`

Constant value: `0`
Description: Specify decoding in first bit. Value is zero.

## Static Field: DONT_GUNZIP
Type: `int`

Constant value: `4`
Description: Legacy option: specify that gzipped data should *not* be automatically gunzipped.

## Static Field: DO_BREAK_LINES
Type: `int`

Constant value: `8`
Description: Do break lines when encoding. Value is 8.

## Static Field: ENCODE
Type: `int`

Constant value: `1`
Description: Specify encoding in first bit. Value is one.

## Static Field: GZIP
Type: `int`

Constant value: `2`
Description: Specify that data should be gzip\-compressed in second bit. Value is two.

## Static Field: NO_OPTIONS
Type: `int`

Constant value: `0`
Description: No options specified. Value is zero.

## Static Field: ORDERED
Type: `int`

Constant value: `32`
Description: Encode using the special "ordered" dialect of Base64 described here: http://www.faqs.org/qa/rfcc\-1940.html.

## Static Field: URL_SAFE
Type: `int`

Constant value: `16`
Description: Encode using Base64\-like encoding that is URL\- and Filename\-safe as described in Section 4 of RFC3548: http://www.faqs.org/rfcs/rfc3548.html. It is important to note that data encoded this way is *not* officially valid Base64, or at the very least should not be called Base64 without also specifying that is was encoded using the URL\- and Filename\-safe dialect.

## Static Method: decode
- parameter: `source`, type: `byte[]`
- return type: `byte[]`

Description: Low\-level access to decoding ASCII characters in the form of a byte array. **Ignores [#AUTO_GUNZIP](#AUTO_GUNZIP), if it's set.** This is not generally a recommended method, although it is used internally as part of the decoding process. Special case: if len = 0, an empty array is returned. Still, if you need more speed and reduced memory footprint \(and aren't gzipping\), consider this method.
parameter: source: The Base64 encoded data
return: decoded data
throws: if the encoded data cannot be decoded

## Static Method: decode
- parameter: `source`, type: `byte[]`
- parameter: `off`, type: `int`
- parameter: `len`, type: `int`
- parameter: `options`, type: `int`
- return type: `byte[]`

Description: Low\-level access to decoding ASCII characters in the form of a byte array. **Ignores [#AUTO_GUNZIP](#AUTO_GUNZIP), if it's set.** This is not generally a recommended method, although it is used internally as part of the decoding process. Special case: if len = 0, an empty array is returned. Still, if you need more speed and reduced memory footprint \(and aren't gzipping\), consider this method.
parameter: source: The Base64 encoded data
parameter: off: The offset of where to begin decoding
parameter: len: The length of characters to decode
parameter: options: Can specify options such as alphabet type to use
return: decoded data
throws: If bogus characters exist in source data

## Static Method: decode
- parameter: `s`, type: `java.lang.String`
- return type: `byte[]`

Description: Decodes data from Base64 notation.
parameter: s: the string to decode
return: the decoded data
throws: If there is a problem

## Static Method: decode
- parameter: `s`, type: `java.lang.String`
- parameter: `options`, type: `int`
- return type: `byte[]`

Description: Decodes data from Base64 notation. If the [#AUTO_GUNZIP](#AUTO_GUNZIP) option is set, gzip\-compressed data will be automatically detected and decompressed unless [#DONT_GUNZIP](#DONT_GUNZIP) is also set.
parameter: s: the string to decode
parameter: options: encode options such as URL\_SAFE
return: the decoded data
throws: if there is an error
throws: if `s` is null

## Static Method: decodeFileToFile
- parameter: `infile`, type: `java.lang.String`
- parameter: `outfile`, type: `java.lang.String`

Description: Reads `infile` and decodes it to `outfile`.
parameter: infile: Input file
parameter: outfile: Output file
throws: if there is an error

## Static Method: decodeFromFile
- parameter: `filename`, type: `java.lang.String`
- return type: `byte[]`

Description: Convenience method for reading a base64\-encoded file and decoding it. 

 As of v 2.3, if there is a error, the method will throw an java.io.IOException. **This is new to v2.3\!** In earlier versions, it just returned false, but in retrospect that's a pretty poor way to handle it.
parameter: filename: Filename for reading encoded data
return: decoded byte array
throws: if there is an error

## Static Method: decodeToFile
- parameter: `dataToDecode`, type: `java.lang.String`
- parameter: `filename`, type: `java.lang.String`

Description: Convenience method for decoding data to a file. 

 As of v 2.3, if there is a error, the method will throw an java.io.IOException. **This is new to v2.3\!** In earlier versions, it just returned false, but in retrospect that's a pretty poor way to handle it.
parameter: dataToDecode: Base64\-encoded data as a string
parameter: filename: Filename for saving decoded data
throws: if there is an error

## Static Method: decodeToObject
- parameter: `encodedObject`, type: `java.lang.String`
- return type: `java.lang.Object`

Description: Attempts to decode Base64 data and deserialize a Java Object within. Returns `null` if there was an error.
parameter: encodedObject: The Base64 data to decode
return: The decoded and deserialized object
throws: if encodedObject is null
throws: if there is a general error
throws: if the decoded object is of a class that cannot be found by             the JVM

## Static Method: decodeToObject
- parameter: `encodedObject`, type: `java.lang.String`
- parameter: `options`, type: `int`
- parameter: `loader`, type: `java.lang.ClassLoader`
- return type: `java.lang.Object`

Description: Attempts to decode Base64 data and deserialize a Java Object within. Returns `null` if there was an error. If `loader` is not null, it will be the class loader used when deserializing.
parameter: encodedObject: The Base64 data to decode
parameter: options: Various parameters related to decoding; use [#AUTO_GUNZIP](#AUTO_GUNZIP) for gzipped            serialized objects
parameter: loader: Optional class loader to use in deserializing classes.
return: The decoded and deserialized object
throws: if encodedObject is null
throws: if there is a general error
throws: if the decoded object is of a class that cannot be found by             the JVM

## Static Method: encode
- parameter: `raw`, type: `java.nio.ByteBuffer`
- parameter: `encoded`, type: `java.nio.ByteBuffer`

Description: Performs Base64 encoding on the `raw` ByteBuffer, writing it to the `encoded` ByteBuffer. This is an experimental feature. Currently it does not pass along any options \(such as [#DO_BREAK_LINES](#DO_BREAK_LINES) or [#GZIP](#GZIP).
parameter: raw: input buffer
parameter: encoded: output buffer

## Static Method: encode
- parameter: `raw`, type: `java.nio.ByteBuffer`
- parameter: `encoded`, type: `java.nio.CharBuffer`

Description: Performs Base64 encoding on the `raw` ByteBuffer, writing it to the `encoded` CharBuffer. This is an experimental feature. Currently it does not pass along any options \(such as [#DO_BREAK_LINES](#DO_BREAK_LINES) or [#GZIP](#GZIP).
parameter: raw: input buffer
parameter: encoded: output buffer

## Static Method: encodeBytes
- parameter: `source`, type: `byte[]`
- return type: `java.lang.String`

Description: Encodes a byte array into Base64 notation. Does not GZip\-compress data.
parameter: source: The data to convert
return: The data in Base64\-encoded form
throws: if source array is null

## Static Method: encodeBytes
- parameter: `source`, type: `byte[]`
- parameter: `options`, type: `int`
- return type: `java.lang.String`

Description: Encodes a byte array into Base64 notation. 

 Example options: 

```

   GZIP: gzip-compresses object before encoding it.
   DO_BREAK_LINES: break lines at 76 characters
     *Note: Technically, this makes your encoding non-compliant.*
 
```
 

 Example: `encodeBytes( myData, Base64.GZIP )` or 

 Example: `encodeBytes( myData, Base64.GZIP | Base64.DO_BREAK_LINES )` 

 As of v 2.3, if there is an error with the GZIP stream, the method will throw an java.io.IOException. **This is new to v2.3\!** In earlier versions, it just returned a null value, but in retrospect that's a pretty poor way to handle it.
parameter: source: The data to convert
parameter: options: Specified options
return: The Base64\-encoded data as a String
see also: Base64#GZIP
see also: Base64#DO_BREAK_LINES
throws: if there is an error
throws: if source array is null

## Static Method: encodeBytes
- parameter: `source`, type: `byte[]`
- parameter: `off`, type: `int`
- parameter: `len`, type: `int`
- return type: `java.lang.String`

Description: Encodes a byte array into Base64 notation. Does not GZip\-compress data. 

 As of v 2.3, if there is an error, the method will throw an java.io.IOException. **This is new to v2.3\!** In earlier versions, it just returned a null value, but in retrospect that's a pretty poor way to handle it.
parameter: source: The data to convert
parameter: off: Offset in array where conversion should begin
parameter: len: Length of data to convert
return: The Base64\-encoded data as a String
throws: if source array is null
throws: if source array, offset, or length are invalid

## Static Method: encodeBytes
- parameter: `source`, type: `byte[]`
- parameter: `off`, type: `int`
- parameter: `len`, type: `int`
- parameter: `options`, type: `int`
- return type: `java.lang.String`

Description: Encodes a byte array into Base64 notation. 

 Example options: 

```

   GZIP: gzip-compresses object before encoding it.
   DO_BREAK_LINES: break lines at 76 characters
     *Note: Technically, this makes your encoding non-compliant.*
 
```
 

 Example: `encodeBytes( myData, Base64.GZIP )` or 

 Example: `encodeBytes( myData, Base64.GZIP | Base64.DO_BREAK_LINES )` 

 As of v 2.3, if there is an error with the GZIP stream, the method will throw an java.io.IOException. **This is new to v2.3\!** In earlier versions, it just returned a null value, but in retrospect that's a pretty poor way to handle it.
parameter: source: The data to convert
parameter: off: Offset in array where conversion should begin
parameter: len: Length of data to convert
parameter: options: Specified options
return: The Base64\-encoded data as a String
see also: Base64#GZIP
see also: Base64#DO_BREAK_LINES
throws: if there is an error
throws: if source array is null
throws: if source array, offset, or length are invalid

## Static Method: encodeBytesToBytes
- parameter: `source`, type: `byte[]`
- return type: `byte[]`

Description: Similar to [#encodeBytes(byte[])](#encodeBytes(byte[])) 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.
parameter: source: The data to convert
return: The Base64\-encoded data as a byte\[\] \(of ASCII characters\)
throws: if source array is null

## Static Method: encodeBytesToBytes
- parameter: `source`, type: `byte[]`
- parameter: `off`, type: `int`
- parameter: `len`, type: `int`
- parameter: `options`, type: `int`
- return type: `byte[]`

Description: Similar to [#encodeBytes(byte[], int, int, int)](#encodeBytes(byte[], int, 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.
parameter: source: The data to convert
parameter: off: Offset in array where conversion should begin
parameter: len: Length of data to convert
parameter: options: Specified options
return: The Base64\-encoded data as a String
see also: Base64#GZIP
see also: Base64#DO_BREAK_LINES
throws: if there is an error
throws: if source array is null
throws: if source array, offset, or length are invalid

## Static Method: encodeFileToFile
- parameter: `infile`, type: `java.lang.String`
- parameter: `outfile`, type: `java.lang.String`

Description: Reads `infile` and encodes it to `outfile`.
parameter: infile: Input file
parameter: outfile: Output file
throws: if there is an error

## Static Method: encodeFromFile
- parameter: `filename`, type: `java.lang.String`
- return type: `java.lang.String`

Description: Convenience method for reading a binary file and base64\-encoding it. 

 As of v 2.3, if there is a error, the method will throw an java.io.IOException. **This is new to v2.3\!** In earlier versions, it just returned false, but in retrospect that's a pretty poor way to handle it.
parameter: filename: Filename for reading binary data
return: base64\-encoded string
throws: if there is an error

## Static Method: encodeObject
- parameter: `serializableObject`, type: `java.io.Serializable`
- return type: `java.lang.String`

Description: Serializes an object and returns the Base64\-encoded version of that serialized object. 

 As of v 2.3, if the object cannot be serialized or there is another error, the method will throw an java.io.IOException. **This is new to v2.3\!** In earlier versions, it just returned a null value, but in retrospect that's a pretty poor way to handle it. 

 The object is not GZip\-compressed before being encoded.
parameter: serializableObject: The object to encode
return: The Base64\-encoded object
throws: if there is an error
throws: if serializedObject is null

## Static Method: encodeObject
- parameter: `serializableObject`, type: `java.io.Serializable`
- parameter: `options`, type: `int`
- return type: `java.lang.String`

Description: Serializes an object and returns the Base64\-encoded version of that serialized object. 

 As of v 2.3, if the object cannot be serialized or there is another error, the method will throw an java.io.IOException. **This is new to v2.3\!** In earlier versions, it just returned a null value, but in retrospect that's a pretty poor way to handle it. 

 The object is not GZip\-compressed before being encoded. 

 Example options: 

```

   GZIP: gzip-compresses object before encoding it.
   DO_BREAK_LINES: break lines at 76 characters
 
```
 

 Example: `encodeObject( myObj, Base64.GZIP )` or 

 Example: `encodeObject( myObj, Base64.GZIP | Base64.DO_BREAK_LINES )`
parameter: serializableObject: The object to encode
parameter: options: Specified options
return: The Base64\-encoded object
see also: Base64#GZIP
see also: Base64#DO_BREAK_LINES
throws: if there is an error

## Static Method: encodeToFile
- parameter: `dataToEncode`, type: `byte[]`
- parameter: `filename`, type: `java.lang.String`

Description: Convenience method for encoding data to a file. 

 As of v 2.3, if there is a error, the method will throw an java.io.IOException. **This is new to v2.3\!** In earlier versions, it just returned false, but in retrospect that's a pretty poor way to handle it.
parameter: dataToEncode: byte array of data to encode in base64 form
parameter: filename: Filename for saving encoded data
throws: if there is an error
throws: if dataToEncode is null

