read stream from web service

S

s o

Hi all,

if this is not the right place for this question, pls let me know.

I have a web service client that receives a image (up to 1 or 2 meg.)
as an attachment. In my first try I'm using datahandler to retrive the
image and it's working, but I was wondering which one of the following
is a better solution and hopefully someone can clarify my question
along the way. thanks.

here's what I have now. it creates a buffer having the same size of the
image. and read it in one shot.

InputStream is = datahandler.getInputStream();
int x = is.available();
buf = new byte[x];
is.read(buf);
//then create the image with buf.

But I'd image I should wrap a much smaller buffer in a
ByteArrayOutputStream to buffer the data as I would a local file. But
I'm not sure if this adds any value because the http response returns
the entire file in one shot anyway (I think) and it's different from
reading a file where multiple physical reads might occur. here's my
proposed way:

byte buf[] = new byte[1024]; //use a much smaller buffer instead
BufferedInputStream bis = new
BufferedInputStream(datahandler.getInputStream());
ByteArrayInputStream bas = new ByteArrayInputStream(buf);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while( ( int x=bas.read(buf) > 0 )
{
bos.write(buf);
}
//call bos.toByteArray();

any comments/suggetions are welcomed. I guess it boils down to if/how
different is reading a stream from a web service attachment from
reading a file. thanks.

s o
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top