There is no difference between BufferedInputStream wrapped in BufferedReader and BufferedReader reading InputStream directly?

Started by
3 comments, last by Glass_Knife 11 years, 4 months ago
This function returns an InputStream object.

[source lang="java"]Manager.open("C:/test.txt");[/source]

Exhibit A:

[source lang="java"]Manager assets = new Manager();
InputStream input = assets.open("C:/test.txt");
BufferedReader buffer = new BufferedReader(new InputStreamReader(input));[/source]

Exhibit B:

[source lang="java"] Manager assets = new Manager();
BufferedInputStream input = new BufferedInputStream(assets.open("C:/test.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(input));[/source]

Highly performance-wise, is there a significant difference between the two? Thanks in advance.
Advertisement
The Javadocs suggest wrapping the InputStreamReader in a BufferedReader to improve performance, which you are doing.

As far as the InputStream passed into the InputStreamReader, since the InputStream is only an interface, do you know what object
is implementing that interface? Maybe the object returned from the Manager.open() class is also a BufferedInputStream.

Either way, I would say that any savings here will be slight, and when you really do need a huge savings, use the jconsole to profile some tests and see which one is better. But until then, don't worry about it.

http://docs.oracle.com/javase/6/docs/technotes/guides/management/jconsole.html

Ciao, G.K.

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

Thanks for the input.

I don't know what object is implementing that InputStream interface (It should be an abstract class, right? That's how it is with my Eclipse editor code syntax coloring...). The only thing I know of when checking the Javadoc for that Manager class, is that it only returns a final InputStream. And that's it. It doesn't say it returns a BufferedInputStream or anything.
By doing that, you'd be buffering twice ... I would advise you to just buffer on the reader.

Thanks for the input.

I don't know what object is implementing that InputStream interface (It should be an abstract class, right? That's how it is with my Eclipse editor code syntax coloring...). The only thing I know of when checking the Javadoc for that Manager class, is that it only returns a final InputStream. And that's it. It doesn't say it returns a BufferedInputStream or anything.


It wasn't obvious to me if the Manager class was something you coded, or a library that you are using. Either way, I would say that one double buffer is enough, and leave it at that!

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

This topic is closed to new replies.

Advertisement