/** * A more efficient implementation of the write method, writing header data to the provided buffer and returning * a new buffer containing the payload. * * @param headerBuffer where the header data is written to (24 bytes) * @return a buffer containing the payload, ready to be read. */ public ByteBuffer writeHeaderAndGetPayloadBuffer(ByteBuffer headerBuffer) { return ByteBuffer.wrap(writeHeader(headerBuffer)); }
/** * For improved memory efficiency, you should use {@link #writeHeaderAndGetPayloadBuffer(ByteBuffer)} * and write the header buffer as well as the returned payload buffer into the channel. * * @param buffer where everything gets written to. Needs to be large enough for the whole message * to be written. */ @Override public void write(ByteBuffer buffer) { byte[] payloadBytes = writeHeader(buffer); buffer.put(payloadBytes); }