Question on OutputStream re-use.

37 views
Skip to first unread message

Andrew Oswald

unread,
Dec 21, 2016, 4:57:58 PM12/21/16
to Chronicle
Hey folks, hope all is well!

I'm utilizing a third party binary encoder that takes an OutputStream.  I retrieve the OutputStream from a Marshallable's writeMarshallable method similar to this:
public void writeMarshallable(WireOut wire) {
    OutputStream outputStream = wire.bytes().outputStream();
   
// third party code gets the outputStream, etc.
}

wire.bytes().outputStream()'s implementation creates a new StreamingOutputStream on each invocation, which is what I'm hoping to avoid.

That said, I'm thinking about storing a WeakReference to the given wire.bytes() value and checking to see if its reference (ie, ==) is identical to a previously provided value:
private WeakReference<Bytes<?>> bytesRef = new WeakReference<>(null);

public
void writeMarshallable(WireOut wire) {
    Bytes<?> bytes = wire.bytes();
    if (bytes != bytesRef.get()) {
        LOGGER.info("provided bytes reference was not the lastBytes");
        bytesRef = new WeakReference<Bytes<?>>(bytes);
        thirdPartyEncoder = EncoderFactoryExample.from(bytes.outputStream());
    }
    // utilize encoder, etc.
}

So my question is whether this is a reasonable approach or whether you Chronicle folks have a more advisable way?

thanks!!
-andy
Reply all
Reply to author
Forward
0 new messages