Filter


When preforming operations on data it is usefull to have the ability
to 'insert' classes that add functionality without 
duplicating work. The filter pattern provides for this.



The super class for getting data from a stream.
Used for decoupling.
-----------------------------------------------------
public abstract class InStream{
    public abstract int getData(byte[] array) throws IOException;

}
-----------------------------------------------------


This is the concrete class that delegetes the actual
reading of bytes to the RandomAccessFile class.
This class extends the InStream class. This keeps
this class and any class that may use it (in this 
example the FilterInStream class) from being coupled.
-----------------------------------------------------
public class FileInStream extends InStream{
    private RandomAccessFile file;

    public FileInStream(String fName) throws IOException{
        file = new RandomAccessFile(fName, "r");
    }

    public int getData(byte[] array) throws IOException{
        return file.read(array);
    }
}
-----------------------------------------------------


This is the super class to all the filter classes.
All filters will extend this class.
Any class that extends InStream can be used by it.
-----------------------------------------------------
public class FilterInStream extends InStream{
    private InStream inStream;

    public FilterInStream(InStream inStream) throws IOException{
        this.inStream = inStream;
    }
    public int getData(byte[] array) throws IOException{
        return inStream.getData(array);
    }
}
-----------------------------------------------------


This is the 'filter' class. It adds funtionality to 
the FilterInStream class.
In this case the added functionality is the ability
to keep a total of the bytes read by this class.
-----------------------------------------------------
public class ByteCountInStream extends FilterInStream{
    private long  totalCount = 0;

    public ByteCountInStream(InStream inStream) throws IOException{
        super(inStream);
    }

    public int getData(byte[] array) throws IOException{
        int count = super.getData(array);
        if(count>0)
            totalCount += count;
        return count;
    }

    public long getTotalCount(){return this.totalCount;}
}
-----------------------------------------------------


Test stub to run it all.
-----------------------------------------------------
class Test{

    public static void main(String[] args){
        System.out.println("class: Test, method: main");

        String filename = "testfile";
        byte[] array = new byte[128];
        
        try{
            ByteCountInStream byteCountInStream = 
                new ByteCountInStream(new FileInStream(filename));
            
            int count =0;
            while((count = byteCountInStream.getData(array)) >0){
                System.out.println(count);
            }
            System.out.println("total count: " +byteCountInStream.getTotalCount());
            
        }catch(Throwable t){}
    }
}
-----------------------------------------------------


Here is a tar with the above java classes in it. 
filter.tar