Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Stream and writer

 
Joseph Sweet
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.

the names they gave to these classes do not help to notice these differences.
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Joseph Sweet
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
code.....

where is it......


 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you...

since javax.servlet.http.HttpServletResponse is an interface, how can it have member fields?
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interfaces, in java, can have variables. By default the variables are public, final and static.

Thanks,
Ram.
 
Joseph Sweet
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you said



but I see OutputStreamWriter can write Strings by



and



so what is the additional wrapping (PrintWriter) for?
 
Joseph Sweet
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
I will bookmark that link.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic