More about Stream

PipedInputStream
The Java.io.PipedInputStream class is a piped input stream that can be connected to a piped output stream, the piped input stream then provides whatever data bytes are written to the piped output stream.Following are the important points about PipedInputStream:
* The piped input stream contains a buffer, decoupling read operations from write operations, within limits.
* Attempting to use both objects from a single thread is not recommended, as it may deadlock the thread.
* A pipe is said to be broken if a thread that was providing data bytes to the connected piped output stream is no longer alive.
Sno Method & Description
1 int available()
This method returns the number of bytes that can be read from this input stream without blocking.
2 void close()
This method closes this piped input stream and releases any system resources associated with the stream.
3 void connect(PipedOutputStream src)
This method causes this piped input stream to be connected to the piped output stream src.
4 int read()
This method reads the next byte of data from this piped input stream.
5 int read(byte[] b, int off, int len)
This method reads up to len bytes of data from this piped input stream into an array of bytes.
6 protected void receive(int b)
This method receives a byte of data.
A pipe is a transferring channel though which data flows. Pipe is a conduit to send data of an output stream to an input stream. Generally, a thread connected to PipedOutputStream sends data to another thread connected to PipedInputStream.
To transfer data between two running processes, there comes 4 classes in java.io package, 2 from byte streams and two from character streams – PipedInputStream, PipedOutputStream, PipedReader and PipedWriter. The data written on an OutputStream object is read by an InputStream object. Here, the pipe is a transferring channel and comes with some predefined system dependent buffer memory. The memory cannot be specified by the programmer.
Example: -
import java.io.*;
class DataThread extends Thread
{
String str = "aNUJ bHARGAVA, 2-ga-27, mANU mARG, aLWAR";
OutputStream ostream1;
public DataThread(OutputStream ostream2)
{
ostream1 = ostream2;
}
public void run()
{ // purpose is to write to one end of pipe
try
{
System.out.println("Original Data: " + str);
DataOutputStream dostream = new DataOutputStream(ostream1);
dostream.writeBytes(str);
dostream.close();
}
catch (IOException e)
{
System.err.println("I/O problem occurred. " + e);
}
}
}
public class PipedInputOutputDemo
{
public static void main(String args[]) throws IOException
{
PipedOutputStream postream = new PipedOutputStream();
PipedInputStream pistream = new PipedInputStream(postream);
DataThread dt = new DataThread(postream);
dt.start();
DataInputStream distream = new DataInputStream(pistream);
String s1 = distream.readLine();
distream.close();
System.out.print("\nData toggled: ");
for(int i = 0; i < s1.length(); i++)
{
char ch = s1.charAt(i);
if(Character.isUpperCase(ch))
System.out.print(Character.toLowerCase(ch));
else if(Character.isLowerCase(ch))
System.out.print(Character.toUpperCase(ch));
else
System.out.print(s1.charAt(i));
}
}
}
Output: -
C:\java>javac PipedInputOutputDemo.java
C:\java>java PipedInputOutputDemo
Original Data: aNUJ bHARGAVA, 2-ga-27, mANU mARG, aLWAR
Data toggled: Anuj Bhargava, 2-GA-27, Manu Marg, Alwar
SequenceInputStream
The Java.io.SequenceInputStream class represents the logical concatenation of other input streams. It starts out with an ordered collection of input streams and reads from the first one until end of file is reached, whereupon it reads from the second one, and so on, until end of file is reached on the last of the contained input streams.
SequenceInputStream is used to copy a number of source files to one destination file. Many input streams are concatenated and converted into a single stream and copied at a stretch to the destination file.
Merging of multiple files can be done easily in Java using SequenceInputStream. SequenceInputStream is a good example to show that Java is a production language where with less code a greater extent of functionality is realized.
Two programs are given using the two overloaded constructors of SequenceInputStream that take two input stream objects as parameters (used to merge two files only) and the other an object of java.util.Enumeration interface (used to merge a number of files, more than 2).
Example: -
import java.io.*;
class TwoFiles { public static void main(String args[]) throws IOException
{ FileInputStream fistream1 = new FileInputStream("Sayings.txt"); // first source file
FileInputStream fistream2 = new FileInputStream("Morals.txt"); //second source file
SequenceInputStream sistream = new SequenceInputStream(fistream1, fistream2);
FileOutputStream fostream = new FileOutputStream("Result.txt"); // destination file
int temp;
while( ( temp = sistream.read() ) != -1)
{
System.out.print( (char) temp ); // to print at DOS prompt fostream.write(temp); // to write to file
}
fostream.close();
sistream.close();
fistream1.close();
fistream2.close();
}
}
Output: -
C:\java>javac TwoFiles.java
C:\java>java TwoFiles
First File: ANUJ BHARGAVA
Second File: 2-GA-27, Manu Marg, Alwar
PushbackInputStream
The Java.io.PushbackInputStream class adds functionality to another input stream, namely the ability to "push back" or "unread" one byte.
The PushbackInputStream is intendended to be used when you parse data from an InputStream. Sometimes you need to read ahead a few bytes to see what is coming, before you can determine how to interprete the current byte. The PushbackInputStream allows you to do that. Well, actually it allows you to push back the read bytes into the stream. These bytes will then be read again the next time you call read().
Example: -
package com.tutorialspoint;
import java.io.*;
public class PushbackInputStreamDemo {
public static void main(String[] args) {
// declare a buffer and initialize its size:
byte[] arrByte = new byte[1024];
// create an array for our message
byte[] byteArray = new byte[]{'H', 'e', 'l', 'l', 'o',};
// create object of PushbackInputStream class for specified stream
InputStream is = new ByteArrayInputStream(byteArray);
PushbackInputStream pis = new PushbackInputStream(is, 10);
try {
// read from the buffer one character at a time
for (int i = 0; i < byteArray.length; i++) {
// read a char into our array
arrByte[i] = (byte) pis.read();
// display the read byte
System.out.print((char) arrByte[i]);
}
// change line
System.out.println();
// create a new byte array to be unread
byte[] b = {'W', 'o', 'r', 'l', 'd'};
// unread the byte array
pis.unread(b);
// read again from the buffer one character at a time
for (int i = 0; i < byteArray.length; i++) {
// read a char into our array
arrByte[i] = (byte) pis.read();
// display the read byte
System.out.print((char) arrByte[i]);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
C:\java>javac PushbackInputStreamDemo.java
C:\java>java PushbackInputStreamDemo
Hello
World


Free Web Hosting