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()
{
out.println( progress );
}
/**
* test driver
*
* @param args arg[0] is UTF-8 filename to read
*/
public static void main( String args[] )
{
try
{
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);
while ( true )
{
if ( br.readLine() == null )
{
break;
}
}
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;
}
}