Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

byte-array vs InputStream  RSS feed

 
manuel aldana
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

when working with downloading/uploading images there are two options to represent the pic: byte[] and InputStream (socket + file variants).

Normally I would favor the InputStream, but uploading and downloading are two completely separate tasks inside codebase. Because they are separate I want to have the download fully completed and the picture written to a byte[] array (see also http://www.pragprog.com/the-pragmatic-programmer/extracts/balance-resources).

Never the less I still got doubts whether this performs well, because pushing the whole stream to a a big byte array (images have are sized around 20K-100K) is not very nice. The environment is multithreaded and roughly 40 images are handled in parallel.

what do you think? is it better to save the stuff in a byte-array or is an just-in-time-read input-stream more appropriate?

thanks.
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds like worst-case scenario is only, er "only" 4MB. Speaking as someone whose first PC had 24K of RAM.

Actually, in a lot of present-day OS's, the OS itself is going to keep copies of these files in system buffer RAM if the demand is high enough, so that makes it a matter of whether you copy the image to RAM or the OS does.

If you really need the responsiveness, I'd use the byte arrays.
 
manuel aldana
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually the images are downloaded from URL not from filesystem.

Why is the byte-array more responsive? In my case it is read only once (for upload) and then discarded.
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess I'm not understanding. I thought you were serving the images. In which case, often-requested images would save time if you didn't have to go all the way back to long-term storage (disk) every time you wanted to transmit them.

Depending on the image format, it might be more sensible to process the inbound image data as a stream, but a lot of image manipulation works based on 2-dimensional operations. Since streams are 1-dimensional by definition, they tend to be sort of awkward in that context.
 
manuel aldana
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The task is: Download an image from a URL and upload it again (HTTP POST). For the upload there is a java client, which expects either a byte-array or an input-stream.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!