Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why does this truncate my files?

 
Ted Reynolds
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to expand some files from a zip file. when I do it by reading one byte and writing one byte at a time it works fine but is very slow, but when I try to read the whole file and then write it with the following code the output file is severly truncated, why? what am I missing?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The call to in.read(b) is not guaranteed to fill the buffer, even though the stream should eventually provide all the bytes you expect. An InputStream may break a read into multiple parts if for some reason it's not convenient to return all the bytes at once. So this means you always need to put read(byte[]) or read(byte[], int, int) into a loop of some sort, checking return values to see how many bytes have been read. Here's one way to do this:

Alternately, in cases where you don't know in advance what size the array will be:

[ April 25, 2004: Message edited by: Jim Yingst ]
 
Ted Reynolds
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, that made it work.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic