Using Filter Streams in Java

Filter input streams read data from a preexisting input stream such as a FileInputStream and have an opportunity to work with or change the data before it is delivered to the client program. Filter output streams write data to a preexisting output stream such as a FileOutputStream and have an opportunity to work with or change the data before it is written onto the underlying stream. A stream filter sits between the source of the data and its eventual destination and applies a specific algorithm to the data.
Need to understand that filter streams in Java are very powerful. For example encryption,compression,translation,buffering,and much more.
Main filter classes are FilterInputStream and FilterOutputStream.
Here I will show example which shows how to extend filter streams and how to use them.
I am going to show example which uses new filter output and filter input streams. These streams I will use in main class.
What this example should do. It is simple.There are 3 classes:
Main which is main class for this example
MyOutputStream which extends FilterOutputStream (basic filter output stream) and shows symbols p,u,b,l,i,c in upper cases
MyInputStream which extends FilterInputStream (basic filter input stream) and shows symbols m,a,i,n in upper cases.
Let’s look at MyOutputStream:

import java.io.FilterOutputStream;import java.io.IOException;import java.io.OutputStream;class MyOutputStream extends FilterOutputStream{ public MyOutputStream(OutputStream out){ super(out); }  @Override  public void write(int b) throws IOException{ switch (b){ case 112: //p out.write(80);//P break; case 117: //u out.write(85);//U break; case 98: //b out.write(66);//B break; case 108: //l out.write(76);//L break; case 105: //i out.write(73);//I break; case 99: //c out.write(67);//C break; default:out.write(b); }  }  @Override  public void write(byte[] data,int offset,int length) throws IOException{ for (int i = offset;i < offset + length;i++){ this.write(data[i]); }  }}

MyInputStream:

import java.io.FilterInputStream;import java.io.IOException;import java.io.InputStream;public class MyInputStream extends FilterInputStream{ public MyInputStream(InputStream in){ super(in); }  @Override  public int read() throws IOException{ int b = in.read(); switch(b){ case 109: //m return 77;//M  case 97: //a return 65;//A  case 105: //i return 73;//I  case 110: //n return 78;//N  default:return b; }  }}

Main:

import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;public class Main{ public static void main(String[] args){ try{ String workDir = System.getProperty("user.dir"); File fileDir = new File(workDir,"src/com/thedevelopersinfo/tutorial/java/io/filterstream/example"); File readFile = new File(fileDir,"Main.java"); InputStream in = new FileInputStream(readFile);//  MyInputStream in = new MyInputStream(new FileInputStream(readFile)); OutputStream out = System.out; // Here's where the output stream is chained  // to the ASCII output stream.  MyOutputStream pout = new MyOutputStream(out); for (int c = in.read();c != -1;c = in.read()){pout.write(c);// out.write(c); }  out.close(); } catch (FileNotFoundException e){ } catch (IOException ex){ System.err.println(ex); }  }}

By default Main class works with MyOutputStream,for using MyInputStream uncomment lines 21,29 and comment lines 20,26,28.
As you can see it is very easy. This post doesn't cover subclasses of filter streams. It shows basic functionality and how to extend these streams.

 

Download code from this article.

Share

No related posts.

Leave a Reply

  

  

  

You can use these HTML tags

<a href=""title=""><abbr title=""><acronym title=""><b><blockquote cite=""><cite><code><del datetime=""><em><i><q cite=""><strike><strong>

A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna,tincidunt vitae molestie nec,molestie at mi. Nulla nulla lorem,suscipit in posuere in,interdum non magna.