• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Which read(byte [] ) method of FileInputStream, BufferedInputStream has higher performance

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am about to deploy my java application but before i did some performance speed testing using java visualvm profiler.

I wanted to test read(byte []) method of FileInputStream and compare it with read method of BufferedInputStream but because of both are too fast and using visualvm i can not catch it because it terminated so fast so i can not see the performance .


inputStream.read(fileContent)
here is why i am baffled. How come can i use read(byte []) like above (and it is working fine without error or exceptions) because BufferedInputStream does not have
any method like read(byte []) . So i want to know using the method like below am i using method of fileinputstream or the method of any other java api class?also when i put some other methods which take several seconds i finally observe this method read and it was about 200 ms forreading 100 files in a folder and there it was shown me that this method comes from FilterInputStream.

In other words using it like this: inputStream.read(fileContent) means that i am working with BufferedInputStream , FilterInputStream or FileInputStream?

Also doing it like i have done in method above is there any way to improve performance speed or this is the best way to read file and put it in an array of byte?
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reading everything in one go into one big array is going to be the fastest. In fact, that's actually what BufferedReader does if you tell it to use a buffer as large as the complete file. It's always going to be a trade-off between memory and speed. For most applications, I find that just using a BufferedReader is fast enough without setting the buffer size.

I question the purpose of reading an entire file into a byte array though. Byte arrays aren't that interesting. Normally you'll want to interpret binary data as soon as it comes out of an InputStream.
 
barlet south
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Stephan Van Hulst so basically the way i have implemented it above seems to be the fastest one , right?
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, in my last post I said BufferedReader. I meant BufferedInputStream.

No. Loading everything in one big byte array will be marginally faster without constructing another BufferedInputStream instance, because BufferedInputStream just calls FileInputStream's read(byte[]) method multiple times.

Even if that's the fastest approach, I argue it's going to be almost useless because the increase in performance is not going to be worth the increase in memory use, if you don't need the entire file as a byte array.

Why do you need it to be a byte array?

As a final remark, you should use try-with-resources when working with input streams. It's a lot less contrived than the try-finally-try-catch approach.
 
Bartender
Posts: 1845
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Re your comment:
>How come can i use read(byte []) like above (and it is working fine without error or exceptions) because BufferedInputStream does not have any method like read(byte [])

Ah but it does have that method. It inherits it from FilterInputStream.
So when you call the "read" method on your BufferedInputStream, it finds the implementation in FilterInputStream and executes that.
The FilterInputStream then delegates to whichever InputStream is being wrapped - in this case the FileInputStream.


I'm gonna call "Premature optimization" on this one.
You probably don't need to worry about which of them is faster. Either of them will be more than fast enough.
Personally I would always wrap a FileInputStream in a BufferedInputStream.
 
barlet south
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Stephan van Hulst
I am working on Galois Filed 256 and all my operation are done for each byte of the file. Since each byte is 8 bit meaning that the fastest way is to use GF256 . And i have to use one galoa field on my project since this project has to do with security protocols.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic