A
Andreas Leitgeb
Suppose I have some OutputStream(OS), which I want to
pass to a method that's supposed to write some data
onto it, and leave the write position right after the
newly written data.
Surely this is no problem, as long as the method uses
the stream directly - But what, if the method wants
e.g. a BufferedOutputStream(BOS) (or perhaps some
Writer) for its output?
If it wraps the OS into a BOS, then I see no way to cleanly
unwrap it later without accidentally closing the original OS.
I hope I'm missing something obvious and easy.
Or, is my usecase really so bizarre?
Alternatives/hacks that do not satisfy me:
Have the method write to an ByteArrayOutputStream wrapped to
the method's liking, and then copy the byte[] to the original
stream.
Add the BOS to a statically kept Collection and leave it there
till end of program (to prevent it's finalizer from closing the
original stream) ... gross hack and object-leak.
Create one's own subclass of FilterOutputStream that does not
close() the wrapped stream. Or does such a class already
exist? I didn't find any in java.io nor among the direct
subclasses of Closeable, FilterOutputStream and some more.
PS: I'm aware that in most cases, streamwrappers just are not
being unwrapped until close()-time, so in the majority of cases
this close()-behaviour is just fine.
PPS: as I stumbled over it: Why has "SequenceInputStream" not
been enhanced with an Iterator<? extends InputStream> taking
constructor, additionally to the one taking an Enumeration?
pass to a method that's supposed to write some data
onto it, and leave the write position right after the
newly written data.
Surely this is no problem, as long as the method uses
the stream directly - But what, if the method wants
e.g. a BufferedOutputStream(BOS) (or perhaps some
Writer) for its output?
If it wraps the OS into a BOS, then I see no way to cleanly
unwrap it later without accidentally closing the original OS.
I hope I'm missing something obvious and easy.
Or, is my usecase really so bizarre?
Alternatives/hacks that do not satisfy me:
Have the method write to an ByteArrayOutputStream wrapped to
the method's liking, and then copy the byte[] to the original
stream.
Add the BOS to a statically kept Collection and leave it there
till end of program (to prevent it's finalizer from closing the
original stream) ... gross hack and object-leak.
Create one's own subclass of FilterOutputStream that does not
close() the wrapped stream. Or does such a class already
exist? I didn't find any in java.io nor among the direct
subclasses of Closeable, FilterOutputStream and some more.
PS: I'm aware that in most cases, streamwrappers just are not
being unwrapped until close()-time, so in the majority of cases
this close()-behaviour is just fine.
PPS: as I stumbled over it: Why has "SequenceInputStream" not
been enhanced with an Iterator<? extends InputStream> taking
constructor, additionally to the one taking an Enumeration?