Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inputstream to bytebuffer

 
Jeremy Wilson
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Edward Kenworthy
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic