Monday, February 13, 2012

Low-level streams

When Java reads or writes data, it opens a data stream, reads or writes the information, and closes the stream.

Java uses the stream, reader, and writer classes for streamed data.
Stream classes deal with general data input and output, whereas the reader and writer classes deal specifically with Unicode and Unicode Transformation Format (UTF) string input and output.
Data received from or sent to general I/O devices consists of bytes. However, Java can support higher-level I/O by piecing together bytes to represent other types of data, such as integers, characters, or strings.
For example, a sequence of four bytes can make up an int.
Java uses a hierarchy of classes to deal with different types of data. The InputStream and OutputStream are abstract classes that use low-level I/O streams to read bytes from or send them to I/O devices such as files, network sockets, and pipes.
Low-level streams provide access directly to underlying bytes. High-level streams build upon low-level streams for additional capabilities.

The FilterInputStream and FilterOutputStream classes extend InputStream and OutputStream respectively. They use high-level filter streams to read or write data, such as strings and ints, from byte streams.
All low-level stream classes inherit from either the InputStream or OutputStream abstract classes.

Many stream input classes have a corresponding output class with similar methods. For example, the FileInputStream class, which is the input class for files, has a corresponding output class, FileOutputStream.
The low-level streams that are direct descendants of the InputStream or OutputStream include
  • ByteArrayInputStream and ByteArrayOutputStream
  • FileInputStream and FileOutputStream
  • ObjectInputStream and ObjectOutputStream
  • PipedInputStream and PipedOutputStream
  • SequenceInputStream
ByteArrayInputStream and ByteArrayOutputStream
The ByteArrayInputStream and ByteArrayOutputStream classes read and write arrays of bytes with buffering.
FileInputStream and FileOutputStream
The FileInputStream receives data from a file in byte form. The FileOutputStream outputs the data to a file.
ObjectInputStream and ObjectOutputStream
The ObjectInputStream deserializes primitive data and objects that have been previously serialized using an ObjectOutputStream object.
PipedInputStream and PipedOutputStream
The PipedInputStream and PipedOutputStream classes work with thread communication. They enable you to create and connect two sides of a stream.
The SequenceInputStream class enables you to concatenate other input streams and to read from each of them, in turn.
The corresponding InputStream and OutputStream subclasses have complementary structures and functions, including
  • constructors
  • read and write methods
  • reading and writing arrays
The FileInputStream and FileOutputStream classes have similar constructors.

The syntax for the constructors is

FileInputStream(String pathname)
FileInputStream(File file)
FileOutputStream(String pathname)
FileOutputStream(File file)
read and write methods
The FileInputStream class uses the read method to read the next byte from a file. The FileOutputStream class uses the write method to write a byte to a file.

The syntax for these methods is

int read () throws IOException
void write (int <b>) throws IOException
reading and writing arrays
The FileInputStream and FileOutputStream classes have complementary read and write methods for reading and writing arrays of bytes.

The syntax for these methods is

int read(byte[] b)
int read(byte[] b, int off, int len)

void write(byte[] b)
void write(byte[] b, int off, int len)
Suppose you have code that reads information on sales from a file and prints it to the standard output. To do this, the code creates a DataInputStream. This class has an InputStream data member, which is inherited from FilterInputStream.
    for (int i = 0; i < descs.length; i ++) {
      myData = new SalesData (descs[i], amounts[i], values[i]) ;
      writeSalesData ( myData ) ;
    out.close() ;

    // Prepare to read it in
    in = new DataInputStream(new FileInputStream(fruitfile)) ;

    for (int i=0; i<6; i++) {
      myData = readSalesData () ;
      System.out.println("You sold " +
      myData.desc + " at $" +
      myData.value + " each. Amount was " + myData.amount);
    in.close() ;
The DataInputStream object wraps a FileInputStream instance. This enables native datatypes to be read in a machine-independent fashion.
You can layer stream objects together to create streams capable of performing very specific I/O functions.

For example, you can extend FilteredInputStream to create custom filters that discriminately read formatted data. Your custom filter can, in turn, be chained to other streams.


Are you sure you can consider ObjectInputStream a low-level stream class? I doubt it.

"Great blog created by you. I read your blog, its best and useful information. You have done a great work. Super blogging and keep it up.php jobs in hyderabad.

nice blog
great information.
VLCC Institute Makeup Courses gives you the platform to become a Professional Makeup Artist by learning from the best in the Beauty Industry.
Our state-of-the-art Makeup Classes will walk you through everything that goes into establishing a successful career in this fun, fast-paced and fabulous industry.
We give a certificate of completion to our students upon completion.

Post a Comment