Monday, February 13, 2012

High-level streams

High-level input and output streams communicate with low-level streams rather than with I/O devices. You can use them for high-level input and output.

Most of Java's high-level input and output classes inherit attributes from the FilterInputStream and FilterOutputStream superclasses. In turn, these classes inherit from the abstract InputStream and OutputStream classes.
Suppose you are using a DataInputStream constructor for one of these classes. You need to pass an InputStream to the constructor as a parameter.
DataInputStream(InputStream objectName)
You can use any class that inherits from the InputStream class as an input source for a high-level stream. For example, you can use a FileInputStream object that you have already created, or use input from a socket or pipe.
When a high-level stream object, such as an instance of the DataInputStream class, receives byte input from a low-level stream, it processes the bytes and converts them into the appropriate datatype.

The DataInputStream class contains read methods that convert bytes into all the primitive datatypes, as well as into UTF strings. For example, the readInt method reads the next four bytes and converts them into an int. For the methods to work correctly, these four bytes must represent an int. You need to make sure that the data is read in the same order in which it is written to a stream.
To close a DataInputStream object, you use the class's close method.

If you need to close a chain of stream objects, you do so in reverse order so that the object that was created first is the last one to close.

This prevents you from closing an InputStream before you close the high-level stream that uses it as an input source.
For example, the code sample that reads sales data from a file uses the close method to close an instance of the DataInputStream object.
    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 subclasses of the FilterOutputStream class include
  • DataOutputStream
  • BufferedOutputStream
  • PrintStream
DataOutputStream
You use the DataOutputStream to write data to a stream by passing an OutputStream to a DataOutputStream object as a parameter when you create the object.
BufferedOutputStream
You use the BufferedOutputStream to write data to a buffer. This in turn writes it to the underlying stream.
PrintStream
A PrintStream allows other output streams to conveniently print data of various formats. This class never throws an IOException, unlike other output streams.
The methods of the DataOutputStream class process data, such as characters, integers, and UTF strings, convert it to bytes, and write it to the stream.
File f = new File (myFileName);
if(f.exists() && f.isFile() && f.canWrite()) {
  try {
    FileOutputStream fostream = 
    new FileOutputStream(f);
    DataOutputStream dostream =
    new DataOutputStream(fostream);
    
    dostream.writeUTF("Some UTF data");
    dostream.close();
    
    fostream.close();
  }
  catch (IOException e) {
  }
}

Supplement

Selecting the link title opens the resource in a new browser window.
View the DataInputStream and DataOutputStream methods.
Consider the code that creates a FileOutputStream object named fostream, and a DataOutputStream object named dostream.
File f = new File (myFileName);
if(f.exists() && f.isFile() && f.canWrite()) {
  try {
    FileOutputStream fostream = 
    new FileOutputStream(f);
    DataOutputStream dostream =
    new DataOutputStream(fostream);
    
    dostream.writeUTF("Some UTF data");
    dostream.close();
    
    fostream.close();
  }
  catch (IOException e) {
  }
}
In doing so, the code writes the DataOutputStream to the FileOutputStream. - fostream.
It passes a string as a parameter to the writeUTF method, which writes it to the output stream.
Finally, it closes the two streams in the correct order. This way, the one that was created last is closed first.
File f = new File (myFileName);
if(f.exists() && f.isFile() && f.canWrite()) {
  try {
    FileOutputStream fostream = 
    new FileOutputStream(f);
    DataOutputStream dostream =
    new DataOutputStream(fostream);
    
    dostream.writeUTF("Some UTF data");
    dostream.close();
    
    fostream.close();
  }
  catch (IOException e) {
  }
}

0 comments:

Post a Comment