Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

I need a "WriterOutputStream"

807569Jun 2 2006
This is quite frustrating.

Backgroud: I have a custom Writer that we use in our web app to filter all of our HTML pages. I use a Servlet Filter to create a custom Response. When a servlet/jsp requests a Writer from the Response, it gets my Writer which is stacked on top of the containers provided Writer.

Now, we've been running in Tomcat and we're porting to Weblogic.

Weblogic never uses the getWriter method of the Response for JSPs. Rather it provides it's own custom JSPWriter which requests the OutputStream from the Response. This handily bypasses all my hard work.

Now, Java IO provides adapters like InputStreamReader, which lets us take an InputStream and converts it into a Reader. Then we can tack on a BufferedReader or whatever.

This is fairly common:

BufferedReader br = new BufferedReader(new inputStreamReader(System.in));

And that's the Java Way, stacking specifilzed Readers/Writers/Streams etc.

But what we don't have is an WriterOutputStream. Something that will take a Writer and give back an OuputStream.

I need to make my response provide an OutputStream for it's getOutputStream method. But all I have is a Writer.

Now, obviously, a Writer is not always an appropriate thing to wedge in to a Stream (Writers deal with Unicode, Streams with raw bytes). But if, say, contentType = "text/html", then interjecting my Writer is not a problem.

But I can't do this:
public OutputStream getOutputStream() {
    OutputStream os = super.getOutputStream();
    if (contentType.equals("text/html")) {
        OutputStreamWriter osw = new OutputStreamWriter(os);
        MyWriter mw = new MyWriter(osw);
        // Can't do the following
        WriterOuputStream wos = new WriterOutputStream(mw);
        os = wos;
    }
    return os;
}
The only way (that I know) to decode a binary Unicode stream of bytes is to use a Reader. The JDK does not (I don't think, maybe I'm missing it) provide a stream unicode encoder/decoder that I can use to make my own WriterOutputStream.

This means for me to plug my filter in I need to use Piped Streams and Writers and spawn a conversion thread so one can read the pipe while the other thread write to it.

Blah! That seems a little overly complicated to me.

Anyone happen to have a handy "WriterOutputStream" I can use?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 30 2006
Added on Jun 2 2006
0 comments
185 views