Skip to content

Instantly share code, notes, and snippets.

@danielshaya
Created October 9, 2015 09:31
Show Gist options
  • Save danielshaya/45760913cd7bf47bbd1c to your computer and use it in GitHub Desktop.
Save danielshaya/45760913cd7bf47bbd1c to your computer and use it in GitHub Desktop.
package chronicle.demo;
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.wire.BinaryWire;
import net.openhft.chronicle.wire.TextWire;
/**
* Created by daniel on 09/10/2015.
*/
public class WireDocumentDemo {
public static void main(String[] args) {
new WireDocumentDemo().test();
}
public void test(){
Bytes bytes = Bytes.elasticByteBuffer();
TextWire tw = new TextWire(bytes);
//serialise some data over wire wrapping it in a document
System.out.println("---------TextWire Demo--------------");
tw.writeDocument(false, w -> w.write(() -> "data").marshallable(
v -> v.write(() -> "name").text("dan")
.write(() -> "age").int8(44)));
System.out.println("Data serialised with TextWire:\n" + bytes.toString());
System.out.println("Data deserialised:");
//to deserialise that data
tw.readDocument(null, w -> w.read(() -> "data")
.marshallable(
m -> m.read(() -> "name").text(this, (t, s) -> System.out.println("Name:" + s))
.read(() -> "age").int8(this, (t, s) -> System.out.println("Age:" + s))));
//Do the same thing with BinaryWire
BinaryWire bw = new BinaryWire(bytes);
//serialise some data over wire wrapping it in a document
System.out.println("---------BinaryWire Demo--------------");
bw.writeDocument(false, w -> w.write(() -> "data").marshallable(
v -> v.write(() -> "name").text("dan")
.write(() -> "age").int8(44)));
System.out.println("Data serialised with BinaryWire:\n" + bytes.toHexString());
System.out.println("Data deserialised:");
//to deserialise that data
bw.readDocument(null, w -> w.read(() -> "data")
.marshallable(
m -> m.read(() -> "name").text(this, (t, s) -> System.out.println("Name:" + s))
.read(() -> "age").int8(this, (t, s) -> System.out.println("Age:" + s))));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment