• Post Reply Bookmark Topic Watch Topic
  • New Topic

Image File to ByteArrayInputStream

 
RamaSarma Vemuri
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have 3 image files, the requirement is I have to make these 3 files in to an array of ByteArrayInputStream. Can anyone help me out.

Regards,
Ram
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the constructor for ByteArrayInputStream in the JavaDoc. It wants a byte array argument.

So, you'll need a way to read a file into a byte array. One challenge with arrays is you have to declare the size up front. Fortunately you can get the size of a file from the File object.

Now you're looking at something like:

Lemme know if that helps.

Now, to take a right-angle turn ... why do you have to create a ByteArrayInputStream? If an instructor has told you to, well, I guess you're stuck. But if the other class that wants this thing can be changed, it really ought to just demand an InputStream. Then any InputStream would do the job ... FileInputStream, ByteArrayInputStream, and so on. The other class would be able to use many different data sources instead of just one kind. Did that makes sense?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!