/*-
 * Copyright (c) 2014 Mikolaj Izdebski
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.lbzip2;

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

/**
 * @author Mikolaj Izdebski
 */
public class LBzip2OutputStream
    extends OutputStream
{
    private final OutputStream os;

    private final StreamComposer composer;

    private final UncompressedBlock block;

    private final byte[] buf1 = new byte[1];

    private final byte[] buf = new byte[4096];

    public LBzip2OutputStream( OutputStream os, int maxBlockSize )
        throws IOException
    {
        this.os = os;
        composer = new StreamComposer( maxBlockSize );
        block = new UncompressedBlock( maxBlockSize );
    }

    @Override
    public void write( int b )
        throws IOException
    {
        buf1[0] = (byte) b;
        write( buf1, 0, 1 );
    }

    @Override
    public void write( byte[] buf, int off, int avail )
        throws IOException
    {
        while ( avail > 0 )
        {
            int written = block.write( buf, off, avail );
            off += written;
            avail -= written;

            if ( block.isFull() )
                transmit();
        }
    }

    private void transmit()
        throws IOException
    {
        if ( !block.isEmpty() )
            composer.addBlock( block.compress() );

        while ( !composer.isEmpty() )
            os.write( buf, 0, composer.read( buf ) );
    }

    @Override
    public void flush()
        throws IOException
    {
        transmit();
        os.flush();
    }

    @Override
    public void close()
        throws IOException
    {
        transmit();
        composer.finish();
        transmit();
        os.close();
    }

    public static void main( String[] args )
    {
        try
        {
            InputStream is = System.in;
            OutputStream os = new LBzip2OutputStream( System.out, 900000 );
            byte[] buf = new byte[4096];
            int n;
            while ( ( n = is.read( buf ) ) > 0 )
            {
                os.write( buf, 0, n );
            }
            is.close();
            os.close();
        }
        catch ( Throwable e )
        {
            e.printStackTrace();
        }
    }
}

@@ -705,16 +705,74 @@
 tream;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0A
 /**%0A * @
@@ -858,16 +858,104 @@
 tream%0A%7B%0A
+    private final Logger logger = LoggerFactory.getLogger( LBzip2OutputStream.class );%0A%0A
     priv
@@ -1833,24 +1833,106 @@
 .isFull() )%0A
+            %7B%0A                logger.trace( %22Block full, forcing transmission%22 );%0A
             
@@ -1939,32 +1939,46 @@
     transmit();%0A
+            %7D%0A
         %7D%0A    %7D%0A
@@ -2063,32 +2063,86 @@
 ock.isEmpty() )%0A
+        %7B%0A            logger.trace( %22Adding block%22 );%0A
             comp
@@ -2175,16 +2175,26 @@
 ess() );
+%0A        %7D
 %0A%0A      
@@ -2217,32 +2217,91 @@
 ser.isEmpty() )%0A
+        %7B%0A            logger.trace( %22Emptying composer%22 );%0A
             os.w
@@ -2334,24 +2334,34 @@
 d( buf ) );%0A
+        %7D%0A
     %7D%0A%0A    @
@@ -2530,38 +2530,80 @@
 ows IOException%0A
-
     %7B%0A
+        logger.trace( %22Closing stream%22 );%0A
         transmit

Add basic LBzip2OutputStream logging