net.rim.device.api.io
Class Base64InputStream

java.lang.Object
  |
  +--java.io.InputStream
        |
        +--net.rim.device.api.io.Base64InputStream

public class Base64InputStream
extends InputStream

Input stream for reading Base64 encoded data.

This class implements a stream which reads in and decodes Base64 encoded data. For a description of the Base64 encoding scheme, see RFC 2045.

The input data is validated during decoding. Carriage return (0x0D) and line feed (0x0A) characters are ignored.

Sample code

Example 1:

try
{
    byte [] data = Base64InputStream.decode(strToDecode);
    String decoded = new String(data, "UTF-8");
}
catch (IOException ioe)
{
}

Example 2:


// Decodes the given byte array from Base64.
// Offset and length specify which part of the array to decode.
// The decoded data is returned as a String.
public static String decodeBase64( byte[] toDecode, int offset, int length )
{
    // Create the input byte array to pass in the content.
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( toDecode, offset, length )
   
    // Create the base64 input stream.
    Base64InputStream base64InputStream = new Base64InputStream(byteArrayInputStream);
   
    StringBuffer sb = new StringBuffer();
    int read;
    try
    {
        // Call read to decode the base64 content.
        while ((read = base64InputStream.read()) >= 0)
        {
            sb.append( (char)read );
        }
       
        base64InputStream.close();
    }
    catch (IOException ioe)
    {
        return null;       
    }
           
    return sb.toString();
}


Constructor Summary
Base64InputStream(InputStream inputStream)
          Constructs a new Base64InputStream instance with underlying input stream.
Base64InputStream(InputStream inputStream, boolean treatErrorAsEOF)
          Constructs a new Base64InputStream instance with underlying input stream.
 
Method Summary
 int available()
          Retrieves number of available bytes.
 void close()
          Closes this input stream.
static byte[] decode(byte[] input, int inputOffset, int inputLength)
          Decode the Base64 encoded input and return the result.
static byte[] decode(String input)
          Decode the Base64 encoded input and return the result.
static byte[] decode(String input, int inputOffset, int inputLength)
          Decode the Base64 encoded input and return the result.
 int read()
          Reads one byte from this stream.
 int read(byte[] buffer)
          Reads some bytes from this stream.
 int read(byte[] buffer, int bufferOffset, int bufferLength)
          Read specified number of bytes from this stream.
 
Methods inherited from class java.io.InputStream
mark, markSupported, reset, skip
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Base64InputStream

public Base64InputStream(InputStream inputStream)
Constructs a new Base64InputStream instance with underlying input stream. The stream defaults to immediately throwing an IOException if a decoding error occurs, rather than treating the error as the EOF.

Parameters:
inputStream - Underlying input stream.

Base64InputStream

public Base64InputStream(InputStream inputStream,
                         boolean treatErrorAsEOF)
Constructs a new Base64InputStream instance with underlying input stream.

Parameters:
inputStream - Underlying input stream.
treatErrorAsEOF - A boolean to specify whether or not to treat a decoding error as the end of stream or to just immediately throw an IOException.
Since:
JDE 3.6.0
Method Detail

available

public int available()
              throws IOException
Retrieves number of available bytes.

Invoke this method to find out the number of bytes you can freely read (or skip over) from this stream (that is, without being blocked by another invocation of one of this stream's methods, on this or another thread).

Overrides:
available in class InputStream
Returns:
Number of bytes freely available for reading in this stream; note that you cannot assume this value to equal the length of this stream.
Throws:
IOException - If this stream is closed.

close

public void close()
           throws IOException
Closes this input stream.

Invoke this method to close this stream and release any system resources it uses.

Overrides:
close in class InputStream
Throws:
IOException - If the underlying stream has a problem.

decode

public static byte[] decode(byte[] input,
                            int inputOffset,
                            int inputLength)
                     throws IOException
Decode the Base64 encoded input and return the result.
Parameters:
input - The Base64 encoded input
inputOffset - The offset into the array
inputLength - The length of the input
Returns:
A byte array containing the decoded input.
Throws:
IOException - Thrown if a decoding error occurred.
Since:
JDE 4.0.2

decode

public static byte[] decode(String input)
                     throws IOException
Decode the Base64 encoded input and return the result.
Parameters:
input - The Base64 encoded input
inputOffset - The offset into the array
inputLength - The length of the input
Returns:
A byte array containing the decoded input.
Throws:
IOException - Thrown if a decoding error occurred.
Since:
JDE 4.1.0

decode

public static byte[] decode(String input,
                            int inputOffset,
                            int inputLength)
                     throws IOException
Decode the Base64 encoded input and return the result.
Parameters:
input - The Base64 encoded input
inputOffset - The offset into the array
inputLength - The length of the input
Returns:
A byte array containing the decoded input.
Throws:
IOException - Thrown if a decoding error occurred.
Since:
JDE 4.1.0

read

public int read()
         throws IOException
Reads one byte from this stream.

Invoke this method to read one byte from this stream.

Overrides:
read in class InputStream
Returns:
Decoded byte from stream, or -1 if no more data to read.
Throws:
IOException - If this stream is closed, the underlying stream has a problem, or the input data is corrupted.

read

public int read(byte[] buffer)
         throws IOException
Reads some bytes from this stream.

Invoke this method to attempt to fill a provided output parameter byte array with data from this stream.

Overrides:
read in class InputStream
Parameters:
buffer - Buffer to contain read and decoded data.
Returns:
0 if length of output parameter byte array is zero; otherwise, the number of bytes actually read into the buffer. If no more data was available when you invoked this method, it returns -1.
Throws:
IOException - If this stream is closed, the underlying stream has a problem, or the input data is corrupted.

read

public int read(byte[] buffer,
                int bufferOffset,
                int bufferLength)
         throws IOException
Read specified number of bytes from this stream.

Overrides:
read in class InputStream
Parameters:
buffer - Buffer to contain data; must be large enough to contain desired number of bytes.
bufferOffset - First element in output parameter buffer at which to begin writing decoded data; if not 0, then output parameter buffer must be at least bufferOffset + bufferLength elements long.
bufferLength - Number of bytes to read from this stream.
Returns:
0 if length of output parameter byte array is zero; otherwise, the number of bytes actually read into the buffer. If no more data was available when you invoked this method, it returns -1.
Throws:
IOException - If this stream is closed, the underlying stream has a problem, or the input data is corrupted.



Copyright 1999-2009 Research In Motion Limited. 295 Phillip Street, Waterloo, Ontario, Canada, N2L 3W8. All Rights Reserved.
Copyright 1993-2003 Sun Microsystems, Inc. 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
Copyright 2002-2003 Nokia Corporation All Rights Reserved.
Java is a trademark or registered trademark of Sun Microsystems, Inc. in the US and other countries.