Source for javax.crypto.CipherOutputStream

   1: /* CipherOutputStream.java -- Filters output through a cipher.
   2:    Copyright (C) 2004  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   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.
  10: 
  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.
  15: 
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  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.
  25: 
  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. */
  37: 
  38: 
  39: package javax.crypto;
  40: 
  41: import java.io.FilterOutputStream;
  42: import java.io.IOException;
  43: import java.io.OutputStream;
  44: 
  45: /**
  46:  * A filtered output stream that transforms data written to it with a
  47:  * {@link Cipher} before sending it to the underlying output stream.
  48:  * 
  49:  * @author Casey Marshall (csm@gnu.org)
  50:  */
  51: public class CipherOutputStream extends FilterOutputStream
  52: {
  53:   /** The underlying cipher. */
  54:   private Cipher cipher;
  55: 
  56:   /**
  57:    * Create a new cipher output stream. The cipher argument must have already
  58:    * been initialized.
  59:    * 
  60:    * @param out The sink for transformed data.
  61:    * @param cipher The cipher to transform data with.
  62:    */
  63:   public CipherOutputStream(OutputStream out, Cipher cipher)
  64:   {
  65:     super(out);
  66:     this.cipher = (cipher != null) ? cipher : new NullCipher();
  67:   }
  68: 
  69:   /**
  70:    * Create a cipher output stream with no cipher.
  71:    *
  72:    * @param out The sink for transformed data.
  73:    */
  74:   protected CipherOutputStream(OutputStream out)
  75:   {
  76:     super(out);
  77:   }
  78: 
  79:   /**
  80:    * Close this output stream, and the sink output stream.
  81:    * <p>
  82:    * This method will first invoke the {@link Cipher#doFinal()} method of the
  83:    * underlying {@link Cipher}, and writes the output of that method to the
  84:    * sink output stream.
  85:    * 
  86:    * @throws IOException If an I/O error occurs, or if an error is caused by
  87:    *           finalizing the transformation.
  88:    */
  89:   public void close() throws IOException
  90:   {
  91:     try
  92:       {
  93:         out.write(cipher.doFinal());
  94:         out.flush();
  95:         out.close();
  96:       }
  97:     catch (Exception cause)
  98:       {
  99:         IOException ioex = new IOException(String.valueOf(cause));
 100:         ioex.initCause(cause);
 101:         throw ioex;
 102:       }
 103:   }
 104: 
 105:   /**
 106:    * Flush any pending output.
 107:    *
 108:    * @throws IOException If an I/O error occurs.
 109:    */
 110:   public void flush() throws IOException
 111:   {
 112:     out.flush();
 113:   }
 114: 
 115:   /**
 116:    * Write a single byte to the output stream.
 117:    * 
 118:    * @param b The next byte.
 119:    * @throws IOException If an I/O error occurs, or if the underlying cipher is
 120:    *           not in the correct state to transform data.
 121:    */
 122:   public void write(int b) throws IOException
 123:   {
 124:     write(new byte[] { (byte) b }, 0, 1);
 125:   }
 126: 
 127:   /**
 128:    * Write a byte array to the output stream.
 129:    * 
 130:    * @param buf The next bytes.
 131:    * @throws IOException If an I/O error occurs, or if the underlying cipher is
 132:    *           not in the correct state to transform data.
 133:    */
 134:   public void write(byte[] buf) throws IOException
 135:   {
 136:     write(buf, 0, buf.length);
 137:   }
 138: 
 139:   /**
 140:    * Write a portion of a byte array to the output stream.
 141:    * 
 142:    * @param buf The next bytes.
 143:    * @param off The offset in the byte array to start.
 144:    * @param len The number of bytes to write.
 145:    * @throws IOException If an I/O error occurs, or if the underlying cipher is
 146:    *           not in the correct state to transform data.
 147:    */
 148:   public void write(byte[] buf, int off, int len) throws IOException
 149:   {
 150:     byte[] b = cipher.update(buf, off, len);
 151:     if (b != null)
 152:       out.write(b);
 153:   }
 154: }