I’m still a little shaky on using ByteBuffer‘s. What I want to do is have data written to the ByteBuffer, then go to the beginning of the ByteBuffer and write a byte before all of that data (the payload of a packet is written, then prepended with the header.) How can I do that?
Diagram:
Buffer starts with:
| PAYLOAD |
Buffer after op code header is added (after what I want to do):
| HEADER | PAYLOAD |
The | just being separators of the kind of data, not literally anything.
What you are looking for is called ‘scatter-gather I/O’ and it is supported by
ScatteringByteChannel.read(ByteBuffer[])andGatheringByteChannel.write(ByteBuffer[]). Note the arrays. These interfaces are supported byFileChannel,SocketChannel, andDatagramSocketChanneland the Pipe channels.