aboutsummaryrefslogtreecommitdiffstats
path: root/libraries/spongycastle/jce/src/main/java/javax/crypto/CipherOutputStream.java
blob: ef2db07f5a97c09532b4d61c2eb855cd5a43bc9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package javax.crypto;

import java.io.OutputStream;
import java.io.IOException;
import java.io.FilterOutputStream;

/**
 * A CipherOutputStream is composed of an OutputStream and a Cipher so
 * that write() methods first process the data before writing them out
 * to the underlying OutputStream.  The cipher must be fully
 * initialized before being used by a CipherOutputStream.
 * <p>
 * For example, if the cipher is initialized for encryption, the
 * CipherOutputStream will attempt to encrypt data before writing out the
 * encrypted data.
 * <p>
 * This class adheres strictly to the semantics, especially the
 * failure semantics, of its ancestor classes
 * java.io.OutputStream and java.io.FilterOutputStream.  This class
 * has exactly those methods specified in its ancestor classes, and
 * overrides them all.  Moreover, this class catches all exceptions
 * that are not thrown by its ancestor classes.
 * <p>
 * It is crucial for a programmer using this class not to use
 * methods that are not defined or overriden in this class (such as a
 * new method or constructor that is later added to one of the super
 * classes), because the design and implementation of those methods
 * are unlikely to have considered security impact with regard to
 * CipherOutputStream.
 *
 * @since JCE1.2
 * @see OutputStream
 * @see FilterOutputStream
 * @see Cipher
 * @see CipherInputStream
 */
public class CipherOutputStream
    extends FilterOutputStream
{
    private Cipher          c;

    private byte[]          oneByte = new byte[1];

    /**
     * Constructs a CipherOutputStream from an OutputStream and a
     * Cipher.
     */
    public CipherOutputStream(
        OutputStream    os,
        Cipher          c)
    {
        super(os);
        this.c = c;
    }

    /**
     * Constructs a CipherOutputStream from an OutputStream without
     * specifying a Cipher. This has the effect of constructing a
     * CipherOutputStream using a NullCipher.
     */
    protected CipherOutputStream(
        OutputStream    os)
    {
        this(os, new NullCipher());
    }

    /**
     * Writes the specified byte to this output stream.
     *
     * @param b the <code>byte</code>.
     * @exception IOException if an I/O error occurs.
     * @since JCE1.2
     */
    public void write(
        int b)
    throws IOException
    {
        oneByte[0] = (byte)b;

        byte[] bytes = c.update(oneByte, 0, 1);

        if (bytes != null)
        {
            out.write(bytes, 0, bytes.length);
        }
    }

    /**
     * Writes <code>b.length</code> bytes from the specified byte array 
     * to this output stream. 
     * <p>
     * The <code>write</code> method of
     * <code>CipherOutputStream</code> calls the <code>write</code>
     * method of three arguments with the three arguments
     * <code>b</code>, <code>0</code>, and <code>b.length</code>.
     *
     * @param b the data.
     * @exception IOException if an I/O error occurs.
     * @since JCE1.2
     * @see #write(byte[], int, int)
     */
    public void write(
        byte[]  b)
    throws IOException
    {
        write(b, 0, b.length);
    }

    /**
     * Writes <code>len</code> bytes from the specified byte array 
     * starting at offset <code>off</code> to this output stream.
     *
     * @param b the data.
     * @param off the start offset in the data.
     * @param len the number of bytes to write.
     * @exception IOException if an I/O error occurs.
     * @since JCE1.2
     */
    public void write(
        byte[]  b,
        int     off,
        int     len)
    throws IOException
    {
        byte[] bytes = c.update(b, off, len);

        if (bytes != null)
        {
            out.write(bytes, 0, bytes.length);
        }
    }

    /**
     * Flushes this output stream by forcing any buffered output bytes 
     * that have already been processed by the encapsulated cipher object
     * to be written out.
     * 
     * <p>
     * Any bytes buffered by the encapsulated cipher
     * and waiting to be processed by it will not be written out. For example,
     * if the encapsulated cipher is a block cipher, and the total number of
     * bytes written using one of the <code>write</code> methods is less than
     * the cipher's block size, no bytes will be written out.
     *
     * @exception IOException if an I/O error occurs.
     * @since JCE1.2
     */
    public void flush()
    throws IOException
    {
        super.flush();
    }

    /**
     * Closes this output stream and releases any system resources 
     * associated with this stream.
     * <p>
     * This method invokes the <code>doFinal</code> method of the encapsulated
     * cipher object, which causes any bytes buffered by the encapsulated
     * cipher to be processed. The result is written out by calling the
     * <code>flush</code> method of this output stream.
     * <p>
     * This method resets the encapsulated cipher object to its initial state
     * and calls the <code>close</code> method of the underlying output
     * stream.
     *
     * @exception IOException if an I/O error occurs.
     * @since JCE1.2
     */
    public void close()
           throws IOException
    {
        try
        {
                byte[]  bytes = c.doFinal();

                if (bytes != null)
                {
                    out.write(bytes, 0, bytes.length);
                }
        }
        catch (Exception e)
        {
            throw new IOException("Error closing stream: " + e.toString());
        }

        flush();

        super.close();
    }
}