Could ByteBuffer implement DataOutput/DataInput?

Just wrap the buffer in ByteArrayInputStream or ByteArrayOutputStream using the put() or wrap() methods. The problem with having a ByteBuffer directly emulate a datainput/output stream has to do with not knowing the sizes in advance. What if there's an overrun?

Up vote 2 down vote favorite share g+ share fb share tw.

Is there some subtle reason why java.nio. ByteBuffer does not implement java.io" rel="nofollow">java.io. DataOutput or java.io" rel="nofollow">java.io.

DataInput, or did the authors just not choose to do this? It would seem straightforward to map the calls (e.g. PutInt() -> writeInt()). The basic problem I (and some others, apparently) have is older classes which know how to serialize/serialize themselves using the generic interfaces: DataInput/DataOutput.

I would like to reuse my custom serialization without writing a custom proxy for ByteBuffer. Java serialization nio bytebuffer link|improve this question edited Jan 27 at 2:35Jonas12.2k741103 asked Nov 1 '10 at 18:44Justin1,470624 96% accept rate.

Just wrap the buffer in ByteArrayInputStream or ByteArrayOutputStream using the put() or wrap() methods. The problem with having a ByteBuffer directly emulate a datainput/output stream has to do with not knowing the sizes in advance. What if there's an overrun?

What is needed is a ByteBufferOutputStream in which you can wrap / expose the required behaviors. Examples of this exist; the Apache avro serialization scheme has such a thing. It's not too hard to roll your own.

Why is there not one by default? Well, it's not a perfect world... ByteArrayOutputStream backing = new ByteArrayOutputStream(); DataOutput foo = new DataOutput(new DataOutputStream(backing); // do your serialization out to foo foo.close(); ByteBuffer buffer = ByteBuffer. Wrap(backing.toByteArray()); // now you've got a bytebuffer...

For mentioning Avero, it looks interesting. – Justin Nov 1 '10 at 19:33 Overrun on read should throw EOFException; overrun on write would be treated just like writing to a full volume (or just throw EOF as well) – Justin Nov 1 '10 at 19:36 @Justin: Looking at the DataOutput api, yeah, that might work fine. Maybe it's worth a little experiment to make sure you can proxy DataOutput down onto a ByteBuffer cleanly and posting a bug at the oracle site or contacting the nio expert group.

It may be that these two apis are intended to target different levels of abstraction; I'm not sure. – andersoj Nov 1 '10 at 19:40 @Justin: This also might be interesting to you. Mina.apache.org/iobuffer.html – andersoj Nov 1 '10 at 19:42.

A better way that works with direct buffers too: class ByteBufferOutputStream extends OutputStream { private final ByteBuffer buffer; public ByteBufferOutputStream(ByteBuffer buffer) { this. Buffer = buffer; } public void write(int b) throws IOException { buffer. Put((byte) b); } } Note that this requires calling buffer.flip() after you are done writing to it, before you can read from it.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions