wood burning stoves 2.0*
The moose likes Java in General and the fly likes inputstream to bytebuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "inputstream to bytebuffer" Watch "inputstream to bytebuffer" New topic
Author

inputstream to bytebuffer

Jeremy Wilson
Ranch Hand

Joined: Feb 18, 2003
Posts: 166
I have an inputstream that I need to read the data into a byte array or a byter buffer. The problem is I don't know exactly how many bytes I need to read into array to wrap with the bytebuffer. Any help reading an inputstream to a byte array without any indication of how many bytes there are that would be create. The data is being read from a proprietary flat file format.


Jeremy Wilson
Edward Kenworthy
Ranch Hand

Joined: Oct 05, 2003
Posts: 66
Originally posted by Jeremy Wilson:
I have an inputstream that I need to read the data into a byte array or a byter buffer. The problem is I don't know exactly how many bytes I need to read into array to wrap with the bytebuffer. Any help reading an inputstream to a byte array without any indication of how many bytes there are that would be create. The data is being read from a proprietary flat file format.


Well if you're reading in a file then you can easily find out it's size and hence the size of the array you need: File has a method called length().

If you really don't know then do something like (this is pseudo code obviously):

while(!EOF)
{
read x bytes into temporary buffer;
increase the size of array by number of bytes actually read)
write bytes to array from buffer to array;
}

Choose x to be reasonably large (say 100) to keep the array re-sizing efficient but not too large otherwise the temporary buffer will have to be too big.
 
Consider Paul's rocket mass heater.
 
subject: inputstream to bytebuffer
 
Similar Threads
NX: cacheless design to keep things simple?
any help on reading data please
Reading variable size buffer into an byte array
Reading float, short from InputStream
socket problem, plz help