Anda di halaman 1dari 3

Chaining of Streams | Java Tutorials, tips, forums, faq

17/02/2014

Java Tutorials, tips, forums, faq a blog for Java

OOPS

I/O STREAMS

COLLECTIONS

THREADS

INTERVIEW QUESTIONS

ABOUT AUTHOR

You are here: Home / I/O Streams / Chaining of Streams

Chaining of Streams
By S. Nageswara Rao, Corporate Trainer on April 16, 2011

Search
Type Your Search Word.....

Java comes with low-level byte streams and high-level byte streams and similarly low-level and high-level character streams. Sub classes of FilterInputStream and FilterOutputStream are known as high-level streams. There are four highlevel streams on input streams side LineNumberInputStream, DataInputStream, BufferedInputStream and PushbackInputStream. The job of high-level streams is to add extra functionality to the existing streams. For example, LineNumberInputStream adds line numbers in the destination file that do not exist in the source file. DataInputStream increases performance with its readInt() and readLine() methods etc. For higher functionality, one stream can be linked or chained to another, but obeying some simple rules. Chaining is very simple. The output of one stream becomes input to the other. Or to say, we pass an object of one stream as parameter to another stream constructor; this is how chaining is affected. Rules of chaining If streams are chained just like that, you will land in compilation errors. Following rules are to be followed. 1. The input for a high-level stream may come from a low-level stream or another high-level stream. That is, in programming, the constructor of a highlevel stream can be passed with an object of low-level or high-level. 2. Being low-level, the low-level stream should work by itself. It is not entitled to get passed with any other stream. In programming, the low-level stream opens the file and hand it over (passes) to a high-level stream. High-level stream cannot open a file directly. That is, high-level streams just add extra functionality and depend solely on low-level streams. For example, see the following.

If you like our content, please show your love by liking our page on facebook

Postings you would like !!!!


1. Static Blocks Static Initialization 2. Java Conversions 3. Aggregation and Composition

String str1 = "Accept\ngreetings\nfrom\nway2.java"; StringBufferInputStream sbi = new StringBufferInputStream(str1); // low-level LineNumberInputStream lis = new LineNumberInputStream(sbi); // high-level DataInputStream dis = new DataInputStream(lis); // high-level

Before going into explanation, let us find out what are low-level and high-level streams. In stream hierarchy, the subclasses of FilterInputStream and FilterOutputStream are known high-level streams and all the remaining are lowlevel.

http://way2java.com/io/chaining-of-streams/

1/3

Chaining of Streams | Java Tutorials, tips, forums, faq

17/02/2014

The intention of code is to give line numbers to the words of string str1 separated by \n. This string is passed to the constructor of low-level StringBufferInputStream because StringBufferInputStream can hold a string. The sbi object is passed to the constructor of high-level LineNumberInputStream to give line numbers. Again, the lis object is passed to another high-level stream constructor DataInputStream which can read each line separately. Here, the functionalities achieved with different streams chained are 1. Low-level FileInputStream opens the file. 2. High-level LineNumberInputStream adds line numbers. 3. High-level DataInputStream reads line by line. For low-level and high-level demarcation refer, input stream hierarchy, output stream hierarchy, reader hierarchy and writer hierarchy. Java IO at a Glance Some questions for you from java.lang package. 1. How many ways you can copy one object properties to another? Ans: 3 ways Shallow copying, Deep copying and Cloning. 2. How many types of inner classes exist? Ans: 4 types. 3. What are JAR files? Ans: JAR file is a zipped file zipped by JVM. 4. How to convert a string into string form? Ans: String to data type conversions byte, short, int, long, float, double, char and boolean. 5. How to convert an object to string? Ans: Object to String toString() 6. Do you know the functionality and methods of following java.lang package classes? Ans: 1. Root class class Object 2. class Character 3. class System 7. What are wrapper classes? Ans: Wrapper classes 8. What is the garbage collection mechanism in Java? Ans: Garbage Collection gc() and exit(0) 9. How to use the C++ destructor functionality where Java does not support destructors? Ans: Java Destructor finalize() 10. How to compare two objects? Ans: Object comparison hashCode() & equals()

If you have any Questions, please ask below


0 comments
Find us on Facebook

Way2Java
You like this. You and 3,007 others like Way2Java.

Posted in I/O Streams | Tagged java chain streams, java connect streams, java high level streams, java link
http://way2java.com/io/chaining-of-streams/ 2/3

Chaining of Streams | Java Tutorials, tips, forums, faq

17/02/2014

streams, java low level streams

One Response

PAGE 1 OF 1

Ramanuja
November 26, 2012 at 9:05 pm | Permalink | Reply

Hello Sir This site is very helpful for me. I will recommend my friends also. I found this site very recently and I feel very happy. Could you please provide Serialization topic also.

Leave a Reply
Name * Email *

Website

Comment

Notify me of follow-up comments by email. Notify me of new posts by email.

Post Comment

Copyright 2014 Java Tutorials, tips, forums, faq.

http://way2java.com/io/chaining-of-streams/

3/3

Anda mungkin juga menyukai