Nov 25, 2011 class overview

This section introduces the basic organization of the classes, consisting of:
     * Input and output streams
     * Readers and Writers
     * Data and object I/O streams

Input stream classes
In the InputStream class, bytes can be read from three different sources:
  •  An array of bytes
  •  A file
  •  A pipe
Sources, such as ByteArrayInputStream and FilterInputStream , subclass the InputStream class. 

InputStream methods
Various methods are included in the InputStream class.
  • abstract int read() reads a single byte, an array, or a subarray of bytes. It returns the bytes read, the number of bytes read, or -1 if end-of-file has been reached.
  • read() , which takes the byte array, reads an array or a subarray of bytes and returns a -1 if the end-of-file has been reached.
  • skip() , which takes long , skips a specified number of bytes of input and returns the number of bytes actually skipped.
  • available() returns the number of bytes that can be read without blocking. Both the input and output can block threads until the byte is read or written.
  • close() closes the input stream to free up system resources.

InputStream marking
Some, but not all, InputStream’s support marking. Marking allows you to go back to a marked place in the stream like a bookmark for future reference. Remember, not all InputStream’s support marking. To test if the stream supports the mark() and reset() methods, use the Boolean markSupported() method.
The mark() method, which takes an integer read_limit , marks the current position in the input stream so that reset() can return to that position as long as no more than the specified number of bytes have been read between the mark() and reset() .

OutputStream classes
Bytes can be written to three different types of sinks:
  •  An array of bytes
  •  A file
  •  A pipe
Let's look at some examples of OutputStream classes. Before sending an OutputStream to its ultimate destination, you can filter it. For example, the BufferedOutputStream is a subclass of the FilterOutputStream that stores values to be written in a buffer and writes them out only when the buffer fills up.
CheckedOutputStream and DigestOutputStream are part of the FilterOutputStream class. They calculate checksums or message digests on the output.
DeflatorOutputStream writes to an OutputStream and creates a zip file. This class does compression on the fly.
The PrintStream class is also a subclass of FilterOutputStream , which implements a number of methods for displaying textual representation of Java primitive types. For example:
  •  println(long)
  •  println(int)
  •  println(float)
  •  println(char)
DataOutputStream implements the DataOutput interface. DataOutput defines the methods required for streams that can write Java primitive data types in a machine-independent binary format.

OutputStream methods
The OutputStream class provides several methods:
  •  The abstract void write() method takes an integer byte and writes a single byte.
  •  The void write() method takes a byte array and writes an array or subarray of bytes.
  •  The void flush() method forces any buffered output to be written.
  •  The void close() method closes the stream and frees up system resources.
It is important to close your output files because sometimes the buffers do not get completely flushed and, as a consequence, are not complete.

DataInput and DataOutput
The DataInput and DataOutput classes define the methods required for streams that can read Java primitive data types in a machine-independent binary format. They are implemented by RandomAccessFile.
The ObjectInput interface extends the DataInput interface and adds methods for deserializing objects and reading bytes and arrays of bytes.
The ObjectOutputStream class creates a stream of objects that can be deserialized by the ObjectInputStream class.
The ObjectOutput interface extends the DataOutput interface and adds methods for serializing objects and writing bytes and arrays of bytes.
ObjectOutputStream is used to serialize objects, arrays, and other values to a stream.

What are Readers?
Readers are character-based input streams that read Unicode characters.
  •  read() reads a single character and returns a character read as an integer in the range from 0 to        65535 or a -1 if the end of the stream is reached.
  •  abstract read() reads characters into a portion of an array (starting at offset up to length     number of characters) and returns the number of characters read or -1 if the end of the stream is reached.

Character input streams
Let's take a look at the different character input streams in the package.
  •  Strings
  •  Character arrays
  •  Pipes
InputStreamReader is a character input stream that uses a byte input stream as its data source and converts it into Unicode characters.
LineNumberReader , a subclass of BufferedReader , is a character input stream that keeps track of the number of lines of text that have been read from it.
PushbackReader , a subclass of FilterReader , is a character input stream that uses another input stream as its input source and adds the ability to push characters back onto the stream. 

What are Writers?
Writers are character-based output streams that write character bytes and turn Unicode into bytes. The base class includes these methods:
  •  The void write() method, which takes a character and writes single character in 16 low-order bits.
  •  The abstract void write() method, which takes a character array and writes a portion of an array of characters .

Character output streams
Let's take a look at the different character output streams in the package. There are several branches of this inheritance tree you can explore. Like Readers , any of the branches are available. Sinks for Writer output can be:
  •  Strings
  •  CharArray
  •  Pipes
OutputStreamWriter uses a byte output stream as the destination for its data.
BufferedWriter applies buffering to a character output stream, thus improving output efficiency by combining many small write requests into a single large request.
FilterWriter is an abstract class that acts as a superclass for character output streams. The streams filter the data written to them before writing it to some other character output stream.
PrintWriter is a character output stream that implements print() and println() methods that output textual representations of primitive values and objects.


Post a Comment