class COSOutputStream extends FilterOutputStream
{
    private final List<Filter> filters;
    private final COSDictionary parameters;
    private final RandomAccessStreamCache streamCache;
    private RandomAccess buffer;

    /**
     * Creates a new COSOutputStream writes to an encoded COS stream.
     * 
     * @param filters Filters to apply.
     * @param parameters Filter parameters.
     * @param output Encoded stream.
     * @param streamCache Stream cache to use.
     * 
     * @throws IOException If there was an error creating a temporary buffer
     */
    COSOutputStream(List<Filter> filters, COSDictionary parameters, OutputStream output,
            RandomAccessStreamCache streamCache) throws IOException
    {
        super(output);
        this.filters = filters;
        this.parameters = parameters;
        this.streamCache = streamCache;
        buffer = filters.isEmpty() ? null : streamCache.createBuffer();
    }

    @Override
    public void write(byte[] b) throws IOException
    {
        write(b, 0, b.length);
    }

    @Override
    public void write(byte[] b, int off, int len) throws IOException
    {
        if (buffer != null)
        {
            buffer.write(b, off, len);
        }
        else
        {
            super.write(b, off, len);
        }
    }

    @Override
    public void write(int b) throws IOException
    {
        if (buffer != null)
        {
            buffer.write(b);
        }
        else
        {
            super.write(b);
        }
    }

    @Override
    public void flush() throws IOException
    {
        if (buffer == null)
        {
            super.flush();
        }
    }

    @Override
    public void close() throws IOException
    {<FILL_FUNCTION_BODY>}
}

        try
        {
            if (buffer != null)
            {
                try
                {
                    // apply filters in reverse order
                    for (int i = filters.size() - 1; i >= 0; i--)
                    {
                        try (InputStream unfilteredIn = new RandomAccessInputStream(buffer))
                        {
                            if (i == 0)
                            {
                                /*
                                 * The last filter to run can encode directly to the enclosed output
                                 * stream.
                                 */
                                filters.get(i).encode(unfilteredIn, out, parameters, i);
                            }
                            else
                            {
                                RandomAccess filteredBuffer = streamCache.createBuffer();
                                try (OutputStream filteredOut = new RandomAccessOutputStream(filteredBuffer))
                                {
                                    filters.get(i).encode(unfilteredIn, filteredOut, parameters, i);
                                }
                                finally
                                {
                                    buffer.close();
                                    buffer = filteredBuffer;
                                }
                            }
                        }
                    }
                }
                finally
                {
                    buffer.close();
                    buffer = null;
                }
            }
        }
        finally
        {
            super.close();
        }
    
<methods>public void <init>(java.io.OutputStream) ,public void close() throws java.io.IOException,public void flush() throws java.io.IOException,public void write(int) throws java.io.IOException,public void write(byte[]) throws java.io.IOException,public void write(byte[], int, int) throws java.io.IOException<variables>private final java.lang.Object closeLock,private volatile boolean closed,protected java.io.OutputStream out