5

We can get a BufferedInputStream by decorating an FileInputStream. And Channel got from FileInputStream.getChannel can also read content into a Buffer.

So, What's the difference between BufferedInputStream and java.nio.Buffer? i.e., when should I use BufferedInputStream and when should I use java.nio.Buffer and java.nio.Channel?

expoter
  • 1,622
  • 17
  • 34
  • 3
    They are two entirely different things that happen to have the word "Buffer" in their names. A `BufferedInputStream` is an `InputStream` and adheres to that API. And a `Buffer` is an accessible buffer of data. Do you have a practical reason to ask this question? In that case it's much more fruitful to state the actual problem that you have than to ask a very unclear question. – Erwin Bolwidt Sep 10 '16 at 02:51
  • Could you explain why `They are two entirely different things`? I just want to get some resource to learn the principle of `BufferedInputStream` and `java.nio.Buffer`. – expoter Sep 10 '16 at 03:39
  • 2
    Javadoc: [BufferedInputStream](https://docs.oracle.com/javase/8/docs/api/java/io/BufferedInputStream.html) and [Buffer](https://docs.oracle.com/javase/8/docs/api/java/nio/Buffer.html). There is no reason why things are unrelated - there are only reasons for things being related. – Erwin Bolwidt Sep 10 '16 at 03:50
  • Of course I have read the java docs. But I still don't understand how they are different in implementation mechanism. Could you explain when should I use `BufferedInputStream` and when should I use `java.nio.Buffer` and `java.nio.Channel`? – expoter Sep 10 '16 at 05:25
  • The difference is that they are not the same, and the only question of any interest here is why you think otherwise, and why you think there is any question here to be answered. – user207421 Sep 10 '16 at 09:49

3 Answers3

2

Getting started with new I/O (NIO), an article excerpt:

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.

lupchiazoem
  • 8,026
  • 6
  • 36
  • 42
0

These classes where written at different times for different packages.

If you are working with classes in the java.io package use BufferedInputStream.

If you are using java.nio use the ByteBuffer.

If are not using either you could use a plain byte[]. ByteBuffer has some useful methods for working with primitives so you might use it for that.

It is unlikely there will be any confusion because in general you will only use one when you have to and in which case only one will compile when you do.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

I think we use BufferedInputStream to wrap the InputStream to make it works like block-oriented. But when deal with too much data, it actually consume more time than the real block-oriented I/O (Channel), but still faster than the unwrapperd InputStream.