Showing posts with label Stream. Show all posts
Showing posts with label Stream. Show all posts

Monday, 9 May 2011

Standard Streams in java

Standard Streams are a feature provided by many operating systems. By default, they read input from the keyboard and write output to the display. They also support I/O operations on files.

Java also supports three Standard Streams:

  • Standard Input: Accessed through System.in which is used to read input from the keyboard.
  • Standard Output: Accessed through System.out which is used to write output to be display.
  • Standard Error: Accessed through System.err which is used to write error output to be display.

These objects are defined automatically and do not need to be opened explicitly. Standard Output and Standard Error, both are to write output; having error output separately so that the user may read error messages efficiently.
System.in is a byte stream that has no character stream features. To use Standard Input as a character stream, wrap System.in within the InputStreamReader as an argument.

InputStreamReader inp = new InputStreamReader(system.in);

Using DataInputStream and DataOutputStream


How to Concatenate Files using SequenceInputStream

The SequenceInputStream(in the API reference documentation) creates a single input stream from multiple input sources. This example program uses SequenceInputStream to implement a concatenation utility that sequentially concatenates files together in the order they are listed on the command line.

So lets first create the Utility class which perform this application, say we call it ListOfFiles which takes list of file names in its constructor, and then concatenates them by creating a new InputStream for every filename listed as argument.

Creating the Utility Class

import java.util.*;
import java.io.*;

public class ListOfFiles implements Enumeration {

private String[] listOfFiles;
private int current = 0;

public ListOfFiles(String[] listOfFiles) {
this.listOfFiles = listOfFiles;
}

public boolean hasMoreElements() {
if (current < listOfFiles.length)
return true;
else
return false;
}

public Object nextElement() {
InputStream in = null;

if (!hasMoreElements())
throw new NoSuchElementException("No more files.");
else {
String nextElement = listOfFiles[current];
current++;
try {
in = new FileInputStream(nextElement);
} catch (FileNotFoundException e) {
System.err.println("ListOfFiles: Can't open " + nextElement);
}
}
return in;
}
}


ListOfFiles implements the Enumeration(in the API reference documentation) interface. You'll see how this comes into play as we walk through the rest of the program.


Using this Utility class


import java.io.*;

public class Concatenate {
public static void main(String[] args) throws IOException {
ListOfFiles mylist = new ListOfFiles(args);

SequenceInputStream s = new SequenceInputStream(mylist);
int c;

while ((c = s.read()) != -1)
System.out.write(c);

s.close();
}
}


After the main method creates the SequenceInputStream, it reads from it one byte at a time. When the SequenceInputStream needs an InputStream from a new source (such as for the first byte read or when it runs off the end of the current input stream), it calls nextElement on the Enumeration object to get the next InputStream. ListOfFiles creates FileInputStream objects lazily, meaning that whenever SequenceInputStream calls nextElement, ListOfFiles opens a FileInputStream on the next filename in the list and returns the stream. When the ListOfFiles runs out of files to read (it has no more elements), nextElement returns null, and the call to SequenceInputStream's read method returns -1 to indicate the end of input.

Concatenate simply echos all the data read from the SequenceInputStream to the standard output.

Working with Filtered Streams

You attach a filtered stream to another stream to filter the data as it's read from or written to the original stream. The java.io package contains these filtered streams which are subclasses of either FilterInputStream(in the API reference documentation) or FilterOutputStream(in the API reference documentation):

  • DataInputStream and DataOutputStream
  • BufferedInputStream and BufferedOutputStream
  • LineNumberInputStream
  • PushbackInputStream
  • PrintStream (this is an output stream)
This section shows you how to use filtered streams through an example that uses a DataInputStream and a DataOutputStream. In addition, this section shows you how to write your own filtered streams.
Using Filtered Streams
To use a filtered input or output stream, attach the filtered stream to another input or output stream. For example, you can attach a DataInputStream to the standard input stream as in the following code:
DataInputStream dis = new DataInputStream(System.in);
String input;

while ((input = dis.readLine()) != null) {
. . . // do something interesting here
}
You might do this so that you can use the more convenient readXXX methods, such as readLine, implemented by DataInputStream.

How to use file stream?


How to use pipe stream


Using the Processing Streams

Processing streams perform some sort of operation, such as buffering or character encoding, as they read and write. Like the data sink streams, java.io often contains pairs of streams: one that performs a particular operation during reading and another that performs the same operation (or reverses it) during writing. This table gives java.io's processing streams.
 

Process CharacterStreams Byte Streams
Buffering BufferedReader,
BufferedWriter
BufferedInputStream,
BufferedOutputStream
Filtering FilterReader,
FilterWriter
FilterInputStream,
FilterOutputStream
Converting between
Bytes and Characters
InputStreamReader,
OutputStreamWriter
 
Concatenation   SequenceInputStream
Object Serialization   ObjectInputStream,
ObjectOutputStream
Data Conversion   DataInputStream,
DataOutputStream
Counting LineNumberReader LineNumberInputStream
Peeking Ahead PushbackReader PushbackInputStream
Printing PrintWriter PrintStream

Notice that many times, java.io contains character streams and byte streams that perform the same processing but for the different data type. The processing streams are briefly described here:

BufferedReader and BufferedWriter BufferedInputStream and BufferedOutputStream
Buffer data while reading or writing, thereby reducing the number of accesses required on the original data source. Buffered streams are typically more efficient than similar nonbuffered streams.
FilterReader and FilterWriter FilterInputStream and FilterOutputStream
Abstract classes, like their parents. They define the interface for filter streams, which filter data as it's being read or written. Working with Filter Streams later in this lesson shows you how to use filter streams and how to implement your own.
InputStreamReader and OutputStreamWriter
A reader and writer pair that forms the bridge between byte streams and character streams. An InputStreamReader reads bytes from an InputStream and converts them to characters using either the default character-encoding or a character-encoding specified by name. Similarly, an OutputStreamWriter converts characters to bytes using either the default character-encoding or a character-encoding specified by name and then writes those bytes to an OutputStream. You can learn the name of the default character-encoding by calling System.getProperty("file.encoding"). Find out more about character-encoding in the Internationalization trail.
SequenceInputStream
Concatenates multiple input streams into one input stream. How to Concatenate Files has a short example of this class.
ObjectInputStream and ObjectOutputStream
Used to serialize objects. See Object Serialization.
DataInputStream and DataOutputStream
Read or write primitive Java data types in a machine-independent format. How to Use DataInputStream and DataOutputStream shows you an example of using these two streams.
LineNumberReader LineNumberInputStream
Keeps track of line numbers while reading.
PushbackReader PushbackInputStream
Two input streams each with a 1-character (or byte) pushback buffer. Sometimes, when reading data from a stream, you will find it useful to peek at the next item in the stream in order to decide what to do next. However, if you do peek ahead, you'll need to put the item back so that it can be read again and processed normally.
PrintWriter PrintStream
Contain convenient printing methods. These are the easiest streams to write to, so you will often see other writable streams wrapped in one of these.

Using the Data Sink Streams

Data sink streams read from or write to specialized data sinks such as strings, files, or pipes. Typically, for each reader or input stream intended to read from a specific kind of input source, java.io contains a parallel writer or output stream that can create it. The following table gives java.io's data sink streams.

Sink Type Character Streams Byte Streams
Memory CharArrayReader,
CharArrayWriter
ByteArrayInputStream,
ByteArrayOutputStream
StringReader,
StringWriter
StringBufferInputStream
Pipe PipedReader,
PipedWriter
PipedInputStream,
PipedOutputStream
File FileReader,
FileWriter
FileInputStream,
FileOutputStream

 

Note that both the character stream group and the byte stream group contain parallel pairs of classes that operate on the same data sinks. These are described next:

CharArrayReader and CharArrayWriter
ByteArrayInputStream and ByteArrayOutputStream
Use these streams to read from and write to memory. You create these streams on an existing array and then use the read and write methods to read from or write to the array.
FileReader and FileWriter
FileInputStream and FileOutputStream
Collectively called file streams, these streams are used to read from or write to a file on the native file system. How to Use File Streams has an example that uses FileReader and FileWriter to copy the contents of one file into another.
PipedReader and PipedWriter
PipedInputStream and PipedOutputStream
Implement the input and output components of a pipe. Pipes are used to channel the output from one program (or thread) into the input of another. See PipedReader and PipedWriter in action in How to Use Pipe Streams.
StringReader and StringWriter
StringBufferInputStream
Use StringReader to read characters from a String as it lives in memory. Use StringWriter to write to a String. StringWriter collects the characters written to it in a StringBuffer, which can then be converted to a String. StringBufferInputStream is similar to StringReader, except that it reads bytes from a StringBuffer.

Where do streams come from?

System.out is an OutputStream; specifically it's a PrintStream. There's a corresponding System.in which is an InputStream used to read data from the console.

Data for streams can also come from files. Later you'll see how to use the File class and the FileInputStream and FileOutputStream classes to read and write data from files.

Network connections commonly provide streams. When you connect to a web or ftp or some other kind of server, you read the data it sends from an InputStream connected from that server and write data onto an OutputStream connected to that server.

Java programs themselves produce streams. ByteArrayInputStreams, ByteArrayOutputStreams, StringBufferInputStreams, PipedInputStreams, and PipedOutputStreams all use the stream metaphor to move data from one part of a Java program to another.

Perhaps a little surprisingly, components like TextArea do not produce streams. However you can always use the strings they do produce to create a ByteArrayInputStream or a StringBufferInputStream.

The Stream Classes

Most stream classes are part of the java.io package. (There are also a few more in the sun.io and sun.net packages, but those are deliberately hidden from you. There are also a couple in java.util.zip.)
 

Dividing the hierarchy in CharacterStream and ByteStream

As we saw  how type of stream is reflected in java.io hierarchy. So we have java.io defined in this way.  So we have Character Streams as well as ByteStreams.
From point of view of Character Stream, java.io. Reader and java.io. Writer are two main classes.
The two main classes from point of view of byte stream are java.io.InputStream and java.io.OutputStream.
 

CharacterStream

Reader and Writer are the abstract superclasses for character streams in java.io. Reader provides the API and partial implementation for readers--streams that read 16-bit characters--and Writer provides the API and partial implementation for writers--streams that write 16-bit characters.
 
Subclasses of Reader and Writer implement specialized streams and are divided into two categories: those that read from or write to data sinks (shown in gray in the following figures) and those that perform some sort of processing (shown in white). The figure shows the class hierarchies for the Reader and Writer classes.
reader
writer

Most programs should use readers and writers to read and write information. This is because they both can handle any character in the Unicode character set (while the byte streams are limited to ISO-Latin-1 8-bit bytes).

Byte Stream

Programs should use the byte streams, descendants of InputStream and OutputStream, to read and write 8-bit bytes. InputStream and OutputStream provide the API and some implementation for input streams (streams that read 8-bit bytes) and output streams (streams that write 8-bit bytes). These streams are typically used to read and write binary data such as images and sounds.

As with Reader and Writer, subclasses of InputStream and OutputStream provide specialized I/O that falls into two categories: data sink streams and processing streams.

inputstream

outputstream

Monday, 2 May 2011

NIO : Streams versus blocks


The most important distinction between the original I/O library (found in java.io.* ) and NIO has to do with how data is packaged and transmitted. As previously mentioned, original I/O deals with data in streams, whereas NIO deals with data in blocks.

A stream-oriented I/O system deals with data one byte at a time. An input stream produces one byte of data, and an output stream consumes one byte of data. It is very easy to create filters for streamed data. It is also relatively simply to chain several filters together so that each one does its part in what amounts to a single, sophisticated processing mechanism. On the flip side, stream-oriented I/O is often rather slow.


A block-oriented I/O system deals with data in blocks. Each operation produces or consumes a block of data in one step. Processing data by the block can be much faster than processing it by the (streamed) byte. But block-oriented I/O lacks some of the elegance and simplicity of stream-oriented I/O.

Sunday, 24 April 2011

What is stream in java IO?

Stream
A stream is a sequence of data of undetermined length. It's called a stream because it's like a stream of water that continues to flow. There's no definite end to it.

A better analogy might be a queue of people waiting to get on a ride at an amusement park. As people are processed at the front (i.e. get on the roller coaster) more are added at the back of the line. If it's a slow day the roller coaster may catch up with the end of the line and have to wait for people to board. Other days there may always be people in line until the park closes. Each person is a discrete individual and must be put on the roller coaster or not put on the roller coaster. There are no half-people. There's always a definite number of people in line though this number may change from moment to moment as people enter at the back of the line and exit from the front of the line. Although all the people are discrete, you'll sometimes have a family that must be put together in the same car. Thus although the individuals are discrete, they aren't necessarily unrelated.

In Java a stream is composed of discrete bytes. The bytes may represent chars or other kinds of data. They may come faster than you can handle them, or your thread may block while waiting for the next one to arrive. It often doesn't matter.
The key to processing the stream is a while loop that processes each piece of data, until you encounter the end-of-stream character or some other exceptional condition occurs. In Unix you type end-of-stream with Control-D. On Windows, you type end-of-stream with Control-Z.
 

The Stream Classes


 

Type of Steams in java (reflected in java.io class hierarchy )

The java.io package contains classes that perform input and output. In Java, I/O classes are differentiated according to the type of data being read or written. Byte oriented and numeric data is written with output streams and read with input streams. Character data, that is text, is written with writers and read with readers. Whether you use streams or readers and writers depends on the type of data you're dealing with. All text data, especially non-ASCII text, should be passed through a reader or writer.
The two main stream classes are java.io.InputStream and java.io.OutputStream. The two main reader and writer classes are java.io.Reader and java.io.Writer These are abstract base classes for many different subclasses with more specialized abilities.

Wednesday, 13 April 2011

Different File Read Write Stream Classes in Java

Byte streams, character streams, buffered streams, data streams and object streams are different data reading mechanisms for File IO. Each of the stream has a unique purpose to serve. In this article, we explore details of these streams. Also, we try to find when to use which stream.

fileioclasses

Byte Streams:

java.io.FileInputStream and java.io.FileOutputStream are the two classes for reading and writing purpose from/to a file. These classes extend from InputStream and OutputStream respectively. Data read by these classes is in low level format i.e. binary format. Hence if you have following code, then it will return value as int (which is the converted byte).

FileInputStream ins = new FileInputStream(“myfile.txt”);

int c = ins.read();


Similarly FileOutputStream writes data in binary format. Hence when you really want to deal with file contents in binary mode, such as bit wise operations, logical operations etc., then go for these classes. Otherwise it is better to choose from other file readers/writers.

Character Streams:

Here everything is handled in terms of characters. java.io.FileReader and java.io.FileWriter are the classes that handle character input from files and character output to the file. These classes are subclasses of java.io.Reader and java.io.Writer respectively.Any data read or written is converted into characters. Character encoding becomes important here otherwise you may end up in garbage conversion of some of the characters not available in default encoding, which is generally 8-bit superset of ASCII. As we are dealing with file in character mode, it is possible to handle contents line by line. Hence FileReader.readLine() method is available to read a line. Similarly FileWriter.write method is there to write a line to file.

All above classes read/write data directly from the underlying operation system. Hence each read/write call involves interaction with native APIs and subsequently with underlying operating system. This means network access, disc access, etc. This is not very performing implementation. Hence following set of classes are the solution to this problem.

Buffered Streams:

Buffered I/O is different because it is based on reading/writing data from memory (called as buffer) instead of directly from the underlying system. Now the interaction with disc is dependent on when you want more in buffer, or when you want to flush something out of buffer. By this mechanism we can control the number of calls being made to the underlying operating system.We can also wrap above classes by buffered readers to avail buffering facility. Code snippet like this will help us to get advantage of buffering with previous classes also.

bufferedInput = new BufferedReader(new FileReader(“myfile.txt”));


Output classes mostly support autoflush to empty buffer on disc periodically. Classes like PrintWriter also provide manual flushing ability so that we can optimize usage of these classes. You can find this feature effectively implemented in large response from client to server using http request-response model.

Data Streams:

These classes have got special function to do. Data stream classed provide special methods to read/write primitive data types. If we do not handle primitive data types in a special way and treat them as objects, then there is possibility of data loss during automatic conversions. To avoid this, these classes can be used. java.io.DataInputStream and java.io.DataOutputStream are two important classes. DataInputStream exposes methods like readInt(), readDouble(), etc. to read primitives, while DataOutputStream offeres similar methods writeInt(), writeDouble(), etc. to write primitives.

Object Streams:

java.io.ObjectInputStream and java.io.ObjectOutputStream are used to perform read and write of objects. Object read/write involves serialization/de-serialization of data, hence it involves special handling. (Most of the classes under object streams support serialization/de-serialization of data). There is complex logic involved in reading and writing of objects. Reason behind complexity are – objects have type hence regeneration should also result in type match with original object. Second is object have hierarchies. These hierarchies should also be satisfied in object streams transfer.

Summary:

java.io package provides many classes to interact with files on operating systems. Usually JVM interfaces with operating system, in other words, it shields program from the complexities of operating system interactions. But File handling is the area where the program has to directly deal with underlying operating system. java.io package offers many classes which serve general as well as specific purposes in file dealing. By selecting correct class, we can write better performing applications.