Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / crypto / CipherInputStream.java
blob35d4956092d0271d1f9f549cbb4fb52506989359
1 /* CipherInputStream.java -- Filters input through a cipher.
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.crypto;
41 import java.io.FilterInputStream;
42 import java.io.IOException;
43 import java.io.InputStream;
45 /**
46 * This is an {@link java.io.InputStream} that filters its data
47 * through a {@link Cipher} before returning it. The <code>Cipher</code>
48 * argument must have been initialized before it is passed to the
49 * constructor.
51 * @author Casey Marshall (csm@gnu.org)
53 public class CipherInputStream extends FilterInputStream
56 // Constants and variables.
57 // ------------------------------------------------------------------------
59 /**
60 * The underlying {@link Cipher} instance.
62 private Cipher cipher;
64 /**
65 * Data that has been transformed but not read.
67 private byte[] outBuffer;
69 /**
70 * The offset into {@link #outBuffer} where valid data starts.
72 private int outOffset;
74 /**
75 * The number of valid bytes in the {@link #outBuffer}.
77 private int outLength;
79 /**
80 * Byte buffer that is filled with raw data from the underlying input
81 * stream.
83 private byte[][] inBuffer;
85 /**
86 * The amount of bytes in inBuffer[0] that may be input to the cipher.
88 private int inLength;
90 /**
91 * We set this when the cipher block size is 1, meaning that we can
92 * transform any amount of data.
94 private boolean isStream;
96 private static final int VIRGIN = 0; // I am born.
97 private static final int LIVING = 1; // I am nailed to the hull.
98 private static final int DYING = 2; // I am eaten by sharks.
99 private static final int DEAD = 3;
100 private int state;
102 // Constructors.
103 // ------------------------------------------------------------------------
106 * Creates a new input stream with a source input stream and cipher.
108 * @param in The underlying input stream.
109 * @param cipher The cipher to filter data through.
111 public CipherInputStream(InputStream in, Cipher cipher)
113 this(in);
114 this.cipher = cipher;
115 if (!(isStream = cipher.getBlockSize() == 1))
117 inBuffer = new byte[2][];
118 inBuffer[0] = new byte[cipher.getBlockSize()];
119 inBuffer[1] = new byte[cipher.getBlockSize()];
120 inLength = 0;
121 outBuffer = new byte[cipher.getBlockSize()];
122 outOffset = outLength = 0;
123 state = VIRGIN;
128 * Creates a new input stream without a cipher. This constructor is
129 * <code>protected</code> because this class does not work without an
130 * underlying cipher.
132 * @param in The underlying input stream.
134 protected CipherInputStream(InputStream in)
136 super(in);
139 // Instance methods overriding java.io.FilterInputStream.
140 // ------------------------------------------------------------------------
143 * Returns the number of bytes available without blocking. The value
144 * returned by this method is never greater than the underlying
145 * cipher's block size.
147 * @return The number of bytes immediately available.
148 * @throws java.io.IOException If an I/O exception occurs.
150 public int available() throws IOException
152 if (isStream)
153 return super.available();
154 return outLength - outOffset;
158 * Close this input stream. This method merely calls the {@link
159 * java.io.InputStream#close()} method of the underlying input stream.
161 * @throws java.io.IOException If an I/O exception occurs.
163 public void close() throws IOException
165 super.close();
169 * Read a single byte from this input stream; returns -1 on the
170 * end-of-file.
172 * @return The byte read, or -1 if there are no more bytes.
173 * @throws java.io.IOExcpetion If an I/O exception occurs.
175 public int read() throws IOException
177 if (isStream)
179 byte[] buf = new byte[1];
180 int in = super.read();
181 if (in == -1)
182 return -1;
183 buf[0] = (byte) in;
186 cipher.update(buf, 0, 1, buf, 0);
188 catch (ShortBufferException shouldNotHappen)
190 throw new IOException(shouldNotHappen.getMessage());
192 return buf[0] & 0xFF;
194 if (state == DEAD) return -1;
195 if (available() == 0) nextBlock();
196 if (state == DEAD) return -1;
197 return outBuffer[outOffset++] & 0xFF;
201 * Read bytes into an array, returning the number of bytes read or -1
202 * on the end-of-file.
204 * @param buf The byte array to read into.
205 * @param off The offset in <code>buf</code> to start.
206 * @param len The maximum number of bytes to read.
207 * @return The number of bytes read, or -1 on the end-of-file.
208 * @throws java.io.IOException If an I/O exception occurs.
210 public int read(byte[] buf, int off, int len) throws IOException
212 if (isStream)
214 len = super.read(buf, off, len);
217 cipher.update(buf, off, len, buf, off);
219 catch (ShortBufferException shouldNotHappen)
221 throw new IOException(shouldNotHappen.getMessage());
223 return len;
226 int count = 0;
227 while (count < len)
229 if (available() == 0)
230 nextBlock();
231 if (state == DEAD)
233 if (count > 0) return count;
234 else return -1;
236 int l = Math.min(available(), len - count);
237 System.arraycopy(outBuffer, outOffset, buf, count+off, l);
238 count += l;
239 outOffset = outLength = 0;
241 return count;
245 * Read bytes into an array, returning the number of bytes read or -1
246 * on the end-of-file.
248 * @param buf The byte arry to read into.
249 * @return The number of bytes read, or -1 on the end-of-file.
250 * @throws java.io.IOException If an I/O exception occurs.
252 public int read(byte[] buf) throws IOException
254 return read(buf, 0, buf.length);
258 * Skip a number of bytes. This class only supports skipping as many
259 * bytes as are returned by {@link #available()}, which is the number
260 * of transformed bytes currently in this class's internal buffer.
262 * @param bytes The number of bytes to skip.
263 * @return The number of bytes skipped.
265 public long skip(long bytes) throws IOException
267 if (isStream)
269 return super.skip(bytes);
271 long ret = 0;
272 if (bytes > 0 && available() > 0)
274 ret = available();
275 outOffset = outLength = 0;
277 return ret;
281 * Returns whether or not this input stream supports the {@link
282 * #mark(long)} and {@link #reset()} methods; this input stream does
283 * not, however, and invariably returns <code>false</code>.
285 * @return <code>false</code>
287 public boolean markSupported()
289 return false;
293 * Set the mark. This method is unsupported and is empty.
295 * @param mark Is ignored.
297 public void mark(int mark)
302 * Reset to the mark. This method is unsupported and is empty.
304 public void reset() throws IOException
306 throw new IOException("reset not supported");
309 // Own methods.
310 // -------------------------------------------------------------------------
312 private void nextBlock() throws IOException
314 byte[] temp = inBuffer[0];
315 inBuffer[0] = inBuffer[1];
316 inBuffer[1] = temp;
317 int count = 0;
318 boolean eof = false;
320 if (state == VIRGIN || state == LIVING)
324 int l = in.read(inBuffer[1], count, inBuffer[1].length - count);
325 if (l == -1)
327 eof = true;
328 break;
330 count += l;
332 while (count < inBuffer[1].length);
337 switch (state)
339 case VIRGIN:
340 state = LIVING;
341 nextBlock();
342 break;
343 case LIVING:
344 if (eof)
346 if (count > 0)
348 outOffset = cipher.update(inBuffer[0], 0, inLength, outBuffer, 0);
349 state = DYING;
351 else
353 outOffset = cipher.doFinal(inBuffer[0], 0, inLength, outBuffer, 0);
354 state = DEAD;
357 else
359 outOffset = cipher.update(inBuffer[0], 0, inLength, outBuffer, 0);
361 break;
362 case DYING:
363 outOffset = cipher.doFinal(inBuffer[0], 0, inLength, outBuffer, 0);
364 state = DEAD;
365 break;
366 case DEAD:
369 catch (ShortBufferException sbe)
371 throw new IOException(sbe.toString());
373 catch (BadPaddingException bpe)
375 throw new IOException(bpe.toString());
377 catch (IllegalBlockSizeException ibse)
379 throw new IOException(ibse.toString());
381 inLength = count;