Apr 12, 2012

Filtering


This section introduces filtering and covers the following topics:
Ø  PushbackInputStream
Ø  PushbackReader
Ø  LineNumberReader
Ø  PrintWriter


What is filtering?
         Use filtering to read or write a subset of data from a much larger input or output stream. The filtering can be independent of the data format (for example, you need to count the number of items in the list) or can be directly related to the data format (for example, you need to get all data in a certain row of a table). You attach a filter stream to an input or output stream to filter the data.
FilterInputStream and FilterOutputStream filter input and output bytes. When a FilterInputStream is created, an InputStream is specified for it to filter. Similarly, you specify an OutputStream to be filtered when you create a FilterOutputStream. They wrap around existing InputStream or OutputStream to provide buffering, checksumming, and so on.
For character I/O, we have the abstract classes FilterWriter and FilterReader. FilterReader acts as a superclass for character input streams that read data from some other character input stream, filter it, and then return the filtered data when its own read() methods are called. FilterWriter is for character output streams that filter data written to them before writing it to some other character output stream.


Pushback
         You use PushbackInputStream to implement a one-byte pushback buffer or a pushback buffer of a specified length. It can be used to parse data.When would you use this?
Sometimes when you read a byte from an InputStream , it signifies the start of a new block of data (for example, a new token). You might say, "I have read this byte, but I really cannot deal with it at this time. I want to push it back on the InputStream . When I come back later to read the InputStream , I want to get that same byte again."
You push the byte back onto the stream with the unread() method. When you read it again, you will read the data you unread before. You can unread() a single byte, an array of bytes, or an array of  bytes with an offset and a length.
If you are reading characters, you can push back a character instead of a byte. You use PushbackReader , which works just like the PushbackInputStream.Use the unread() methods to  unread a single character, an array of characters, or an array of characters with an offset and a length.


LineNumberReader versus LineNumberInputStream
           A LineNumberReader is a character input stream that keeps track of the number of lines of text that have been read from it. A line is considered terminated by a new line (\n), a carriage return ), or a carriage return followed by a linefeed. The readLine() method returns all the characters in the line without the terminator. The getLineNumber() method returns the number of lines that have been read.
If, for some reason, you want to reset the line number, you can do that with setLineNumber() and start the line count again from that point. LineNumberInputStream also keeps track of the number of lines of data that have been read, but this class was deprecated in Java 1.1 because it reads bytes rather than characters. LineNumberReader was introduced, which reads characters.  


PrintStream versus PrintWriter
     PrintStream is used to output Java data as text. It implements a number of methods for displaying textual representation of Java primitive data types.
You've probably used a PrintStream object since your earliest Java programming days. System.out is a PrintStream object. Probably everyone has used that for debugging! That being said, PrintStream has been superseded by PrintWriter in Java 1.1. The constructors of this class have been deprecated, but the class itself has not because it is still used by the System.out and System.err standard output streams.
The methods in PrintStream and PrintWriter are very similar. PrintWriter does not have methods for writing raw bytes, which PrintStream has.


Flushing
        Both the PrintStream and PrintWriter classes employ flushing, which moves data from the internal buffers to the output stream. Flushing is employed in two ways:
·       Automatic flushing, which says when you call println you get flushing
·        Without automatic flushing, which says flushing occurs only when the flush() method is called.
 With the PrintStream class, if a new-line character was written, the output is flushed. With the println() method is invoked. The println() methods of PrintWriter use the System property line.separator instead of the new-line ( \n) character that PrintStream uses.


PrintWriter process
        Let's look at the whole process.You can give the PrintWriter constructor a Writer or you can give it an OutputStream.With the latter, an OutputStreamWriter is transparently created, which performs the conversion for characters to bytes using the default character encoding.
You can choose one of the two flushing options: with autoflush or without. You can print integers, Strings, and characters without a new line with the print()methods and with a new line with the println()methods. 



0 comments :

Post a Comment