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