• 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

buffer size and reading a file into an outputstream

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I have the following piece of code:

FileInputStream fileInputStream = new FileInputStream(file);
OutputStream outputStream = socket.getOutputStream();
while((size = fileInputStream.read()) != -1) {
outputStream.write(data, 0, size);
outputStream.flush();
}
outputStream.close();

where 'data' is a byte[] and I have set it to 1024.....so my questions are:

1. what if the file size is huge....will this code segment work? by huge I mean really huge..

2. will the statment outputStream.write(data, 0, size);...work for huge files?

Thanks
 
Preetham Chandrasekhar
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Ok....lemme try answering my second question....please lemme know if i am wrong

the statement has nothing to do with huge/small files.....its just that huger the file and smaller the buffer (te byte array)...more the time its gonna take?
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code will not work at all. Where is the data put into the data variable?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I assume leaving the "data" variable out of the read() call was a typo. Otherwise I think you got it. It will read one buffer full, write it to the output, and repeat until done.

The idea is as long as you're making the OS tell the device to position the read head on the disk, grab a lot of bytes. Fewer and larger physical reads should be better. How much should you buffer? Probably as much as you can fit in memory. Maybe there is some optimum for the OS or the disk hardware but you don't even want to know about those.

Java's BufferedInputStream (and output) do the same thing for you. I've never looked into their buffer sizes beyond this note in BufferedReader: "The buffer size may be specified, or the default size may be used. The default is large enough for most purposes."
 
Preetham Chandrasekhar
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry guys...the 'data' was a typo....
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic