public class Base64InputStream
extends java.io.FilterInputStream
read
methods.
Base64 is the encoding format used by Multipurpose Internet Mail Extension (Mime) for transmitting non-text material over text-only communications channels. Base64 is based on a 65-character subset of US-ASCII, enabling 6 bits to be represented per printable character (see RFC 1521).
This class may be used for decoding Base64 encoding data read in from some file, e.g.:
FileInputStream fis = new FileInputStream("test/base64.enc");
Base64InputStream base64is = new Base64InputStream(fis);
byte[] data = new byte[1024];
int r;
while ((r = base64is.read(data)) != -1) {
System.out.print(new String(data));
}
To increase the throughput, an application can consider using a
BufferedInputStream under this stream.
Base64OutputStream| Modifier and Type | Field and Description |
|---|---|
protected int[] |
decoding |
protected static int |
ERROR |
protected static int |
IGNORE |
protected static int |
NOTIFY |
| Constructor and Description |
|---|
Base64InputStream(java.io.InputStream in)
Creates a new Base64InputStream to read data from the specified input stream.
|
Base64InputStream(java.io.InputStream in,
boolean ignoreInvalidCharacters)
Creates a new Base64InputStream to read data from the specified input stream
and specifies whether or not to ignore invalid BASE-64 characters.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
markSupported()
Returns
false since mark/reset
is not supported by this input stream. |
protected void |
notify(byte[] buffer)
This method is called if a character specified with
NOTIFY is encountered. |
int |
read()
Returns the next BASE64 decoded byte, read from the underlying input stream.
|
int |
read(byte[] b,
int off,
int len)
Reads and decodes the specified number of data bytes from the underlying input
stream into a byte array until bytes are available.
|
static void |
setDefaultIgnoreInvalidCharacters(boolean ignoreInvalidCharacters)
Sets the default behavior for invalid character handling.
|
void |
setIgnoreInvalidCharacters(boolean ignoreInvalidCharacters)
Sets whether or not to ignore invalid BASE-64 characters in the input.
|
protected int[] decoding
protected static final int ERROR
protected static final int IGNORE
protected static final int NOTIFY
public Base64InputStream(java.io.InputStream in)
in - the underlying input stream.public Base64InputStream(java.io.InputStream in,
boolean ignoreInvalidCharacters)
in - the underlying input stream.ignoreInvalidCharacters - false to throw a Base64Exception when
invalid characters are encountered, true to ignore invlaid characterspublic void setIgnoreInvalidCharacters(boolean ignoreInvalidCharacters)
ignoreInvalidCharacters - false to throw a Base64Exception when
invalid characters are encountered, true to ignore invlaid characterspublic int read(byte[] b,
int off,
int len)
throws java.io.IOException,
Base64Exception
The bytes are decoded and read to the given byte array, beginning at position
off. The number of bytes read to the array are returned.
read in class java.io.FilterInputStreamb - the byte array into which the decoded bytes are readoff - the offset indicating the position in b to which the
first decoded byte shall be readlen - the maximum number of bytes to be readjava.io.IOException - if an I/O error occurs.Base64Exception - if there is an Base64 format errorprotected void notify(byte[] buffer)
throws java.io.IOException
NOTIFY is encountered.
This method does nothing and shall be overwritten by a sub-class.buffer - the characters already read from the input stream; and the
first one was marked with NOTIFYjava.io.IOException - allows the subclass to throw an Exception if an error occurspublic int read()
throws java.io.IOException,
Base64Exception
read in class java.io.FilterInputStreamjava.io.IOException - if an I/O error occursBase64Exception - if there is a Base64 format errorpublic static void setDefaultIgnoreInvalidCharacters(boolean ignoreInvalidCharacters)
Base64InputStream will handle invalid
characters according the new settings, unless explicitly specified when
creating the new instance.ignoreInvalidCharacters - false to throw a Base64Exception when
invalid characters are encountered, true to ignore invalid characterspublic boolean markSupported()
false since mark/reset
is not supported by this input stream.markSupported in class java.io.FilterInputStreamfalse