• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Bear Bibeault
  • Liutauras Vilda
  • Devaka Cooray
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

inputstream to bytebuffer

 
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.
 
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.
 
Talk sense to a fool and he calls you foolish. -Euripides A foolish tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!