CipherInputStream.java revision 0
2362N/A * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved. 827N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 827N/A * This code is free software; you can redistribute it and/or modify it 827N/A * under the terms of the GNU General Public License version 2 only, as 827N/A * published by the Free Software Foundation. Sun designates this 827N/A * particular file as subject to the "Classpath" exception as provided 827N/A * by Sun in the LICENSE file that accompanied this code. 827N/A * This code is distributed in the hope that it will be useful, but WITHOUT 827N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 827N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 827N/A * version 2 for more details (a copy is included in the LICENSE file that 827N/A * accompanied this code). 827N/A * You should have received a copy of the GNU General Public License version 827N/A * 2 along with this work; if not, write to the Free Software Foundation, 2362N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 827N/A * CA 95054 USA or visit www.sun.com if you need additional information or 827N/A * A CipherInputStream is composed of an InputStream and a Cipher so 827N/A * that read() methods return data that are read in from the 827N/A * underlying InputStream but have been additionally processed by the 827N/A * Cipher. The Cipher must be fully initialized before being used by 827N/A * <p> For example, if the Cipher is initialized for decryption, the 827N/A * CipherInputStream will attempt to read in data and decrypt them, 827N/A * before returning the decrypted data. 827N/A * <p> This class adheres strictly to the semantics, especially the 827N/A * failure semantics, of its ancestor classes 827N/A * java.io.FilterInputStream and java.io.InputStream. This class has 827N/A * exactly those methods specified in its ancestor classes, and 827N/A * overrides them all. Moreover, this class catches all exceptions 827N/A * that are not thrown by its ancestor classes. In particular, the 827N/A * <code>skip</code> method skips, and the <code>available</code> 827N/A * method counts only data that have been processed by the encapsulated Cipher. 827N/A * <p> It is crucial for a programmer using this class not to use 827N/A * methods that are not defined or overriden in this class (such as a 827N/A * new method or constructor that is later added to one of the super 827N/A * classes), because the design and implementation of those methods 827N/A * are unlikely to have considered security impact with regard to 827N/A * @see java.io.InputStream 827N/A * @see java.io.FilterInputStream 827N/A * @see javax.crypto.Cipher 827N/A * @see javax.crypto.CipherOutputStream 827N/A // the cipher engine to use to process stream data 827N/A // the underlying input stream 827N/A /* the buffer holding data that have been read in from the 827N/A underlying stream, but have not been processed by the cipher 827N/A engine. the size 512 bytes is somewhat randomly chosen */ private byte[]
ibuffer =
new byte[
512];
// having reached the end of the underlying input stream private boolean done =
false;
/* the buffer holding data that have been processed by the cipher engine, but have not been read out */ // the offset pointing to the next "new" byte // the offset pointing to the last "new" byte * private convenience function. * Entry condition: ostart = ofinish * Exit condition: ostart <= ofinish * return (ofinish-ostart) (we have this many bytes for you) * return 0 (no data now, but could have more later) * return -1 (absolutely no more data) * Constructs a CipherInputStream from an InputStream and a * <br>Note: if the specified input stream or cipher is * null, a NullPointerException may be thrown later when * @param is the to-be-processed input stream * @param c an initialized Cipher object * Constructs a CipherInputStream from an InputStream without * specifying a Cipher. This has the effect of constructing a * CipherInputStream using a NullCipher. * <br>Note: if the specified input stream is null, a * NullPointerException may be thrown later when it is used. * @param is the to-be-processed input stream * Reads the next byte of data from this input stream. The value * byte is returned as an <code>int</code> in the range * <code>0</code> to <code>255</code>. If no byte is available * because the end of the stream has been reached, the value * <code>-1</code> is returned. This method blocks until input data * is available, the end of the stream is detected, or an exception * @return the next byte of data, or <code>-1</code> if the end of the * @exception IOException if an I/O error occurs. // we loop for new data as the spec says we are blocking * Reads up to <code>b.length</code> bytes of data from this input * stream into an array of bytes. * The <code>read</code> method of <code>InputStream</code> calls * the <code>read</code> method of three arguments with the arguments * <code>b</code>, <code>0</code>, and <code>b.length</code>. * @param b the buffer into which the data is read. * @return the total number of bytes read into the buffer, or * <code>-1</code> is there is no more data because the end of * the stream has been reached. * @exception IOException if an I/O error occurs. * @see java.io.InputStream#read(byte[], int, int) * Reads up to <code>len</code> bytes of data from this input stream * into an array of bytes. This method blocks until some input is * available. If the first argument is <code>null,</code> up to * <code>len</code> bytes are read and discarded. * @param b the buffer into which the data is read. * @param off the start offset in the destination array * @param len the maximum number of bytes read. * @return the total number of bytes read into the buffer, or * <code>-1</code> if there is no more data because the end of * the stream has been reached. * @exception IOException if an I/O error occurs. * @see java.io.InputStream#read() // we loop for new data as the spec says we are blocking * Skips <code>n</code> bytes of input from the bytes that can be read * from this input stream without blocking. * <p>Fewer bytes than requested might be skipped. * The actual number of bytes skipped is equal to <code>n</code> or * the result of a call to * {@link #available() <code>available</code>}, * If <code>n</code> is less than zero, no bytes are skipped. * <p>The actual number of bytes skipped is returned. * @param n the number of bytes to be skipped. * @return the actual number of bytes skipped. * @exception IOException if an I/O error occurs. * Returns the number of bytes that can be read from this input * stream without blocking. The <code>available</code> method of * <code>InputStream</code> returns <code>0</code>. This method * <B>should</B> be overridden by subclasses. * @return the number of bytes that can be read from this input stream * @exception IOException if an I/O error occurs. * Closes this input stream and releases any system resources * associated with the stream. * The <code>close</code> method of <code>CipherInputStream</code> * calls the <code>close</code> method of its underlying input * @exception IOException if an I/O error occurs. // throw away the unprocessed data * Tests if this input stream supports the <code>mark</code> * and <code>reset</code> methods, which it does not. * @return <code>false</code>, since this class does not support the * <code>mark</code> and <code>reset</code> methods. * @see java.io.InputStream#mark(int) * @see java.io.InputStream#reset()