File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Replacing text in a stream Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Replacing text in a stream" Watch "Replacing text in a stream" New topic
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!
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Replacing text in a stream
 
Similar Threads
Read Unicode chars and insert into Oracle DB
reading byte array
Can't write to file
Blob Inputstream Reading
Struts iFrame security workaround - problems