public final class Base64
extends java.lang.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 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.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.
Modifier and Type | Method and Description |
---|---|
static byte[] |
decode(byte[] source,
int off,
int len,
boolean urlSafe)
Descodifica datos en Base64.
|
static byte[] |
decode(java.lang.String str)
Descodifica datos en Base64.
|
static byte[] |
decode(java.lang.String str,
boolean urlSafe)
Descodifica datos en Base64.
|
static java.lang.String |
encode(byte[] source)
Codifica un binario en Base64.
|
static java.lang.String |
encode(byte[] source,
boolean urlSafe)
Codifica un binario en Base64.
|
public static java.lang.String encode(byte[] source)
source
- Datos a convertir a Base64public static java.lang.String encode(byte[] source, boolean urlSafe)
source
- Datos a convertir a Base64urlSafe
- Si se establece a true
indica que los datos se codificarán con un alfabeto Base64
susceptible de ser usado en URL, según se indica en la secctión 4 de la RFC3548,
si se establece a false
los datos se codificarán en Base64 normalpublic static byte[] decode(byte[] source, int off, int len, boolean urlSafe) throws java.io.IOException
source
- Datos codificados en Base64.off
- El índice inicial por el que empezar a descodificar.len
- Número de caracteres que descodificar.urlSafe
- Si se establece a true
indica que los datos están con un alfabeto Base64
susceptible de ser usado en URL, según se indica en la secctión 4 de la RFC3548,
si se establece a false
los datos deben estar en Base64 normaljava.io.IOException
- si ocurre cualquier errorpublic static byte[] decode(java.lang.String str) throws java.io.IOException
str
- Cadena de caracteres en formato Base64java.io.IOException
- si ocurre cualquier errorpublic static byte[] decode(java.lang.String str, boolean urlSafe) throws java.io.IOException
str
- Cadena de caracteres en formato Base64urlSafe
- Si se establece a true
indica que los datos están con un alfabeto Base64
susceptible de ser usado en URL, según se indica en la secctión 4 de la RFC3548,
si se establece a false
los datos deben estar en Base64 normaljava.io.IOException
- si ocurre cualquier error