Link to home
Start Free TrialLog in
Avatar of b612_forever
b612_forever

asked on

Writer/OutputStream to String?

Hi, I am now writing a servlet filter that should capture all response and modify them before reply to browser.

What I need to do is extract all data from the OutputStream/Writer and store them into String before String manipulation can happen. Then write the modified data to user.

Can anyone tell me how can I extract all data from OutputStream/Writer to String? or any easier way to perform this filtering? (I am actually trying to filter the output of Crystal Report JRC)

Thanks in advance.
Avatar of bloodredsun
bloodredsun
Flag of Australia image

You need to do somehting similar to this http://www.onjava.com/pub/a/onjava/2003/11/19/filters.html  and over-ride the response write() method.
Technically that should be" override the write method of the ServletOutputStream used by the response".....
Avatar of b612_forever
b612_forever

ASKER

I am actually want to remove certain string pattern from the response that is generated by Crystal Report, if I override the write() method, does that means I will process the data bte by byte?
Convert the byte array to a String using the constructor

String byteString = new String(byte[] bytes) ;

do the replacement

byteString = byteString.replace("bert|ernie" , "") ;

and convert back

byte [] newBytes = byteString.getBytes() ;

and then write it out :-)
thanks a lot bloodredsun, i am actually very new to servlet filter, can u show me the code sample on ur previous post?

thanks
That is the code sample!?! What more do you need to know. Just override the ServletOutputStream in the way shown by the link in createing your own ResponseWrapper and do the conversion above.
Thanks a lots. I am trying now...

instead of GZIPOutputStream, are me had to use some others output stream?
ASKER CERTIFIED SOLUTION
Avatar of bloodredsun
bloodredsun
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks a lot, bloodredsun. It had work.

and to moderator, I suggest to rename this question to something like "modify content with servlet filter" to ease others users.

thank you.