You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by James Watkin <ja...@anderson.ucla.edu> on 2003/03/12 21:40:56 UTC

ServletOutputStream.write(byte[], int, int) Return Value

I have a question regarding the return value of the write method in the 
ServletOutputStream class in the example code given in a previous post to 
this list (see the message below). Isn't the correct return value void, not 
int?

It's my understanding that javax.servlet.ServletOutputStream extends 
java.io.OutputStream which, at least currently, defines the write method's 
return value to be void. What's interesting though is that the Java 
Tutorial claims it's int. See:
http://java.sun.com/docs/books/tutorial/essential/io/overview.html

Another interesting point is that even though the current JavaDoc for 
java.io.OutputStream defines the return value to be void, it includes the 
following line: "The general contract for write(b, off, len) is that some 
of the bytes in the array b are written...", the key word here being "some."

Would anyone care to comment on the history or reality of this API?

- Jim

The following message was taken from the Struts Mail Archive:

Response to: File upload/download design question 
(rhayden@rdg.boehringer-ingelheim.com)

Date: Wed, 30 May 2001 20:08:25 -0700 (PDT)
From: Craig R. McClanahan <cr...@apache.org>
Subject: File upload/download design question
Content-Type: TEXT/PLAIN; charset=US-ASCII

On Wed, 25 Apr 2001 rhayden@rdg.boehringer-ingelheim.com wrote:

 >
 > I have a design question regarding file upload/download which is not
 > completely Struts related but I was hoping someone could provide some
 > suggestions.  I have succeeded with the upload using the struts upload
 > package, and then I load the file into an Oracle BLOB field. Now I need to
 > get it back to the user if they want to view it.
 >
 > I was planning to create an Action for processing the file download. The
 > user will click on a URL and pass in the ID of the file as a request
 > parameter to the Action (i.e. fileDownload.do?id=1234). The Action will
 > access a session EJB which then locates the requested file and returns it to
 > the Action as a File object. Here is where I need advice: what is the best
 > way to send this File back to the browser?  Can I change the response type
 > to "file" somehow and stream this to the browser? Another option I thought
 > about was saving the file to a temp directory and then redirecting the
 > browser to this file. I would rather not write temp files if I don't have
 > to. Or, is there any functionality provided with struts for sending a file
 > back to the browser?
 >

This is one of the cases where you might actually want to have an Action
create the response itself, instead of forwarding to a JSP page to do
it.  The reason for this is that JSP pages aren't meant for binary output
(like a GIF image) -- but servlets can do this easily.

An example of an Action to do this might look like this:

   byte buffer[] = ... load byte array from the database ...
   int size = xxx; // Number of bytes to be sent

   response.setContentLength(size);
   response.setContentType("image/gif");  // Or whatever is correct
   ServletOutputStream stream = response.getOutputStream();
   int sent = 0;
   while (sent < size) {
     int count = stream.write(buffer, sent, size - sent);
     sent += count;
   }
   stream.flush();

   return (null);

Note that NULL is returned, instead of an ActionForward.  This is the
signal to the controller servlet that the Action has already created the
response, so no forwarding is required.


 > Any suggestions greatly appreciated...
 >
 > Thanks,
 > Bob
 >

Craig





--------------------------------------------------------------------------------

Copyright © 1999-2002, The Apache Software Foundation



______________________________
James Watkin
ACIS Software Development
The Anderson School at UCLA
james.watkin@anderson.ucla.edu
Voice: 1-310-825-5030
   Fax: 1-310-825-4835
______________________________ 


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


RE: ServletOutputStream.write(byte[], int, int) Return Value

Posted by Mark Galbreath <ma...@qat.com>.
The return type (not value) is void (which, technically, is no return at
all), and of course the API states "some," because argument "len" may be
less than the byte array.  I think this could have been better documented,
however.

Mark

-----Original Message-----
From: James Watkin [mailto:james.watkin@anderson.ucla.edu] 
Sent: Wednesday, March 12, 2003 3:41 PM

I have a question regarding the return value of the write method in the 
ServletOutputStream class in the example code given in a previous post to 
this list (see the message below). Isn't the correct return value void, not 
int?

It's my understanding that javax.servlet.ServletOutputStream extends 
java.io.OutputStream which, at least currently, defines the write method's 
return value to be void. What's interesting though is that the Java 
Tutorial claims it's int. See:
http://java.sun.com/docs/books/tutorial/essential/io/overview.html

Another interesting point is that even though the current JavaDoc for 
java.io.OutputStream defines the return value to be void, it includes the 
following line: "The general contract for write(b, off, len) is that some 
of the bytes in the array b are written...", the key word here being "some."

Would anyone care to comment on the history or reality of this API?

- Jim

The following message was taken from the Struts Mail Archive:

Response to: File upload/download design question 
(rhayden@rdg.boehringer-ingelheim.com)

Date: Wed, 30 May 2001 20:08:25 -0700 (PDT)
From: Craig R. McClanahan <cr...@apache.org>
Subject: File upload/download design question
Content-Type: TEXT/PLAIN; charset=US-ASCII

On Wed, 25 Apr 2001 rhayden@rdg.boehringer-ingelheim.com wrote:

 >
 > I have a design question regarding file upload/download which is not  >
completely Struts related but I was hoping someone could provide some  >
suggestions.  I have succeeded with the upload using the struts upload  >
package, and then I load the file into an Oracle BLOB field. Now I need to
> get it back to the user if they want to view it.  >  > I was planning to
create an Action for processing the file download. The  > user will click on
a URL and pass in the ID of the file as a request  > parameter to the Action
(i.e. fileDownload.do?id=1234). The Action will  > access a session EJB
which then locates the requested file and returns it to  > the Action as a
File object. Here is where I need advice: what is the best  > way to send
this File back to the browser?  Can I change the response type  > to "file"
somehow and stream this to the browser? Another option I thought  > about
was saving the file to a temp directory and then redirecting the  > browser
to this file. I would rather not write temp files if I don't have  > to. Or,
is there any functionality provided with struts for sending a file  > back
to the browser?  >

This is one of the cases where you might actually want to have an Action
create the response itself, instead of forwarding to a JSP page to do it.
The reason for this is that JSP pages aren't meant for binary output (like a
GIF image) -- but servlets can do this easily.

An example of an Action to do this might look like this:

   byte buffer[] = ... load byte array from the database ...
   int size = xxx; // Number of bytes to be sent

   response.setContentLength(size);
   response.setContentType("image/gif");  // Or whatever is correct
   ServletOutputStream stream = response.getOutputStream();
   int sent = 0;
   while (sent < size) {
     int count = stream.write(buffer, sent, size - sent);
     sent += count;
   }
   stream.flush();

   return (null);

Note that NULL is returned, instead of an ActionForward.  This is the signal
to the controller servlet that the Action has already created the response,
so no forwarding is required.


 > Any suggestions greatly appreciated...
 >
 > Thanks,
 > Bob
 >

Craig





----------------------------------------------------------------------------
----

Copyright C 1999-2002, The Apache Software Foundation



______________________________
James Watkin
ACIS Software Development
The Anderson School at UCLA
james.watkin@anderson.ucla.edu
Voice: 1-310-825-5030
   Fax: 1-310-825-4835
______________________________ 


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org