package net.openhft.chronicle.wire.marshallable;

import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.core.util.ObjectUtils;
import net.openhft.chronicle.wire.RawWire;
import net.openhft.chronicle.wire.Wire;
import org.junit.Test;

import java.nio.ByteBuffer;

import static org.junit.Assert.assertEquals;

public class ByteBufferMarshallingTest {
    @Test
    public void writeReadByteBuffer() {
        Bytes<ByteBuffer> bytes = Bytes.elasticByteBuffer();
        Wire wire = new RawWire(bytes);

        AClass o1 = new AClass(1, true, (byte) 2, '3', (short) 4, 5, 6, 7, 8, "nine");

        o1.writeMarshallable(wire);

        AClass o2 = ObjectUtils.newInstance(AClass.class);
        o2.readMarshallable(wire);

        assertEquals(o1, o2);
    }

    @Test
    public void writeReadViaByteBuffer() {
        Bytes<ByteBuffer> bytes = Bytes.elasticByteBuffer();
        Wire wire = new RawWire(bytes);

        AClass o1 = new AClass(1, true, (byte) 2, '3', (short) 4, 5, 6, 7, 8, "nine");

        o1.writeMarshallable(wire);

        ByteBuffer bb = bytes.underlyingObject();
        bb.position((int) bytes.readPosition());
        bb.limit((int) bytes.readLimit());

        Bytes<ByteBuffer> bytes2 = Bytes.elasticByteBuffer();
        bytes2.ensureCapacity(bb.remaining());

        ByteBuffer bb2 = bytes.underlyingObject();
        bb2.reset();

        bb2.put(bb);
        // read what we just wrote
        bytes2.readPosition(0);
        bytes2.readLimit(bb2.position());

        Wire wire2 = new RawWire(bytes2);

        AClass o2 = ObjectUtils.newInstance(AClass.class);
        o2.readMarshallable(wire2);
        assertEquals(o1, o2);
    }

    @Test
    public void writeReadBytesViaByteBuffer() {
        Bytes<ByteBuffer> bytes = Bytes.elasticByteBuffer();

        AClass o1 = new AClass(1, true, (byte) 2, '3', (short) 4, 5, 6, 7, 8, "nine");

        o1.writeMarshallable(bytes);

        ByteBuffer bb = bytes.underlyingObject();
        bb.position((int) bytes.readPosition());
        bb.limit((int) bytes.readLimit());

        Bytes<ByteBuffer> bytes2 = Bytes.elasticByteBuffer();
        bytes2.ensureCapacity(bb.remaining());

        ByteBuffer bb2 = bytes.underlyingObject();
        bb2.reset();

        bb2.put(bb);
        // read what we just wrote
        bytes2.readPosition(0);
        bytes2.readLimit(bb2.position());

        AClass o2 = ObjectUtils.newInstance(AClass.class);
        o2.readMarshallable(bytes2);
        assertEquals(o1, o2);
    }
}

@@ -1324,32 +1324,33 @@
 ffer bb2 = bytes
+2
 .underlyingObjec
@@ -1358,37 +1358,37 @@
 ();%0A        bb2.
-reset
+clear
 ();%0A%0A        bb2
@@ -2203,32 +2203,33 @@
 ffer bb2 = bytes
+2
 .underlyingObjec
@@ -2249,13 +2249,13 @@
 bb2.
-reset
+clear
 ();%0A

Add an example of exchanging data with ByteBuffer