src/java.base/share/classes/javax/crypto/CipherInputStream.java
author igerasim
Sat, 15 Sep 2018 22:02:08 -0700
changeset 51759 ac6e9a2ebc04
parent 47216 71c04702a3d5
child 52160 2de3d2f1df39
permissions -rw-r--r--
8210786: Typo s/overriden/overridden/ in several places Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
51759
ac6e9a2ebc04 8210786: Typo s/overriden/overridden/ in several places
igerasim
parents: 47216
diff changeset
     2
 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.crypto;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
29819
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents: 29492
diff changeset
    28
import java.io.InputStream;
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents: 29492
diff changeset
    29
import java.io.FilterInputStream;
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents: 29492
diff changeset
    30
import java.io.IOException;
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents: 29492
diff changeset
    31
import javax.crypto.BadPaddingException;
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents: 29492
diff changeset
    32
import javax.crypto.IllegalBlockSizeException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * A CipherInputStream is composed of an InputStream and a Cipher so
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * that read() methods return data that are read in from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * underlying InputStream but have been additionally processed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Cipher.  The Cipher must be fully initialized before being used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * a CipherInputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p> For example, if the Cipher is initialized for decryption, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * CipherInputStream will attempt to read in data and decrypt them,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * before returning the decrypted data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p> This class adheres strictly to the semantics, especially the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * failure semantics, of its ancestor classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * java.io.FilterInputStream and java.io.InputStream.  This class has
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * exactly those methods specified in its ancestor classes, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * overrides them all.  Moreover, this class catches all exceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * that are not thrown by its ancestor classes.  In particular, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <code>skip</code> method skips, and the <code>available</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * method counts only data that have been processed by the encapsulated Cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <p> It is crucial for a programmer using this class not to use
51759
ac6e9a2ebc04 8210786: Typo s/overriden/overridden/ in several places
igerasim
parents: 47216
diff changeset
    55
 * methods that are not defined or overridden in this class (such as a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * new method or constructor that is later added to one of the super
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * classes), because the design and implementation of those methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * are unlikely to have considered security impact with regard to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * CipherInputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @author  Li Gong
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @see     java.io.InputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @see     java.io.FilterInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @see     javax.crypto.Cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * @see     javax.crypto.CipherOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
public class CipherInputStream extends FilterInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // the cipher engine to use to process stream data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private Cipher cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // the underlying input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private InputStream input;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /* the buffer holding data that have been read in from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
       underlying stream, but have not been processed by the cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
       engine. the size 512 bytes is somewhat randomly chosen */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private byte[] ibuffer = new byte[512];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // having reached the end of the underlying input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private boolean done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /* the buffer holding data that have been processed by the cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
       engine, but have not been read out */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private byte[] obuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    // the offset pointing to the next "new" byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private int ostart = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    // the offset pointing to the last "new" byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private int ofinish = 0;
17917
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents: 5506
diff changeset
    93
    // stream status
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents: 5506
diff changeset
    94
    private boolean closed = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
44999
b1c62cd70983 8178014: CryptoPolicyParser's API comment contains < and > characters
wetmore
parents: 29819
diff changeset
    96
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * private convenience function.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Entry condition: ostart = ofinish
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Exit condition: ostart <= ofinish
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * return (ofinish-ostart) (we have this many bytes for you)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * return 0 (no data now, but could have more later)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * return -1 (absolutely no more data)
29819
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents: 29492
diff changeset
   106
     *
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents: 29492
diff changeset
   107
     * Note:  Exceptions are only thrown after the stream is completely read.
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents: 29492
diff changeset
   108
     * For AEAD ciphers a read() of any length will internally cause the
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents: 29492
diff changeset
   109
     * whole stream to be read fully and verify the authentication tag before
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents: 29492
diff changeset
   110
     * returning decrypted data or exceptions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private int getMoreData() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (done) return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        int readin = input.read(ibuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (readin == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                obuffer = cipher.doFinal();
27066
d3f0b8d935e6 8037846: Ensure streaming of input cipher streams
ascarpino
parents: 25859
diff changeset
   119
            } catch (IllegalBlockSizeException | BadPaddingException e) {
d3f0b8d935e6 8037846: Ensure streaming of input cipher streams
ascarpino
parents: 25859
diff changeset
   120
                obuffer = null;
d3f0b8d935e6 8037846: Ensure streaming of input cipher streams
ascarpino
parents: 25859
diff changeset
   121
                throw new IOException(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            if (obuffer == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                ostart = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                ofinish = obuffer.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                return ofinish;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            obuffer = cipher.update(ibuffer, 0, readin);
27066
d3f0b8d935e6 8037846: Ensure streaming of input cipher streams
ascarpino
parents: 25859
diff changeset
   133
        } catch (IllegalStateException e) {
d3f0b8d935e6 8037846: Ensure streaming of input cipher streams
ascarpino
parents: 25859
diff changeset
   134
            obuffer = null;
d3f0b8d935e6 8037846: Ensure streaming of input cipher streams
ascarpino
parents: 25859
diff changeset
   135
            throw e;
d3f0b8d935e6 8037846: Ensure streaming of input cipher streams
ascarpino
parents: 25859
diff changeset
   136
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        ostart = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (obuffer == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            ofinish = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        else ofinish = obuffer.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return ofinish;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Constructs a CipherInputStream from an InputStream and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * <br>Note: if the specified input stream or cipher is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * null, a NullPointerException may be thrown later when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * they are used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param is the to-be-processed input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param c an initialized Cipher object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public CipherInputStream(InputStream is, Cipher c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        super(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        input = is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        cipher = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Constructs a CipherInputStream from an InputStream without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * specifying a Cipher. This has the effect of constructing a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * CipherInputStream using a NullCipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * <br>Note: if the specified input stream is null, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * NullPointerException may be thrown later when it is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @param is the to-be-processed input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    protected CipherInputStream(InputStream is) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        super(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        input = is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        cipher = new NullCipher();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Reads the next byte of data from this input stream. The value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * byte is returned as an <code>int</code> in the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * <code>0</code> to <code>255</code>. If no byte is available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * because the end of the stream has been reached, the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * <code>-1</code> is returned. This method blocks until input data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * is available, the end of the stream is detected, or an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @return  the next byte of data, or <code>-1</code> if the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *          stream is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (ostart >= ofinish) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            // we loop for new data as the spec says we are blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            while (i == 0) i = getMoreData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            if (i == -1) return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return ((int) obuffer[ostart++] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Reads up to <code>b.length</code> bytes of data from this input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * stream into an array of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * The <code>read</code> method of <code>InputStream</code> calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * the <code>read</code> method of three arguments with the arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * <code>b</code>, <code>0</code>, and <code>b.length</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @param      b   the buffer into which the data is read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @return     the total number of bytes read into the buffer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *             <code>-1</code> is there is no more data because the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *             the stream has been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @see        java.io.InputStream#read(byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public int read(byte b[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        return read(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Reads up to <code>len</code> bytes of data from this input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * into an array of bytes. This method blocks until some input is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * available. If the first argument is <code>null,</code> up to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * <code>len</code> bytes are read and discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @param      b     the buffer into which the data is read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @param      off   the start offset in the destination array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *                   <code>buf</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @param      len   the maximum number of bytes read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @return     the total number of bytes read into the buffer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *             <code>-1</code> if there is no more data because the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *             the stream has been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @see        java.io.InputStream#read()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    public int read(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (ostart >= ofinish) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            // we loop for new data as the spec says we are blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            while (i == 0) i = getMoreData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            if (i == -1) return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (len <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        int available = ofinish - ostart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (len < available) available = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (b != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            System.arraycopy(obuffer, ostart, b, off, available);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        ostart = ostart + available;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        return available;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Skips <code>n</code> bytes of input from the bytes that can be read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * from this input stream without blocking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * <p>Fewer bytes than requested might be skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * The actual number of bytes skipped is equal to <code>n</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * the result of a call to
18771
9dadb0719cea 8019772: Fix doclint issues in javax.crypto and javax.security subpackages
juh
parents: 17917
diff changeset
   257
     * {@link #available() available},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * whichever is smaller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * If <code>n</code> is less than zero, no bytes are skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * <p>The actual number of bytes skipped is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @param      n the number of bytes to be skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @return     the actual number of bytes skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public long skip(long n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        int available = ofinish - ostart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (n > available) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            n = available;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if (n < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        ostart += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * Returns the number of bytes that can be read from this input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * stream without blocking. The <code>available</code> method of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * <code>InputStream</code> returns <code>0</code>. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * <B>should</B> be overridden by subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @return     the number of bytes that can be read from this input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *             without blocking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    public int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        return (ofinish - ostart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * Closes this input stream and releases any system resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * associated with the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * The <code>close</code> method of <code>CipherInputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * calls the <code>close</code> method of its underlying input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    public void close() throws IOException {
17917
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents: 5506
diff changeset
   304
        if (closed) {
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents: 5506
diff changeset
   305
            return;
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents: 5506
diff changeset
   306
        }
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents: 5506
diff changeset
   307
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents: 5506
diff changeset
   308
        closed = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        input.close();
29819
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents: 29492
diff changeset
   310
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents: 29492
diff changeset
   311
        // Throw away the unprocessed data and throw no crypto exceptions.
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents: 29492
diff changeset
   312
        // AEAD ciphers are fully readed before closing.  Any authentication
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents: 29492
diff changeset
   313
        // exceptions would occur while reading.
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents: 29492
diff changeset
   314
        if (!done) {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents: 29492
diff changeset
   315
            try {
18808
b2eaaaaed037 8012637: Adjust CipherInputStream class to work in AEAD/GCM mode
valeriep
parents: 18771
diff changeset
   316
                cipher.doFinal();
b2eaaaaed037 8012637: Adjust CipherInputStream class to work in AEAD/GCM mode
valeriep
parents: 18771
diff changeset
   317
            }
29819
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents: 29492
diff changeset
   318
            catch (BadPaddingException | IllegalBlockSizeException ex) {
2de061001dcb 8064546: CipherInputStream throws BadPaddingException if stream is not fully read
ascarpino
parents: 29492
diff changeset
   319
                // Catch exceptions as the rest of the stream is unused.
27721
e261e8fb8837 8042480: CipherInputStream.close() throws AEADBadTagException in some cases
ascarpino
parents: 27066
diff changeset
   320
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        ostart = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        ofinish = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * Tests if this input stream supports the <code>mark</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * and <code>reset</code> methods, which it does not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @return  <code>false</code>, since this class does not support the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *          <code>mark</code> and <code>reset</code> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @see     java.io.InputStream#mark(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @see     java.io.InputStream#reset()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    public boolean markSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
}