Modify the output of a PrintStream before print

Hi,

This is quite complicated and I have little to no clue how it can be done. Basically I am using a 3rd party class (which I do not have the source of) which has a method that as a parameter takes the PrintStream it is to use to display the output on the screen. Now I want to actually know what is going to be outputted before it is displayed on the screen so that I can change some special characters. Following I did a very simple example of how this works:import java.io.PrintStream;

publicclass Test8{

public Test8(){

// 1.

v(System.out);

}

privatevoid v(PrintStream out){

// Uses PrintStream to display 'Hi <name>'

out.println("Hi {0}");

}

publicstaticvoid main(String[] args){

new Test8();

}

}

In the code example above you can see that the methods named v will use the PrintStream object I passed as a parameter to display Hi {0}. However I want to capture that output so that I can replace that {0} with a parameter; example name.

What I thought of is actually passing a PrintStream that rather then outputs on the screen it outputs in some Buffer or String, so that I can then get that String, replace the elements I want and then print it normally. The psuedo code would be something like this:import java.io.PrintStream;

publicclass Test8{

public Test8(){

// 1.

v(myStreamObject);

// make my modifications

System.out.print(myNewString);

}

privatevoid v(PrintStream out){

// Uses PrintStream to display 'Hi <name>'

out.println("Hi {0}");

}

publicstaticvoid main(String[] args){

new Test8();

}

}

However I checked the APIs and so far still did not find anything!

Does anyone here have any idea on what I can do to manage this? I know my solution could be wrong. But unfortunately I have no other idea how this can be done.

Thanks for any suggestions,

Regards,

Sim085

ps: for the second code example I thought of extending the PrintStream object (trying to do that now) but not sure if I can do that.

[3469 byte] By [sim085a] at [2007-11-27 11:53:59]
# 1

Can you extend PrintStream and overwrite the read methods to return or do what you want them to return, and pass that to the method? Sort of writing a filter stream...

P.S.: yeah, what you said. Didn't read your P.S.

The other way would be to read that stream yourself and pass the contents into a new ByteArrayInputStream...

CeciNEstPasUnProgrammeura at 2007-7-29 18:53:21 > top of Java-index,Java Essentials,Java Programming...
# 2

Thanks :) ...

Here is my fast code sampleimport java.io.OutputStream;

import java.io.PrintStream;

import java.util.ArrayList;

public class Test8 {

public Test8(){

// 1.

v(new MyPrintScreen(System.out, new ArrayList()));

}

private void v(PrintStream out){

// Uses PrintStream to display 'Hi <name>'

out.print("Hi {0}");

}

public static void main(String[] args) {

new Test8();

}

}

class MyPrintScreen extends PrintStream{

private ArrayList params;

public MyPrintScreen(OutputStream out, ArrayList params) {

super(out);

this.params = params;

}

public void print(String s){

super.print(this.handle(s));

}

private String handle(String s){

return "Changed";

}

}

I will now elaborate on that :) Thanks again.

Regards,

Sim085

sim085a at 2007-7-29 18:53:21 > top of Java-index,Java Essentials,Java Programming...
# 3

> extend PrintStream and overwrite the read methods

I need caffeine.

CeciNEstPasUnProgrammeura at 2007-7-29 18:53:21 > top of Java-index,Java Essentials,Java Programming...