To abstract from a specific serialization format I thought to define the following:
public interface TransportCodec {
void write(OutputStream out, Object obj) throws IOException;
Object read(InputStream in) throws IOException;
}
A default implementation would use just Java object serialization like this:
public void write(OutputStream out, Object obj) throws IOException {
ObjectOutputStream oout = new ObjectOutputStream(out);
oout.writeObject(obj);
oout.flush();
}
Obviously the oout.close() is missing, but for a reason: I want to be able write several objects into the same stream with independent calls to write. Looking at the source code of ObjectOutputStream (jdk 1.8), oout.close() closes the underlying stream, but also clears data structures that are part of ObjectOutputStream. But since I leave oout right to the garbage collector, I would not expect problems from not closing the stream.
Apart from the risk that a future JDK really needs the oout.close(), two questions:
- What do I loose in the current JDK when not closing the
ObjectOutputStreamabove. - First serializing into a
ByteArrayOutputStreamand then copying the bytes tooutwould allow to closeoout. Are there better options?
Separate into two interfaces and make the implementation class "own" the underlying stream.
Advantages:
Underlying storage is no longer restricted to be an
OutputStream/InputStream.By making the two interfaces extend
Closeable, they can now be used in a try-with-resources block.Caller only need to carry one reference (e.g.
TransportEncoder), and will no longer have to carry the stream too (e.g.OutputStream).Interfaces
ObjectStream implementations