my dog learned polymorphism*
The moose likes Servlets and the fly likes Stream and writer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Stream and writer" Watch "Stream and writer" New topic
Author

Stream and writer

Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
Hello guys,

Happy Passover.

what are exactly stream (especially OutputStream) and writer, and what are the differences between them?

For example, what a stream does that a writer cannot and vice versa?

I am trying to understand the following GZIPResponseWrapper:
(among many things, I do not understand why they need both a stream and a writer ).




That GZIPResponseWrapper is needed for the following filter:




Thanks!
[ April 28, 2005: Message edited by: Joseph Sweet ]

We must know, we will know. -- David Hilbert
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

The stream that you refer to is a ServletOutputStream in your program.
The writer is a PrintWriter.

The former is a byte stream and the latter a character stream. A character Stream is a high level stream that allows you to transparently write character data (Strings) to the output stream. If you use the byte streams, you would have to write byte by paniful byte to the stream. Character streams wrap over byte streams and do the translation of character to bytes for you.

Thus you would write Character arrays to the outermost character stream. The character stream wraps over the byte Stream , converts the character to bytes and writes it to the underlying byte streams.



Writing to stream, out or writer (in the above snippet) always writes to the underlying output stream. The difference is that you write bytes if writing to 'stream', chars if writing using 'out' and Strings if writing using 'writer'.

The high level wrapper streams provide additional functionality by wrapping over low level streams connected to the source.

Hope this helps.

Cheers,
ram.
Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
Thank you.

the names they gave to these classes do not help to notice these differences.
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489


the names they gave to these classes do not help to notice these differences.


Well, I wouldnt think so.
I think if you realize that there are 2 sets of Streams (a byte and its character contemporary) to do most read/write tasks, its not too difficult.

Character Streams always end with Reader or Writer and Byte streams with InputStream or OutputStream.

ram.
Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225
If you look at the code of getOutputStream() and getWriter(), you will also notice that in getOutputStream(), there is a check to see if getWriter() is already called and vice versa.

The essence here is that while using a servlet, you can use only one output stream/writer. Trying to get the other after getting the first one results in an Exception. and so, BOTH are never used, which the code of the two methods ensures.

By 'get an output stream/writer', i mean:

ServletOutputStream outStream = response.getOutputStream();

and

Writer outWriter = response.getWriter();
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Joseph Sweet:
Thank you.

the names they gave to these classes do not help to notice these differences.


The Javadocs might:
http://java.sun.com/j2se/1.5.0/docs/api/java/io/PrintWriter.html
http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletOutputStream.html
http://java.sun.com/j2se/1.5.0/docs/api/java/io/OutputStream.html
[ April 28, 2005: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
Thank you.

1. how do I get those links next time by myself. I often get to some sun's main index page and search there for a class or method and cannot find it. for example the first link does not include in its index the HttpServletResponse. why....


2. is the source code of HttpServletResponse/Request on my computer now? I do not know where it hides
[ April 28, 2005: Message edited by: Joseph Sweet ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Joseph Sweet:
Thank you.

1. how do I get those links next time by myself. I often get to some sun's main index page and search there for a class or method and cannot find it. for example the first link does not include in its index the HttpServletResponse. why....


Look in my signature.
Also, from any of the "drill down" pages in a javadoc site, you can click on the "frames" link to get back to the top level.
Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
code.....

where is it......


ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489


Originally posted by Joseph Sweet
code.....
where is it......


Code for what ? HttpServletRequest and Response ? Please read the javadocs carefully, both are interfaces. Sun just defined the specifications in such cases and its upto the Container to provide implementations that result in the expected behaviour as stated in the specs.
If you are using Tomcat, you could download the source code and search for classes that implement HttpServletRequest and HttpServletResponse.

Cheers,
Ram.
Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
thank you...

since javax.servlet.http.HttpServletResponse is an interface, how can it have member fields?
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

Interfaces, in java, can have variables. By default the variables are public, final and static.

Thanks,
Ram.
Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
you said



but I see OutputStreamWriter can write Strings by



and



so what is the additional wrapping (PrintWriter) for?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You can browse all of the Apache code online.
http://cvs.apache.org/viewcvs.cgi/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/connector/CoyoteOutputStream.java?view=markup
Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
Thank you.
I will bookmark that link.
 
 
subject: Stream and writer
 
Similar Threads
Filters and GZip
Fudging Filter Functionality as a method call
chain.doFilter() being called twice.
Response Wrappers and JSPs
JSP with large data loads slow in Solaris10 OS , we are using Jrun4 and nested logic:iterate