@Override protected <T> byte[] toByteArray(T message, Schema<T> schema) { return ProtobufIOUtil.toByteArray(message, schema, buf()); }
@Override protected <T> void mergeFrom(byte[] data, int offset, int length, T message, Schema<T> schema) throws IOException { ProtobufIOUtil.mergeFrom(data, offset, length, message, schema); }
/** * Creates a protobuf pipe from a byte array. */ public static Pipe newPipe(byte[] data) { return newPipe(data, 0, data.length); }
static <T> void protobufRoundTrip(T message, Schema<T> schema, Pipe.Schema<T> pipeSchema) throws Exception { byte[] protobuf = ProtobufIOUtil.toByteArray(message, schema, buf()); ByteArrayInputStream protobufStream = new ByteArrayInputStream(protobuf); byte[] smile = SmileIOUtil.toByteArray( ProtobufIOUtil.newPipe(protobuf, 0, protobuf.length), pipeSchema, false); byte[] smileFromStream = SmileIOUtil.toByteArray( ProtobufIOUtil.newPipe(protobufStream), pipeSchema, false); assertTrue(Arrays.equals(smile, smileFromStream)); T parsedMessage = schema.newMessage(); SmileIOUtil.mergeFrom(smile, parsedMessage, schema, false); SerializableObjects.assertEquals(message, parsedMessage); ByteArrayInputStream smileStream = new ByteArrayInputStream(smile); byte[] protobufRoundTrip = ProtobufIOUtil.toByteArray( SmileIOUtil.newPipe(smile, 0, smile.length, false), pipeSchema, buf()); byte[] protobufRoundTripFromStream = ProtobufIOUtil.toByteArray( SmileIOUtil.newPipe(smileStream, false), pipeSchema, buf()); assertTrue(Arrays.equals(protobufRoundTrip, protobufRoundTripFromStream)); assertTrue(Arrays.equals(protobufRoundTrip, protobuf)); }
static void verifyProtobuf(PojoWithInts p) throws IOException { Schema<PojoWithInts> schema = PojoWithInts.getSchema(); byte[] data = ProtobufIOUtil.toByteArray(p, schema, buf()); PojoWithInts pFromByteArray = new PojoWithInts(); ProtobufIOUtil.mergeFrom(data, pFromByteArray, schema); assertEquals(p, pFromByteArray); ByteArrayOutputStream out = new ByteArrayOutputStream(); ProtobufIOUtil.writeTo(out, p, schema, buf()); byte[] dataFromStream = out.toByteArray(); assertTrue(Arrays.equals(data, dataFromStream)); ByteArrayInputStream in = new ByteArrayInputStream(data); PojoWithInts pFromStream = new PojoWithInts(); ProtobufIOUtil.mergeFrom(in, pFromStream, schema); assertEquals(p, pFromStream); }
public void testProtobuf() throws Exception { Schema<Zoo> schema = RuntimeSchema.getSchema(Zoo.class); Zoo p = filledZoo(); byte[] data = ProtobufIOUtil.toByteArray(p, schema, buf()); // System.err.println("protobuf: " + data.length); Zoo p2 = new Zoo(); ProtobufIOUtil.mergeFrom(data, p2, schema); assertEquals(p, p2); List<Zoo> list = new ArrayList<Zoo>(); list.add(p); list.add(p2); ByteArrayOutputStream out = new ByteArrayOutputStream(); ProtobufIOUtil.writeListTo(out, list, schema, buf()); byte[] listData = out.toByteArray(); ByteArrayInputStream in = new ByteArrayInputStream(listData); List<Zoo> parsedList = ProtobufIOUtil.parseListFrom(in, schema); assertEquals(list, parsedList); }
public void testNestedOverflow() { final int bufSize = 256; final Bar bar = new Bar(); // reserve 3 bytes: // 1st: tag // 2nd and 3rd: delimited length (greater than 127 takes to more than one byte) int repeat = bufSize - 3; bar.setSomeString(repeatChar('a', repeat)); byte[] data = ProtobufIOUtil.toByteArray(bar, bar.cachedSchema(), buf(bufSize)); assertEquals(bufSize, data.length); // additional size will be: // 1 (tag) // 1 (delimiter) // 1 + 1 = tag + value (10) // 1 + 1 + 3 = tag + delim + value ("baz") // 1 + 1 = tag + value (15) // ===== // 11 bar.setSomeBaz(new Baz(10, "baz", 15l)); data = ProtobufIOUtil.toByteArray(bar, bar.cachedSchema(), buf(bufSize)); assertEquals(bufSize + 11, data.length); final Bar parsedBar = new Bar(); ProtobufIOUtil.mergeFrom(data, parsedBar, parsedBar.cachedSchema()); assertEquals(bar, parsedBar); }
public void testProtobuf() throws Exception { Schema<Entity> schema = RuntimeSchema.getSchema(Entity.class); Entity p = filledEntity(); byte[] data = ProtobufIOUtil.toByteArray(p, schema, LinkedBuffer.allocate(512)); Entity p2 = new Entity(); ProtostuffIOUtil.mergeFrom(data, p2, schema); assertEquals(p, p2); List<Entity> list = new ArrayList<Entity>(); list.add(p); list.add(p2); ByteArrayOutputStream out = new ByteArrayOutputStream(); ProtobufIOUtil.writeListTo(out, list, schema, buf()); byte[] listData = out.toByteArray(); ByteArrayInputStream in = new ByteArrayInputStream(listData); List<Entity> parsedList = ProtobufIOUtil.parseListFrom(in, schema); assertEquals(list, parsedList); }
public void testInheritanceProtobuf() throws IOException { if (skipTests) { System.err .println("RuntimeSchema.MORPH_NON_FINAL_POJOS was not enabled."); return; } System.err.println("executing inheritance test for protobuf ... "); Schema<InputSystem> schema = RuntimeSchema.getSchema(InputSystem.class); InputSystem sys = new InputSystem(); KeyBoard kb = new KeyBoard(); Mouse ms = new Mouse(); kb.setName("Test"); kb.setNumberOfKeys(10); ms.setName("Test1"); ms.setNumberOfButtons(2); List<InputDevice> devices = new ArrayList<InputDevice>(); devices.add(ms); devices.add(kb); sys.setInputDevices(devices); ByteArrayOutputStream out = new ByteArrayOutputStream(); ProtobufIOUtil.writeTo(out, sys, schema, buf()); byte[] listData = out.toByteArray(); InputSystem deserSystem = new InputSystem(); ByteArrayInputStream in = new ByteArrayInputStream(listData); ProtobufIOUtil.mergeFrom(in, deserSystem, schema, buf()); assertEquals(sys, deserSystem); }
@Benchmark public void generated_serialize_1_int_field() throws Exception { try { ProtobufIOUtil.writeTo(buffer, generatedInt1, generatedInt1Schema); } finally { buffer.clear(); } }
@Override protected <T> void mergeDelimitedFrom(InputStream in, T message, Schema<T> schema) throws IOException { ProtobufIOUtil.mergeDelimitedFrom(in, message, schema); }
static <T> void protobufRoundTrip(T message, Schema<T> schema, Pipe.Schema<T> pipeSchema) throws Exception { byte[] protobuf = ProtobufIOUtil.toByteArray(message, schema, buf()); ByteArrayInputStream protobufStream = new ByteArrayInputStream(protobuf); byte[] yaml = YamlIOUtil.toByteArray( ProtobufIOUtil.newPipe(protobuf, 0, protobuf.length), pipeSchema, buf()); byte[] yamlFromStream = YamlIOUtil.toByteArray( ProtobufIOUtil.newPipe(protobufStream), pipeSchema, buf()); assertTrue(yaml.length == yamlFromStream.length); String strYaml = STRING.deser(yaml); String strYamlFromStream = STRING.deser(yamlFromStream); assertEquals(strYaml, strYamlFromStream); }
public void testProtobuf() throws Exception { Schema<Payment> schema = RuntimeSchema.getSchema(Payment.class); Payment p = filledPayment(); byte[] data = ProtobufIOUtil.toByteArray(p, schema, buf()); Payment p2 = new Payment(); ProtobufIOUtil.mergeFrom(data, p2, schema); /* * System.err.println(p2.getId()); System.err.println(p2.getBd()); System.err.println(p2.getBi()); * System.err.println(p2.getBdList()); System.err.println(p2.getBiList()); */ assertEquals(p, p2); List<Payment> list = new ArrayList<Payment>(); list.add(p); list.add(p2); ByteArrayOutputStream out = new ByteArrayOutputStream(); ProtobufIOUtil.writeListTo(out, list, schema, buf()); byte[] listData = out.toByteArray(); ByteArrayInputStream in = new ByteArrayInputStream(listData); List<Payment> parsedList = ProtobufIOUtil.parseListFrom(in, schema); assertEquals(list, parsedList); }
public void testNestedLarge() { final int bufSize = 256; final Bar bar = new Bar(); // nested message size will be: // 1 + 1 = tag + value (10) // 1 + 1 + 125 = tag + delim + value ("baz") // 1 + 1 = tag + value (15) // ===== // 131 bar.setSomeBaz(new Baz(10, repeatChar('b', 125), 15l)); // size will be: // 1 (tag) // 2 (delimited length) (nested message size is greater than 127) // 131 (nested message size) // ===== // 134 byte[] data = ProtobufIOUtil.toByteArray(bar, bar.cachedSchema(), buf(bufSize)); assertEquals(134, data.length); final Bar parsedBar = new Bar(); ProtobufIOUtil.mergeFrom(data, parsedBar, parsedBar.cachedSchema()); assertEquals(bar, parsedBar); }
@Benchmark public void runtime_sparse_serialize_10_int_fields() throws Exception { try { ProtobufIOUtil.writeTo(buffer, sparseInt10, sparseInt10RuntimeSchema); } finally { buffer.clear(); } }
@Override protected <T> void mergeDelimitedFrom(InputStream in, T message, Schema<T> schema) throws IOException { ProtobufIOUtil.mergeDelimitedFrom(in, message, schema); }
@Override protected <T> void mergeFrom(byte[] data, int offset, int length, T message, Schema<T> schema) throws IOException { ProtobufIOUtil.mergeFrom(data, offset, length, message, schema); }
@Override protected <T> void roundTrip(T message, Schema<T> schema, Pipe.Schema<T> pipeSchema) throws Exception { byte[] protobuf = ProtobufIOUtil.toByteArray(message, schema, buf()); ByteArrayInputStream protobufStream = new ByteArrayInputStream(protobuf); byte[] protostuff = ProtostuffIOUtil.toByteArray( ProtobufIOUtil.newPipe(protobuf, 0, protobuf.length), pipeSchema, buf()); byte[] protostuffFromStream = ProtostuffIOUtil.toByteArray( ProtobufIOUtil.newPipe(protobufStream), pipeSchema, buf()); assertTrue(Arrays.equals(protostuff, protostuffFromStream)); T parsedMessage = schema.newMessage(); ProtostuffIOUtil.mergeFrom(protostuff, parsedMessage, schema); SerializableObjects.assertEquals(message, parsedMessage); ByteArrayInputStream protostuffStream = new ByteArrayInputStream( protostuff); byte[] protobufRoundTrip = ProtobufIOUtil.toByteArray( ProtostuffIOUtil.newPipe(protostuff, 0, protostuff.length), pipeSchema, buf()); byte[] protobufRoundTripFromStream = ProtobufIOUtil.toByteArray( ProtostuffIOUtil.newPipe(protostuffStream), pipeSchema, buf()); assertTrue(Arrays.equals(protobufRoundTrip, protobufRoundTripFromStream)); assertTrue(Arrays.equals(protobufRoundTrip, protobuf)); }
@Override protected <T> byte[] toByteArray(T message, Schema<T> schema) { return ProtobufIOUtil.toByteArray(message, schema, buf()); }
@Benchmark public void generated_serialize_10_int_field() throws Exception { try { ProtobufIOUtil.writeTo(buffer, generatedInt10, generatedInt10Schema); } finally { buffer.clear(); } }