O
ohaya
Hi,
I have a program that currently writes to a file using PrintStream, and
I'll be needing to extend the functionality of the program to do some
post-output processing of the file, and if possible, I want to minimize
changes to the existing code.
Initially, I was thinking of just writing a separate program to read the
file that was output by the existing program, but then I started
wondering if there was some way to kind of redirect the existing output
to some kind of in-memory buffer, and then the new part could input from
the buffer.
What I'm thinking about is building a wrapper around PipeOutputStream to
add a 'println()' method. That way, with minimal code changes, the
original could would 'think' it was writing to a file, but it would
actually be writing to a PipeOutputStream.
Then, the new code could read the information created by the original
code by using a PipeInputStream.
I've never used PipeInput/OutputStream before, and so before I go about
this, I was hoping that I could get some comments on this approach? I
also have several concerns:
1) In several articles that I've read, it was also indicated that the
performance of PipeInput/Output was bad, and
2) All the documents I've read seem to indicate that PipeInputStream and
PipeOutputStream are intended to be used "between threads". In my case,
that wouldn't be the case, since the main thread would complete all
writing to the PipeOutputStream, and then start reading from the
PipeInputStream. Is this going to be a problem?
3) It seems like the PipeInputStream blocks on reads if there's no
input. If this is the case, how do I signal an "EOF-like" condition so
that the program doesn't hang when it gets to the end of the
information? Is the only way to do this for the writing end to put some
kind of indicator in the pipe that signals "EOF"?
4) Is there any memory limitation that will prevent this from working
(the 'output file' can be quite large)?
If this isn't a good approach, are there any other mechanisms/approaches
that might be better?
Thanks,
Jim
I have a program that currently writes to a file using PrintStream, and
I'll be needing to extend the functionality of the program to do some
post-output processing of the file, and if possible, I want to minimize
changes to the existing code.
Initially, I was thinking of just writing a separate program to read the
file that was output by the existing program, but then I started
wondering if there was some way to kind of redirect the existing output
to some kind of in-memory buffer, and then the new part could input from
the buffer.
What I'm thinking about is building a wrapper around PipeOutputStream to
add a 'println()' method. That way, with minimal code changes, the
original could would 'think' it was writing to a file, but it would
actually be writing to a PipeOutputStream.
Then, the new code could read the information created by the original
code by using a PipeInputStream.
I've never used PipeInput/OutputStream before, and so before I go about
this, I was hoping that I could get some comments on this approach? I
also have several concerns:
1) In several articles that I've read, it was also indicated that the
performance of PipeInput/Output was bad, and
2) All the documents I've read seem to indicate that PipeInputStream and
PipeOutputStream are intended to be used "between threads". In my case,
that wouldn't be the case, since the main thread would complete all
writing to the PipeOutputStream, and then start reading from the
PipeInputStream. Is this going to be a problem?
3) It seems like the PipeInputStream blocks on reads if there's no
input. If this is the case, how do I signal an "EOF-like" condition so
that the program doesn't hang when it gets to the end of the
information? Is the only way to do this for the writing end to put some
kind of indicator in the pipe that signals "EOF"?
4) Is there any memory limitation that will prevent this from working
(the 'output file' can be quite large)?
If this isn't a good approach, are there any other mechanisms/approaches
that might be better?
Thanks,
Jim