001    /* CipherOutputStream.java -- Filters output through a cipher.
002       Copyright (C) 2004  Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    
039    package javax.crypto;
040    
041    import java.io.FilterOutputStream;
042    import java.io.IOException;
043    import java.io.OutputStream;
044    
045    /**
046     * A filtered output stream that transforms data written to it with a
047     * {@link Cipher} before sending it to the underlying output stream.
048     * 
049     * @author Casey Marshall (csm@gnu.org)
050     */
051    public class CipherOutputStream extends FilterOutputStream
052    {
053      /** The underlying cipher. */
054      private Cipher cipher;
055    
056      /**
057       * Create a new cipher output stream. The cipher argument must have already
058       * been initialized.
059       * 
060       * @param out The sink for transformed data.
061       * @param cipher The cipher to transform data with.
062       */
063      public CipherOutputStream(OutputStream out, Cipher cipher)
064      {
065        super(out);
066        this.cipher = (cipher != null) ? cipher : new NullCipher();
067      }
068    
069      /**
070       * Create a cipher output stream with no cipher.
071       *
072       * @param out The sink for transformed data.
073       */
074      protected CipherOutputStream(OutputStream out)
075      {
076        super(out);
077      }
078    
079      /**
080       * Close this output stream, and the sink output stream.
081       * <p>
082       * This method will first invoke the {@link Cipher#doFinal()} method of the
083       * underlying {@link Cipher}, and writes the output of that method to the
084       * sink output stream.
085       * 
086       * @throws IOException If an I/O error occurs, or if an error is caused by
087       *           finalizing the transformation.
088       */
089      public void close() throws IOException
090      {
091        try
092          {
093            out.write(cipher.doFinal());
094            out.flush();
095            out.close();
096          }
097        catch (Exception cause)
098          {
099            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    }