package org.bouncycastle.asn1;

import java.io.IOException;
import java.io.OutputStream;

import org.bouncycastle.asn1.DEROctetString;

public class BEROctetStringGenerator
    extends BERGenerator
{
    public BEROctetStringGenerator(OutputStream out) 
        throws IOException
    {
        super(out);
        
        writeBERHeader(DERTags.CONSTRUCTED | DERTags.OCTET_STRING);
    }

    public BEROctetStringGenerator(
        OutputStream out,
        int tagNo,
        boolean isExplicit) 
        throws IOException
    {
        super(out, tagNo, isExplicit);
        
        writeBERHeader(DERTags.CONSTRUCTED | DERTags.OCTET_STRING);
    }
    
    public OutputStream getOctetOutputStream()
    {
        return new BEROctetStream();
    }

    public OutputStream getOctetOutputStream(
        byte[] buf)
    {
        return new BufferedBEROctetStream(buf);
    }
    
    private class BEROctetStream
        extends OutputStream
    {
        private byte[] _buf = new byte[1];

        public void write(
            int b)
            throws IOException
        {
            _buf[0] = (byte)b;
            
            _out.write(new DEROctetString(_buf).getEncoded()); 
        }
        
        public void write(
            byte[] buf) 
            throws IOException
        {
            if (buf.length > 0)
            {
                _out.write(new DEROctetString(buf).getEncoded());
            }
        }
        
        public void write(
            byte[] buf,
            int    offSet,
            int    len) 
            throws IOException
        {
            if (len > 0)
            {
                byte[] bytes = new byte[len];

                System.arraycopy(buf, offSet, bytes, 0, len);

                _out.write(new DEROctetString(bytes).getEncoded());
            }
        }
        
        public void close() 
            throws IOException
        {
             writeBEREnd();
        }
    }
    
    private class BufferedBEROctetStream
        extends OutputStream
    {
        private byte[] _buf;
        private int    _off;
    
        BufferedBEROctetStream(
            byte[] buf)
        {
            _buf = buf;
            _off = 0;
        }
        
        public void write(
            int b)
            throws IOException
        {
            _buf[_off++] = (byte)b;

            if (_off == _buf.length)
            {
                _out.write(new DEROctetString(_buf).getEncoded());
                _off = 0;
            }
        }
        
        public void close() 
            throws IOException
        {
            if (_off != 0)
            {
                byte[] bytes = new byte[_off];
                System.arraycopy(_buf, 0, bytes, 0, _off);
                
                _out.write(new DEROctetString(bytes).getEncoded());
            }
            
             writeBEREnd();
        }
    }
}

@@ -2517,32 +2517,33 @@
 %7D%0A        %7D%0A
+%0A
         
 %0A        pub
@@ -2522,32 +2522,571 @@
      %7D%0A%0A        
+public void write(byte%5B%5D b, int off, int len) throws IOException%0A        %7B%0A            while (len %3E 0)%0A            %7B%0A                int numToCopy = Math.min(len, _buf.length - _off);%0A                System.arraycopy(b, off, _buf, _off, numToCopy);%0A%0A                _off += numToCopy;%0A                if (_off %3C _buf.length)%0A                    break;%0A%0A                _out.write(new DEROctetString(_buf).getEncoded());%0A                _off = 0;%0A%0A                off += numToCopy;%0A                len -= numToCopy;%0A            %7D%0A        %7D%0A
 %0A        public 

Implement write(byte[],int,int) as optimisation in BufferedBEROctetStream