• Post Reply Bookmark Topic Watch Topic
  • New Topic

buffer size and reading a file into an outputstream  RSS feed

 
Preetham Chandrasekhar
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Joe Ess
Bartender
Posts: 9426
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code will not work at all. Where is the data put into the data variable?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
sorry guys...the 'data' was a typo....
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!