Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt in using input streams  RSS feed

palanivelrajan subramanian
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all,

If we want to get input from the user we are using
BufferedInputStream, ByteArrayInputStream, DataInputStream, FilterInputStream,PushbackInputStream

which is efficient among these.... what are the differences...
Matthew Brown
Posts: 4568
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Input streams generally fall into two categories@

- Streams that give you the ability to read from a particular place
- Streams that provide a particular interface/functionality

Often, you use them in pairs. One that can read from whatever you want to read from, and then a second one wrapping that that provides whatever interface you want.

For instance, ByteArrayInputStream is an example of the first type. You can use it to turn a straightforward byte array in memory into an input stream. FileInputStream is another example of this type, and lets you read from a file.

But these just have the basic InputStream interface, which allows you to read either single bytes or arrays of bytes. Often, you want something a bit more sophisticated than that, and that's when the second type of stream comes in. Which you choose depends on what you want to be able to do:

- BufferedInputStream gives you more efficiency by reading the stream in chunks
- DataInputStream gives you methods for reading primitive values and Strings
- ObjectInputStream allows you to read serialized objects
- PushbackInputStream allows you to "unread" bytes you've already read.

The Javadocs for each class tell you exactly what it can do.

Does that help?
Rob Spoor
Posts: 21048
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you can "stack" them too. For instance:
This code works from inside out:
1) data is read from the file
2) in a buffered way
3) it is then uncompressed
4) then decrypted
5) then made available for reading using DataInputStream

To write for this setup, simply replace Input by Output:
(also, the cipher needs to updated properly, but that's not relevant to this discussion)

It doesn't really matter in which order you stack these streams, as long as:
1) the source / destination goes in the center
2) the one you want to use (e.g. DataInputStream/DataOutputStream, ObjectInputStream/ObjectOutputStream, ZipInputStream/ZipOutputStream, but sometimes just one of the "intermediates") goes at the end
3) the order for reading and writing must be the same

Note that some of them don't modify data, and can be omitted. BufferedInputStream and BufferedOutputStream in my example are for performance reasons only. The others (GZIP, Cipher) modify the data so they cannot be omitted.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!