Class Base64

java.lang.Object
org.jppf.utils.base64.Base64

public final class Base64 extends Object

Encodes and decodes to and from Base64 notation.

Homepage: http://iharder.net/base64.

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( bytes, options ) 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.

Also...

String encoded = Base64.encodeBytes( crazyString.getBytes() );

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
Author:
Robert Harder, rob@iharder.net
  • Field Details

    • NO_OPTIONS

      public static final int NO_OPTIONS
      No options specified. Value is zero.
      See Also:
    • ENCODE

      public static final int ENCODE
      Specify encoding in first bit. Value is one.
      See Also:
    • DECODE

      public static final int DECODE
      Specify decoding in first bit. Value is zero.
      See Also:
    • GZIP

      public static final int GZIP
      Specify that data should be gzip-compressed in second bit. Value is two.
      See Also:
    • DONT_GUNZIP

      public static final int DONT_GUNZIP
      Specify that gzipped data should not be automatically gunzipped.
      See Also:
    • DO_BREAK_LINES

      public static final int DO_BREAK_LINES
      Do break lines when encoding. Value is 8.
      See Also:
    • URL_SAFE

      public static final int URL_SAFE
      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.
      See Also:
    • ORDERED

      public static final int ORDERED
      Encode using the special "ordered" dialect of Base64 described here: http://www.faqs.org/qa/rfcc-1940.html.
      See Also: