aspose file tools*
The moose likes I/O and Streams and the fly likes Why does this truncate my files? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Why does this truncate my files?" Watch "Why does this truncate my files?" New topic
Author

Why does this truncate my files?

Ted Reynolds
Greenhorn

Joined: Mar 16, 2004
Posts: 20
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

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
Ted Reynolds
Greenhorn

Joined: Mar 16, 2004
Posts: 20
thanks, that made it work.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why does this truncate my files?