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
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