package com.mindprod.example;

import java.io.*;

/**
 * Demonstrate use of ProgressFilters
 * <p/>
 * composed with IntelliJ IDEA
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0,2007-08-03
 */
public final class TestProgressFilter extends FilterInputStream
    {
    /**
     * tracks how many bytes we have read so far
     */
    private long progress;

    // -------------------------- PUBLIC INSTANCE  METHODS --------------------------
    /**
     * Reads the next byte of data from this input stream.
     */
    public int read() throws IOException
        {
        int ret = super.read();
        progress++;
        reportProgress();
        return ret;
        }

    /**
     * Reads up to byte.length bytes of data from this input stream into an array of bytes.
     */
    public int read( byte[] b ) throws IOException
        {
        int ret = super.read( b );
        progress += b.length;
        reportProgress();
        return ret;
        }

    /**
     * Reads up to len bytes of data from this input stream into an array of bytes.
     */
    public int read( byte[] b, int off, int len ) throws IOException
        {
        int ret = super.read( b, off, len );
        progress += len;
        reportProgress();
        return ret;
        }

    /**
     * Skips over and discards n bytes of data from this input stream.   *
     */
    public long skip( long n ) throws IOException
        {
        long ret = super.skip( n );
        progress += n;
        reportProgress();
        return ret;
        }

// --------------------------- CONSTRUCTORS ---------------------------

    /**
     * Constructor
     *
     * @param in input stream to progress monitor
     */
    private TestProgressFilter( InputStream in )
        {
        super( in );
        }

// -------------------------- OTHER METHODS --------------------------

    /**
     * report the progress to the user
     */
    @SuppressWarnings( { "WeakerAccess" } )
    protected void reportProgress()
        {
        // this is a dummy routine. A real version would send an event
        // to a progress meter.
        System.out.println( progress );
        }

// --------------------------- main() method ---------------------------

    /**
     * test driver
     *
     * @param args arg[0] is UTF-8 filename to read
     */
    public static void main( String args[] )
        {
        try
            {
            // O P E N
            FileInputStream fis = new FileInputStream( new File( args[ 0 ] ) );
            TestProgressFilter tpfis = new TestProgressFilter( fis );
            InputStreamReader eisr = new InputStreamReader( tpfis, "UTF-8" );
            BufferedReader br = new BufferedReader( eisr, 400/* buffsize */ );

            // R E A D
            while ( true )
                {
                if ( br.readLine() == null )
                    {
                    // line == null means EOF
                    break;
                    }
                }

            // C L O S E
            br.close();
            }
        catch ( Exception e )
            {
            System.err.println( e );
            e.printStackTrace( System.err );
            }
        }
    }