javapipes-filters

how to get pipes data as string through pipedreader


I am building a pipes and filters application in java .

I am writing an array-list of strings through pipedWriter. I am little confuse that how to get data as strings through pipedReader.

The way I'm doing is by getting characters but I want to get in the form of strings.

Here is code:

this is for writing. list is of strings.

for(String line : Filter1.list)
{
    myPw.write(line);
}

for getting data through pipedreader:

while((name=(char) myPr.read())!= -1)
{
    System.out.println(name);
} 

I am getting data in characters from this. Don't know how to get in strings


Solution

  • A pipe is just a stream of bytes, which in the case of the Reader/Writer flavor is automatically interpreted as a stream of chars. If you want to interpret it in terms of a more complex structure, then you have to implement some kind of data protocol in the stream.

    For example, if the strings you are sending are certain not to contain newline characters, then it would be pretty natural to delimit strings via newlines or perhaps even carriage return / newline pairs. If you need to signal the end of your list without closing the pipe, then you will need a marker for that as well.

    Alternatively, you could send first a number saying how many strings to accept, and then for each string send first the length, followed by the characters.

    There are many other variations on this theme.

    On the reading side, you interpret the stream contents according to the protocol you've established to assemble the characters you read into strings, and the strings into a list.