/*-
 * 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.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author Mikolaj Izdebski
 */
public class Main
{
    private static void processFile( InputStream is )
        throws IOException
    {
        if ( System.getProperty( "org.lbzip2.mbc" ) != null )
        {
            MBC mbc = new MBC( is, System.out );
            mbc.expand();
        }
        else
        {
            InputStream zis = new LBzip2InputStream( is );

            byte[] buf = new byte[4096];
            int r;
            while ( ( r = zis.read( buf ) ) != -1 )
                System.out.write( buf, 0, r );

            zis.close();
        }
    }

    public static void main( String[] args )
    {
        try
        {
            if ( args.length != 0 )
            {
                for ( String arg : args )
                {
                    processFile( new FileInputStream( arg ) );
                }
            }
            else
            {
                processFile( System.in );
            }
        }
        catch ( IOException e )
        {
            e.printStackTrace();
        }
    }
}

@@ -634,31 +634,27 @@
 java.io.
-FileInputStream
+IOException
 ;%0Aimport
@@ -659,34 +659,34 @@
 rt java.io.I
-OException
+nputStream
 ;%0Aimport jav
@@ -682,34 +682,35 @@
 %0Aimport java.io.
-In
+Out
 putStream;%0A%0A/**%0A
@@ -789,34 +789,527 @@
 oid 
-processFile( InputStream i
+copyStream( InputStream is, OutputStream os )%0A        throws IOException%0A    %7B%0A        byte%5B%5D buf = new byte%5B4096%5D;%0A        int r;%0A        while ( ( r = is.read( buf ) ) != -1 )%0A            os.write( buf, 0, r );%0A%0A    %7D%0A%0A    private static void compress( InputStream is, OutputStream os )%0A        throws IOException%0A    %7B%0A        LBzip2OutputStream zos = new LBzip2OutputStream( os, 900000 );%0A        copyStream( is, zos );%0A        zos.finish();%0A    %7D%0A%0A    private static void decompress( InputStream is, OutputStream o
 s )%0A
@@ -1448,26 +1448,18 @@
 BC( is, 
-System.out
+os
  );%0A    
@@ -1568,25 +1568,24 @@
 ream( is );%0A
-%0A
             
@@ -1588,35 +1588,29 @@
     
-byte%5B%5D buf = new byte%5B4096%5D
+copyStream( zis, os )
 ;%0A  
@@ -1619,143 +1619,148 @@
     
+%7D%0A
     
-int r;%0A            while ( ( r = zis.read( buf ) ) != -1 )%0A                System.out.write( buf, 0, r );%0A%0A            zis.close();
+%7D%0A%0A    private static void processStream( InputStream is, OutputStream os )%0A        throws IOException%0A    %7B%0A        if ( decompress )
 %0A   
@@ -1768,67 +1768,53 @@
     
-%7D%0A
+%7B%0A  
     
-%7D%0A%0A
     
-public static void main( String%5B%5D arg
+  decompress( is, o
 s )
+;
 %0A    
-%7B
+    %7D
 %0A   
@@ -1822,11 +1822,12 @@
     
-try
+else
 %0A   
@@ -1849,31 +1849,27 @@
     
-if ( args.length != 0
+compress( is, os
  )
+;
 %0A   
@@ -1877,42 +1877,92 @@
     
+%7D%0A
     
-%7B
+%7D%0A
 %0A    
-            for 
+private static boolean decompress;%0A%0A    public static void main
 ( String
  arg
@@ -1961,14 +1961,10 @@
 ring
- arg :
+%5B%5D
  arg
@@ -1971,27 +1971,26 @@
 s )%0A    
-   
+%7B%0A
         
  %7B%0A     
@@ -1973,34 +1973,35 @@
 )%0A    %7B%0A        
- %7B
+try
 %0A               
@@ -1985,32 +1985,34 @@
     try%0A        
+%7B%0A
             proc
@@ -2011,118 +2011,65 @@
     
-processFile( new FileInputStream( arg ) );%0A                %7D%0A            %7D%0A            else%0A            %7B%0A    
+decompress = args.length %3E 0 && args%5B0%5D.equals( %22-d%22 );%0A%0A
     
@@ -2087,12 +2087,14 @@
 cess
-File
+Stream
 ( Sy
@@ -2104,25 +2104,23 @@
 m.in
- );%0A            %7D
+, System.out );
 %0A   

Rewrite Main to allow compression