001    package org.bouncycastle.crypto.io;
002    
003    import java.io.FilterOutputStream;
004    import java.io.IOException;
005    import java.io.OutputStream;
006    import org.bouncycastle.crypto.BufferedBlockCipher;
007    import org.bouncycastle.crypto.StreamCipher;
008    
009    public class CipherOutputStream extends FilterOutputStream
010    {
011        private BufferedBlockCipher field_74849_a;
012        private StreamCipher field_74847_b;
013        private byte[] field_74848_c = new byte[1];
014        private byte[] field_74846_d;
015    
016        public CipherOutputStream(OutputStream par1OutputStream, BufferedBlockCipher par2BufferedBlockCipher)
017        {
018            super(par1OutputStream);
019            this.field_74849_a = par2BufferedBlockCipher;
020            this.field_74846_d = new byte[par2BufferedBlockCipher.func_71792_a()];
021        }
022    
023        public void write(int par1) throws IOException
024        {
025            this.field_74848_c[0] = (byte)par1;
026    
027            if (this.field_74849_a != null)
028            {
029                int var2 = this.field_74849_a.func_71791_a(this.field_74848_c, 0, 1, this.field_74846_d, 0);
030    
031                if (var2 != 0)
032                {
033                    this.out.write(this.field_74846_d, 0, var2);
034                }
035            }
036            else
037            {
038                this.out.write(this.field_74847_b.func_74851_a((byte)par1));
039            }
040        }
041    
042        public void write(byte[] par1) throws IOException
043        {
044            this.write(par1, 0, par1.length);
045        }
046    
047        public void write(byte[] par1, int par2, int par3) throws IOException
048        {
049            byte[] var4;
050    
051            if (this.field_74849_a != null)
052            {
053                var4 = new byte[this.field_74849_a.func_71789_b(par3)];
054                int var5 = this.field_74849_a.func_71791_a(par1, par2, par3, var4, 0);
055    
056                if (var5 != 0)
057                {
058                    this.out.write(var4, 0, var5);
059                }
060            }
061            else
062            {
063                var4 = new byte[par3];
064                this.field_74847_b.func_74850_a(par1, par2, par3, var4, 0);
065                this.out.write(var4, 0, par3);
066            }
067        }
068    
069        public void flush() throws IOException
070        {
071            super.flush();
072        }
073    
074        public void close() throws IOException
075        {
076            try
077            {
078                if (this.field_74849_a != null)
079                {
080                    byte[] var1 = new byte[this.field_74849_a.func_71789_b(0)];
081                    int var2 = this.field_74849_a.func_71790_a(var1, 0);
082    
083                    if (var2 != 0)
084                    {
085                        this.out.write(var1, 0, var2);
086                    }
087                }
088            }
089            catch (Exception var3)
090            {
091                throw new IOException("Error closing stream: " + var3.toString());
092            }
093    
094            this.flush();
095            super.close();
096        }
097    }