jdk/src/share/classes/javax/crypto/CipherOutputStream.java
author henryjen
Mon, 16 Jun 2014 09:44:54 -0700
changeset 25176 3c2ba9344cbf
parent 17917 9bd1b39cdbcf
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 CipherOutputStream is composed of an OutputStream and a Cipher so
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * that write() methods first process the data before writing them out
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * to the underlying OutputStream.  The cipher must be fully
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * initialized before being used by a CipherOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p> For example, if the cipher is initialized for encryption, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * CipherOutputStream will attempt to encrypt data before writing out the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * encrypted data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p> This class adheres strictly to the semantics, especially the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * failure semantics, of its ancestor classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * java.io.OutputStream and java.io.FilterOutputStream.  This class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * has exactly those methods specified in its ancestor classes, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * overrides them all.  Moreover, this class catches all exceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * that are not thrown by its ancestor classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p> It is crucial for a programmer using this class not to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * methods that are not defined or overriden in this class (such as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * new method or constructor that is later added to one of the super
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * classes), because the design and implementation of those methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * are unlikely to have considered security impact with regard to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * CipherOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @author  Li Gong
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @see     java.io.OutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @see     java.io.FilterOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @see     javax.crypto.Cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @see     javax.crypto.CipherInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
public class CipherOutputStream extends FilterOutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // the cipher engine to use to process stream data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private Cipher cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // the underlying output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private OutputStream output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /* the buffer holding one byte of incoming data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private byte[] ibuffer = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    // the buffer holding data ready to be written out
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private byte[] obuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
17917
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents: 5506
diff changeset
    77
    // stream status
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents: 5506
diff changeset
    78
    private boolean closed = false;
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents: 5506
diff changeset
    79
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Constructs a CipherOutputStream from an OutputStream and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * <br>Note: if the specified output stream or cipher is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * null, a NullPointerException may be thrown later when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * they are used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @param os  the OutputStream object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @param c   an initialized Cipher object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public CipherOutputStream(OutputStream os, Cipher c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        super(os);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        output = os;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        cipher = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Constructs a CipherOutputStream from an OutputStream without
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * specifying a Cipher. This has the effect of constructing a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * CipherOutputStream using a NullCipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * <br>Note: if the specified output stream is null, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * NullPointerException may be thrown later when it is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @param os  the OutputStream object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    protected CipherOutputStream(OutputStream os) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        super(os);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        output = os;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        cipher = new NullCipher();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Writes the specified byte to this output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @param      b   the <code>byte</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public void write(int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        ibuffer[0] = (byte) b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        obuffer = cipher.update(ibuffer, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (obuffer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            output.write(obuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            obuffer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Writes <code>b.length</code> bytes from the specified byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * to this output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * The <code>write</code> method of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * <code>CipherOutputStream</code> calls the <code>write</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * method of three arguments with the three arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * <code>b</code>, <code>0</code>, and <code>b.length</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param      b   the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @exception  NullPointerException if <code>b</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @see        javax.crypto.CipherOutputStream#write(byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public void write(byte b[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        write(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Writes <code>len</code> bytes from the specified byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * starting at offset <code>off</code> to this output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @param      b     the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param      off   the start offset in the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param      len   the number of bytes to write.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public void write(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        obuffer = cipher.update(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (obuffer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            output.write(obuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            obuffer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Flushes this output stream by forcing any buffered output bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * that have already been processed by the encapsulated cipher object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * to be written out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * <p>Any bytes buffered by the encapsulated cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * and waiting to be processed by it will not be written out. For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * if the encapsulated cipher is a block cipher, and the total number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * bytes written using one of the <code>write</code> methods is less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * the cipher's block size, no bytes will be written out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public void flush() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (obuffer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            output.write(obuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            obuffer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        output.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Closes this output stream and releases any system resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * associated with this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * This method invokes the <code>doFinal</code> method of the encapsulated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * cipher object, which causes any bytes buffered by the encapsulated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * cipher to be processed. The result is written out by calling the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * <code>flush</code> method of this output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * This method resets the encapsulated cipher object to its initial state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * and calls the <code>close</code> method of the underlying output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public void close() throws IOException {
17917
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents: 5506
diff changeset
   199
        if (closed) {
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents: 5506
diff changeset
   200
            return;
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents: 5506
diff changeset
   201
        }
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents: 5506
diff changeset
   202
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents: 5506
diff changeset
   203
        closed = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            obuffer = cipher.doFinal();
17917
9bd1b39cdbcf 7160837: DigestOutputStream does not turn off digest calculation when "close()" is called
ascarpino
parents: 5506
diff changeset
   206
        } catch (IllegalBlockSizeException | BadPaddingException e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            obuffer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        } catch (IOException ignored) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
}