tags:

views:

60

answers:

2

I'm calling a method from an external library with a (simplified) signature like this:

public class Alien
{
    // ...
    public void munge(Reader in, Writer out) { ... }
}

The method basically reads a String from one stream and writes its results to the other. I have several strings which I need processed by this method, but none of them exist in the file system. The strings can get quite long (ca 300KB each). Ideally, I would like to call munge() as a filter:

public void myMethod (ArrayList<String> strings)
{
    for (String s : strings) {
        String result = alienObj.mungeString(s);
        // do something with result
    }
}

Unfortunately, the Alien class doesn't provide a mungeString() method, and wasn't designed to be inherited from. Is there a way I can avoid creating two temporary files every time I need to process a list of strings? Like, pipe my input to the Reader stream and read it back from the Writer stream, without actually touching the file system?

I'm new to Java, please forgive me if the answer is obvious to professionals.

+3  A: 

You can easily avoid temporary files by using any/all of these:

A sample mungeString() method could look like this:

public String mungeString(String input) {
  StringWriter writer = new StringWriter();
  alienObj.munge(new StringReader(input), writer));
  return writer.toString();
}
Joachim Sauer
+3  A: 

StringReader

StringWriter

Michael Borgwardt