The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
April 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Correct usage of Java.IO classes with servlets

Posted by Chris Adams on May 07, 2001 at 6:07 PM

I am trying to create a servlet that serves as a broker within a 'web services' model.
For example - there exists a web service that upon receiveing the correct HTTP GET request, in XML format, it will return to me a JPG formatted image of my choice. My servlet actually receives a request first, as its the real client app, but if I don't have that image I go and get it from this external web service.

An example GET request that I'll generate to send out to the service might be:

String XMLRequest= ""

I send this string using a simple URLConnection Object, for example:

URL url = new URL("http://10.3.1.56/imageserver.asp");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
PrintWriter out1 = new PrintWriter(connection.getOutputStream());
out1.println(XML_Request);
out1.close();

Now I want to get my response off that connection:

BufferedInputStream in = new BufferedInputStream(connection.getInputStream());

The problem is that I do not always know what I will get back. If the image is unavailable or my request string is bad, I'll get an XML error message back...like: BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());

Finally, I guess (low confidence here), I want to transfer the data from the 'in' buffer array to the 'out' buffer array and then do a out.flush(). Is that smart? I also thought to use the read(byte array) method like:

while(in.read(out));

but I get an error stating 'in' is not a buffer array. What?


Question 1.) First theory. What combination of OutputStream classes should I use here so that if I get the image data back, I can forward it on to the response losing the least amount of time and processing/memory?

Question 2.) Java documentation for BufferedInputStream states that it creates a buffered array..."When the BufferedInputStream is created, an internal buffer array is created." WHere is it?

Question 3.) How should I work with transfering binary data from one buffer array to another? (Perhaps an example from question 1 makes this clear.) Does the flush write it out like I think it does?

Question 4.) How should I deal with the fact, I may get two types of data back(binary or XML text) and its unknown when?

I sincerely thank you for a response and will be happy to share results afterward.

Regards,
Chris Adams



Replies:
  • java dihan July 18, 2001 at 12:36 AM (0)

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us