jdk/src/share/classes/javax/crypto/CipherInputStream.java
author henryjen
Mon, 16 Jun 2014 09:44:54 -0700
changeset 25176 3c2ba9344cbf
parent 18808 b2eaaaaed037
permissions -rw-r--r--
8047721: @since should have JDK version Reviewed-by: darcy, wetmore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
17917
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents: 5506
diff changeset
     2
 * Copyright (c) 1997, 2013, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * A CipherInputStream is composed of an InputStream and a Cipher so
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * that read() methods return data that are read in from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * underlying InputStream but have been additionally processed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Cipher.  The Cipher must be fully initialized before being used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * a CipherInputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p> For example, if the Cipher is initialized for decryption, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * CipherInputStream will attempt to read in data and decrypt them,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * before returning the decrypted data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p> This class adheres strictly to the semantics, especially the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * failure semantics, of its ancestor classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * java.io.FilterInputStream and java.io.InputStream.  This class has
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * exactly those methods specified in its ancestor classes, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * overrides them all.  Moreover, this class catches all exceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * that are not thrown by its ancestor classes.  In particular, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <code>skip</code> method skips, and the <code>available</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * method counts only data that have been processed by the encapsulated Cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p> It is crucial for a programmer using this class not to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * methods that are not defined or overriden in this class (such as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * new method or constructor that is later added to one of the super
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * classes), because the design and implementation of those methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * are unlikely to have considered security impact with regard to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * CipherInputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @author  Li Gong
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @see     java.io.InputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @see     java.io.FilterInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @see     javax.crypto.Cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @see     javax.crypto.CipherOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
public class CipherInputStream extends FilterInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // the cipher engine to use to process stream data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private Cipher cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // the underlying input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private InputStream input;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /* the buffer holding data that have been read in from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
       underlying stream, but have not been processed by the cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
       engine. the size 512 bytes is somewhat randomly chosen */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private byte[] ibuffer = new byte[512];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // having reached the end of the underlying input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private boolean done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /* the buffer holding data that have been processed by the cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
       engine, but have not been read out */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private byte[] obuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // the offset pointing to the next "new" byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private int ostart = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    // the offset pointing to the last "new" byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private int ofinish = 0;
17917
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents: 5506
diff changeset
    89
    // stream status
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents: 5506
diff changeset
    90
    private boolean closed = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * private convenience function.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Entry condition: ostart = ofinish
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Exit condition: ostart <= ofinish
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * return (ofinish-ostart) (we have this many bytes for you)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * return 0 (no data now, but could have more later)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * return -1 (absolutely no more data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private int getMoreData() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (done) return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        int readin = input.read(ibuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (readin == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                obuffer = cipher.doFinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            catch (IllegalBlockSizeException e) {obuffer = null;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            catch (BadPaddingException e) {obuffer = null;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (obuffer == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                ostart = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                ofinish = obuffer.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                return ofinish;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            obuffer = cipher.update(ibuffer, 0, readin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        } catch (IllegalStateException e) {obuffer = null;};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        ostart = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (obuffer == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            ofinish = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        else 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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Constructs a CipherInputStream from an InputStream and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * <br>Note: if the specified input stream or cipher is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * null, a NullPointerException may be thrown later when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * they are used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @param is the to-be-processed input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @param c an initialized Cipher object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public CipherInputStream(InputStream is, Cipher c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        super(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        input = is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        cipher = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Constructs a CipherInputStream from an InputStream without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * specifying a Cipher. This has the effect of constructing a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * CipherInputStream using a NullCipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * <br>Note: if the specified input stream is null, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * NullPointerException may be thrown later when it is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @param is the to-be-processed input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    protected CipherInputStream(InputStream is) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        super(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        input = is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        cipher = new NullCipher();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Reads the next byte of data from this input stream. The value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * byte is returned as an <code>int</code> in the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * <code>0</code> to <code>255</code>. If no byte is available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * because the end of the stream has been reached, the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * <code>-1</code> is returned. This method blocks until input data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * is available, the end of the stream is detected, or an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @return  the next byte of data, or <code>-1</code> if the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *          stream is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (ostart >= ofinish) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            // we loop for new data as the spec says we are blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            while (i == 0) i = getMoreData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            if (i == -1) return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        return ((int) obuffer[ostart++] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Reads up to <code>b.length</code> bytes of data from this input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * stream into an array of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * The <code>read</code> method of <code>InputStream</code> calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * the <code>read</code> method of three arguments with the arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * <code>b</code>, <code>0</code>, and <code>b.length</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param      b   the buffer into which the data is read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @return     the total number of bytes read into the buffer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *             <code>-1</code> is there is no more data because the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *             the stream has been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @see        java.io.InputStream#read(byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public int read(byte b[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        return read(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Reads up to <code>len</code> bytes of data from this input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * into an array of bytes. This method blocks until some input is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * available. If the first argument is <code>null,</code> up to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * <code>len</code> bytes are read and discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @param      b     the buffer into which the data is read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @param      off   the start offset in the destination array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *                   <code>buf</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @param      len   the maximum number of bytes read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @return     the total number of bytes read into the buffer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *             <code>-1</code> if there is no more data because the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *             the stream has been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @see        java.io.InputStream#read()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public int read(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if (ostart >= ofinish) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            // we loop for new data as the spec says we are blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            while (i == 0) i = getMoreData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            if (i == -1) return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if (len <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        int available = ofinish - ostart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (len < available) available = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (b != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            System.arraycopy(obuffer, ostart, b, off, available);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        ostart = ostart + available;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        return available;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * Skips <code>n</code> bytes of input from the bytes that can be read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * from this input stream without blocking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * <p>Fewer bytes than requested might be skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * The actual number of bytes skipped is equal to <code>n</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * the result of a call to
18771
9dadb0719cea 8019772: Fix doclint issues in javax.crypto and javax.security subpackages
juh
parents: 17917
diff changeset
   245
     * {@link #available() available},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * whichever is smaller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * If <code>n</code> is less than zero, no bytes are skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * <p>The actual number of bytes skipped is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @param      n the number of bytes to be skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @return     the actual number of bytes skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    public long skip(long n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        int available = ofinish - ostart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if (n > available) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            n = available;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if (n < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        ostart += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Returns the number of bytes that can be read from this input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * stream without blocking. The <code>available</code> method of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * <code>InputStream</code> returns <code>0</code>. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * <B>should</B> be overridden by subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @return     the number of bytes that can be read from this input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *             without blocking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return (ofinish - ostart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Closes this input stream and releases any system resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * associated with the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * The <code>close</code> method of <code>CipherInputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * calls the <code>close</code> method of its underlying input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public void close() throws IOException {
17917
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents: 5506
diff changeset
   292
        if (closed) {
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents: 5506
diff changeset
   293
            return;
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents: 5506
diff changeset
   294
        }
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents: 5506
diff changeset
   295
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents: 5506
diff changeset
   296
        closed = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        input.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            // throw away the unprocessed data
18808
b2eaaaaed037 8012637: Adjust CipherInputStream class to work in AEAD/GCM mode
valeriep
parents: 18771
diff changeset
   300
            if (!done) {
b2eaaaaed037 8012637: Adjust CipherInputStream class to work in AEAD/GCM mode
valeriep
parents: 18771
diff changeset
   301
                cipher.doFinal();
b2eaaaaed037 8012637: Adjust CipherInputStream class to work in AEAD/GCM mode
valeriep
parents: 18771
diff changeset
   302
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
17917
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents: 5506
diff changeset
   304
        catch (BadPaddingException | IllegalBlockSizeException ex) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        ostart = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        ofinish = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * Tests if this input stream supports the <code>mark</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * and <code>reset</code> methods, which it does not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @return  <code>false</code>, since this class does not support the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *          <code>mark</code> and <code>reset</code> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @see     java.io.InputStream#mark(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @see     java.io.InputStream#reset()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    public boolean markSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
}