| Author |
Replacing text in a stream
|
Chris Boldon
Ranch Hand
Joined: Aug 10, 2006
Posts: 190
|
|
Is there a good way to replace text in a stream? I've created a servlet to read in the contents on an html page on another server and stream it out so it appears to be local. However, all relative paths are obviously broken. Below is my code so far, very simple. I have google as the URL for testing.
|
 |
Deva Sagar
Greenhorn
Joined: May 21, 2007
Posts: 7
|
|
You _could_ use a BufferedReader instead of a BufferedInputStream, read your input line by line and process each line to do the replace before you write it out. For example (assuming you have written a method called "replaceRelativeRefs" that, given a String, returns another String in which the relative references have been replaced with absolute ones) BufferedReader bufIn = new BufferedReader(new InputStreamReader(instream)); BufferedWriter bufOut = new BufferedWriter(response.getWriter()); String ln = bufIn.readLine(); while (ln != null) { bufOut.write(replaceRelativeRefs(ln)); ln = bufIn.readLine(); } However, replacing all relative URLs with aboslute ones is a non-trivial exercise because you have to account for many different types of references (e.g.) some pages use Javascript in their hyperlinks, and so on. Line-by-line processing of course means you won't catch anything that spans multiple lines. Best I could come up with....good luck!
|
 |
 |
|
|
subject: Replacing text in a stream
|
|
|