use of com.yahoo.io.GrowableByteBuffer in project vespa by vespa-engine.
the class TypedBinaryFormat method encode.
public static byte[] encode(Tensor tensor) {
GrowableByteBuffer buffer = new GrowableByteBuffer();
if (tensor instanceof MixedTensor) {
buffer.putInt1_4Bytes(MIXED_BINARY_FORMAT_TYPE);
new MixedBinaryFormat().encode(buffer, tensor);
} else if (tensor instanceof IndexedTensor) {
buffer.putInt1_4Bytes(DENSE_BINARY_FORMAT_TYPE);
new DenseBinaryFormat().encode(buffer, tensor);
} else {
buffer.putInt1_4Bytes(SPARSE_BINARY_FORMAT_TYPE);
new SparseBinaryFormat().encode(buffer, tensor);
}
buffer.flip();
byte[] result = new byte[buffer.remaining()];
buffer.get(result);
return result;
}
use of com.yahoo.io.GrowableByteBuffer in project vespa by vespa-engine.
the class SerializeTestCase method testOne.
@Test
public void testOne() {
SomeIdClass s = new SomeIdClass();
BufferSerializer buf = new BufferSerializer(new GrowableByteBuffer());
s.serializeWithId(buf);
buf.flip();
Identifiable s2 = Identifiable.create(buf);
assertThat((s2 instanceof SomeIdClass), is(true));
}
use of com.yahoo.io.GrowableByteBuffer in project vespa by vespa-engine.
the class SerializeTestCase method testBig.
@Test
public void testBig() {
BigIdClass dv = new BigIdClass();
BigIdClass ov = new BigIdClass(6667666);
BigIdClass bv = new BigIdClass(123456789);
assertThat(BigIdClass.classId, is(42));
assertThat(dv.getClassId(), is(42));
assertThat(ov.getClassId(), is(42));
assertThat(bv.getClassId(), is(42));
assertThat(ov.equals(dv), is(false));
assertThat(dv.equals(bv), is(false));
assertThat(bv.equals(ov), is(false));
BufferSerializer buf = new BufferSerializer(new GrowableByteBuffer());
ov.serialize(buf);
buf.flip();
dv.deserialize(buf);
assertThat(ov, equalTo(dv));
assertThat(dv, equalTo(ov));
buf = new BufferSerializer(new GrowableByteBuffer());
bv.serializeWithId(buf);
buf.flip();
dv.deserializeWithId(buf);
assertThat(bv, equalTo(dv));
assertThat(dv, equalTo(bv));
buf = new BufferSerializer(new GrowableByteBuffer());
SomeIdClass s = new SomeIdClass();
assertThat(dv.equals(s), is(false));
assertThat(ov.equals(s), is(false));
assertThat(bv.equals(s), is(false));
assertThat(dv.equals(new Object()), is(false));
s.serializeWithId(buf);
buf.flip();
boolean caught = false;
try {
dv.deserializeWithId(buf);
} catch (IllegalArgumentException ex) {
caught = true;
// System.out.println(ex);
}
assertThat(caught, is(true));
buf = new BufferSerializer(new GrowableByteBuffer());
buf.putLong(null, 0x7777777777777777L);
buf.flip();
caught = false;
try {
dv.deserializeWithId(buf);
} catch (IllegalArgumentException ex) {
caught = true;
// System.out.println(ex);
}
assertThat(caught, is(true));
}
use of com.yahoo.io.GrowableByteBuffer in project vespa by vespa-engine.
the class SerializeTestCase method testOrdering.
@Test
public void testOrdering() {
BufferSerializer buf = new BufferSerializer(new GrowableByteBuffer());
assertThat(buf.order(), is(ByteOrder.BIG_ENDIAN));
buf.putInt(null, 0x11223344);
buf.order(ByteOrder.LITTLE_ENDIAN);
buf.putInt(null, 0x55667788);
assertThat(buf.order(), is(ByteOrder.LITTLE_ENDIAN));
buf.flip();
assertThat(buf.getByte(null), is((byte) 0x11));
assertThat(buf.getByte(null), is((byte) 0x22));
assertThat(buf.getShort(null), is((short) 0x4433));
buf.order(ByteOrder.BIG_ENDIAN);
assertThat(buf.getByte(null), is((byte) 0x88));
assertThat(buf.getByte(null), is((byte) 0x77));
assertThat(buf.getShort(null), is((short) 0x6655));
}
use of com.yahoo.io.GrowableByteBuffer in project vespa by vespa-engine.
the class Document method getSerializedSize.
public int getSerializedSize() throws SerializationException {
DocumentSerializer data = DocumentSerializerFactory.create42(new GrowableByteBuffer(64 * 1024, 2.0f));
data.write(this);
return data.getBuf().position();
}
Aggregations