/*
 * [TestProgressFilter.java]
 *
 * Summary: Demonstrate use of ProgressFilters.
 *
 * Copyright: (c) 2009-2017 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.8+
 *
 * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
 *
 * Version History:
 *  1.0 2007-08-03
 */
package com.mindprod.example;

import com.mindprod.common18.EIO;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import static java.lang.System.*;

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

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

    /**
     * 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.
        out.println( progress );
        }

    /**
     * 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, EIO.UTF8 );
            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 )
            {
            err.println();
            e.printStackTrace( System.err );
            err.println();
            }
        }

    /**
     * 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;
        }
    }