FileDocCategorySizeDatePackage
CipherInputStream.javaAPI DocAndroid 1.5 API7719Wed May 06 22:41:02 BST 2009javax.crypto

CipherInputStream

public class CipherInputStream extends FilterInputStream
This class wraps an {@code InputStream} and a cipher so that {@code read()} methods return data that are read from the underlying {@code InputStream} and processed by the cipher.

The cipher must be initialized for the requested operation before being used by a {@code CipherInputStream}. For example, if a cipher initialized for decryption is used with a {@code CipherInputStream}, the {@code CipherInputStream} tries to read the data an decrypt them before returning.

since
Android 1.0

Fields Summary
private final Cipher
cipher
private final int
I_BUFFER_SIZE
private final byte[]
i_buffer
private int
index
private byte[]
o_buffer
private boolean
finished
Constructors Summary
public CipherInputStream(InputStream is, Cipher c)
Creates a new {@code CipherInputStream} instance for an {@code InputStream} and a cipher.

param
is the input stream to read data from.
param
c the cipher to process the data with.
since
Android 1.0


                                                                  
         
        super(is);
        this.cipher = c;
    
protected CipherInputStream(InputStream is)
Creates a new {@code CipherInputStream} instance for an {@code InputStream} without a cipher.

A {@code NullCipher} is created and used to process the data.

param
is the input stream to read data from.
since
Android 1.0

        this(is, new NullCipher());
    
Methods Summary
public intavailable()
Returns the number of bytes available without blocking. It (currently) always returns {@code 0} in Android.

return
the number of bytes available, currently zero.
throws
IOException if an error occurs
since
Android 1.0

        return 0;
    
public voidclose()
Closes this {@code CipherInputStream}, also closes the underlying input stream and call {@code doFinal} on the cipher object.

throws
IOException if an error occurs.
since
Android 1.0

        in.close();
        try {
            cipher.doFinal();
        } catch (GeneralSecurityException ignore) {
            //do like RI does
        }

    
public booleanmarkSupported()
Returns whether this input stream supports {@code mark} and {@code reset} , which it does not.

return
false, since this input stream does not support {@code mark} and {@code reset}.
since
Android 1.0

        return false;
    
public intread()
Reads the next byte from this cipher input stream.

return
the next byte, or {@code -1} if the end of the stream is reached.
throws
IOException if an error occurs.
since
Android 1.0

        if (finished) {
            return ((o_buffer == null) || (index == o_buffer.length)) 
                            ? -1 
                            : o_buffer[index++] & 0xFF;
        }
        if ((o_buffer != null) && (index < o_buffer.length)) {
            return o_buffer[index++] & 0xFF;
        }
        index = 0;
        o_buffer = null;
        int num_read;
        while (o_buffer == null) {
            if ((num_read = in.read(i_buffer)) == -1) {
                try {
                    o_buffer = cipher.doFinal();
                } catch (Exception e) {
                    throw new IOException(e.getMessage());
                }
                finished = true;
                break;
            }
            o_buffer = cipher.update(i_buffer, 0, num_read);
        }
        return read();
    
public intread(byte[] b)
Reads the next {@code b.length} bytes from this input stream into buffer {@code b}.

param
b the buffer to be filled with data.
return
the number of bytes filled into buffer {@code b}, or {@code -1} if the end of the stream is reached.
throws
IOException if an error occurs.
since
Android 1.0

        return read(b, 0, b.length);
    
public intread(byte[] b, int off, int len)
Reads the next {@code len} bytes from this input stream into buffer {@code b} starting at offset {@code off}.

if {@code b} is {@code null}, the next {@code len} bytes are read and discarded.

param
b the buffer to be filled with data.
param
off the offset to start in the buffer.
param
len the maximum number of bytes to read.
return
the number of bytes filled into buffer {@code b}, or {@code -1} of the of the stream is reached.
throws
IOException if an error occurs.
throws
NullPointerException if the underlying input stream is {@code null}.
since
Android 1.0

        if (in == null) {
            throw new NullPointerException("Underlying input stream is null");
        }

        int read_b;
        int i;
        for (i=0; i<len; i++) {
            if ((read_b = read()) == -1) {
                return (i == 0) ? -1 : i; 
            }
            if (b != null) {
                b[off+i] = (byte) read_b;
            }
        }
        return i;
    
public longskip(long n)
Skips up to n bytes from this input stream.

The number of bytes skipped depends on the result of a call to {@link CipherInputStream#available() available}. The smaller of n and the result are the number of bytes being skipped.

Skipping is (currently) not supported in Android.

param
n the number of bytes that should be skipped.
return
the number of bytes actually skipped.
throws
IOException if an error occurs
since
Android 1.0

        long i = 0;
        int available = available();
        if (available < n) {
            n = available;
        }
        while ((i < n) && (read() != -1)) {
            i++;
        }
        return i;