~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to jce/src/javax/crypto/CipherOutputStream.java

  • Committer: Brian Thomason
  • Date: 2011-12-20 17:20:32 UTC
  • Revision ID: brian.thomason@canonical.com-20111220172032-rdtm13jgdxtksacr
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package javax.crypto;
 
2
 
 
3
import java.io.OutputStream;
 
4
import java.io.IOException;
 
5
import java.io.FilterOutputStream;
 
6
 
 
7
/**
 
8
 * A CipherOutputStream is composed of an OutputStream and a Cipher so
 
9
 * that write() methods first process the data before writing them out
 
10
 * to the underlying OutputStream.  The cipher must be fully
 
11
 * initialized before being used by a CipherOutputStream.
 
12
 * <p>
 
13
 * For example, if the cipher is initialized for encryption, the
 
14
 * CipherOutputStream will attempt to encrypt data before writing out the
 
15
 * encrypted data.
 
16
 * <p>
 
17
 * This class adheres strictly to the semantics, especially the
 
18
 * failure semantics, of its ancestor classes
 
19
 * java.io.OutputStream and java.io.FilterOutputStream.  This class
 
20
 * has exactly those methods specified in its ancestor classes, and
 
21
 * overrides them all.  Moreover, this class catches all exceptions
 
22
 * that are not thrown by its ancestor classes.
 
23
 * <p>
 
24
 * It is crucial for a programmer using this class not to use
 
25
 * methods that are not defined or overriden in this class (such as a
 
26
 * new method or constructor that is later added to one of the super
 
27
 * classes), because the design and implementation of those methods
 
28
 * are unlikely to have considered security impact with regard to
 
29
 * CipherOutputStream.
 
30
 *
 
31
 * @since JCE1.2
 
32
 * @see OutputStream
 
33
 * @see FilterOutputStream
 
34
 * @see Cipher
 
35
 * @see CipherInputStream
 
36
 */
 
37
public class CipherOutputStream
 
38
    extends FilterOutputStream
 
39
{
 
40
    private Cipher          c;
 
41
 
 
42
    private byte[]          oneByte = new byte[1];
 
43
 
 
44
    /**
 
45
     * Constructs a CipherOutputStream from an OutputStream and a
 
46
     * Cipher.
 
47
     */
 
48
    public CipherOutputStream(
 
49
        OutputStream    os,
 
50
        Cipher          c)
 
51
    {
 
52
        super(os);
 
53
        this.c = c;
 
54
    }
 
55
 
 
56
    /**
 
57
     * Constructs a CipherOutputStream from an OutputStream without
 
58
     * specifying a Cipher. This has the effect of constructing a
 
59
     * CipherOutputStream using a NullCipher.
 
60
     */
 
61
    protected CipherOutputStream(
 
62
        OutputStream    os)
 
63
    {
 
64
        this(os, new NullCipher());
 
65
    }
 
66
 
 
67
    /**
 
68
     * Writes the specified byte to this output stream.
 
69
     *
 
70
     * @param b the <code>byte</code>.
 
71
     * @exception IOException if an I/O error occurs.
 
72
     * @since JCE1.2
 
73
     */
 
74
    public void write(
 
75
        int b)
 
76
    throws IOException
 
77
    {
 
78
        oneByte[0] = (byte)b;
 
79
 
 
80
        byte[] bytes = c.update(oneByte, 0, 1);
 
81
 
 
82
        if (bytes != null)
 
83
        {
 
84
            out.write(bytes, 0, bytes.length);
 
85
        }
 
86
    }
 
87
 
 
88
    /**
 
89
     * Writes <code>b.length</code> bytes from the specified byte array 
 
90
     * to this output stream. 
 
91
     * <p>
 
92
     * The <code>write</code> method of
 
93
     * <code>CipherOutputStream</code> calls the <code>write</code>
 
94
     * method of three arguments with the three arguments
 
95
     * <code>b</code>, <code>0</code>, and <code>b.length</code>.
 
96
     *
 
97
     * @param b the data.
 
98
     * @exception IOException if an I/O error occurs.
 
99
     * @since JCE1.2
 
100
     * @see #write(byte[], int, int)
 
101
     */
 
102
    public void write(
 
103
        byte[]  b)
 
104
    throws IOException
 
105
    {
 
106
        write(b, 0, b.length);
 
107
    }
 
108
 
 
109
    /**
 
110
     * Writes <code>len</code> bytes from the specified byte array 
 
111
     * starting at offset <code>off</code> to this output stream.
 
112
     *
 
113
     * @param b the data.
 
114
     * @param off the start offset in the data.
 
115
     * @param len the number of bytes to write.
 
116
     * @exception IOException if an I/O error occurs.
 
117
     * @since JCE1.2
 
118
     */
 
119
    public void write(
 
120
        byte[]  b,
 
121
        int     off,
 
122
        int     len)
 
123
    throws IOException
 
124
    {
 
125
        byte[] bytes = c.update(b, off, len);
 
126
 
 
127
        if (bytes != null)
 
128
        {
 
129
            out.write(bytes, 0, bytes.length);
 
130
        }
 
131
    }
 
132
 
 
133
    /**
 
134
     * Flushes this output stream by forcing any buffered output bytes 
 
135
     * that have already been processed by the encapsulated cipher object
 
136
     * to be written out.
 
137
     * 
 
138
     * <p>
 
139
     * Any bytes buffered by the encapsulated cipher
 
140
     * and waiting to be processed by it will not be written out. For example,
 
141
     * if the encapsulated cipher is a block cipher, and the total number of
 
142
     * bytes written using one of the <code>write</code> methods is less than
 
143
     * the cipher's block size, no bytes will be written out.
 
144
     *
 
145
     * @exception IOException if an I/O error occurs.
 
146
     * @since JCE1.2
 
147
     */
 
148
    public void flush()
 
149
    throws IOException
 
150
    {
 
151
        super.flush();
 
152
    }
 
153
 
 
154
    /**
 
155
     * Closes this output stream and releases any system resources 
 
156
     * associated with this stream.
 
157
     * <p>
 
158
     * This method invokes the <code>doFinal</code> method of the encapsulated
 
159
     * cipher object, which causes any bytes buffered by the encapsulated
 
160
     * cipher to be processed. The result is written out by calling the
 
161
     * <code>flush</code> method of this output stream.
 
162
     * <p>
 
163
     * This method resets the encapsulated cipher object to its initial state
 
164
     * and calls the <code>close</code> method of the underlying output
 
165
     * stream.
 
166
     *
 
167
     * @exception IOException if an I/O error occurs.
 
168
     * @since JCE1.2
 
169
     */
 
170
    public void close()
 
171
           throws IOException
 
172
    {
 
173
        try
 
174
        {
 
175
                byte[]  bytes = c.doFinal();
 
176
 
 
177
                if (bytes != null)
 
178
                {
 
179
                    out.write(bytes, 0, bytes.length);
 
180
                }
 
181
        }
 
182
        catch (Exception e)
 
183
        {
 
184
            throw new IOException("Error closing stream: " + e.toString());
 
185
        }
 
186
 
 
187
        flush();
 
188
 
 
189
        super.close();
 
190
    }
 
191
}