@Override protected LinkedBuffer initialValue() { return LinkedBuffer.allocate(DEFAULT_BUF_SIZE); } };
@Override protected LinkedBuffer initialValue() { return LinkedBuffer.allocate(DEFAULT_BUF_SIZE); } };
public static LinkedBuffer buf() { return LinkedBuffer.allocate(BUF_SIZE); }
public static LinkedBuffer buf() { return LinkedBuffer.allocate(BUF_SIZE); }
public static LinkedBuffer buf(int size) { return LinkedBuffer.allocate(size); }
@Setup public void prepare() throws IOException { sharedBuffer = LinkedBuffer.allocate(512); sharedSession = new WriteSession(sharedBuffer); StringBuilder sb = new StringBuilder(); for (int i = 0; i < stringLength; i++) { sb.append('.'); } s = sb.toString(); }
private static <T> byte[] serializeGraph(T object) { @SuppressWarnings("unchecked") Class<T> clazz = (Class<T>) object.getClass(); Schema<T> schema = RuntimeSchema.getSchema(clazz); return GraphIOUtil.toByteArray(object, schema, LinkedBuffer.allocate()); }
private void writeObject(ObjectOutputStream out) throws IOException { byte[] data = ProtostuffIOUtil.toByteArray(this, RuntimeSchema.getSchema(PojoWithArrayAndSet.class), LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE)); out.writeInt(data.length); out.write(data); out.close(); }
@Test public void testFooArray() throws IOException { FooMessage foo = new FooMessage(); foo.field = Arrays.asList("a", "b"); byte[] xdata = MsgpackXIOUtil.toByteArray(foo, FOO_SCHEMA, false, LinkedBuffer.allocate()); byte[] data = MsgpackIOUtil.toByteArray(foo, FOO_SCHEMA, false); Assert.assertTrue(Arrays.equals(data, xdata)); }
@Test public void testString() throws IOException { String string = "ч"; byte[] data = string.getBytes(MessagePack.UTF8); // System.out.println(Arrays.toString(data)); LinkedBuffer lb = LinkedBuffer.allocate(); WriteSession session = new WriteSession(lb); StringSerializer.writeUTF8((CharSequence) string, session, session.tail); byte[] xdata = session.toByteArray(); // System.out.println(Arrays.toString(xdata)); Assert.assertTrue(Arrays.equals(data, xdata)); }
@Test public void testXIOCharset() throws IOException { ExampleMessage message = new ExampleMessage(); message.field2 = "ч"; byte[] xdata = MsgpackXIOUtil.toByteArray(message, SCHEMA, false, LinkedBuffer.allocate()); byte[] data = MsgpackIOUtil.toByteArray(message, SCHEMA, false); Assert.assertTrue(Arrays.equals(data, xdata)); }
@Test public void forceUseSunReflectionFactory() throws Exception { System.setProperty("protostuff.runtime.always_use_sun_reflection_factory", "true"); Schema<MyClass> schema = RuntimeSchema.getSchema(MyClass.class); ByteArrayOutputStream output = new ByteArrayOutputStream(); MyClass myClass = new MyClass(); // constructor initializes list with one element ProtostuffIOUtil.writeTo(output, myClass, schema, LinkedBuffer.allocate()); byte[] bytes = output.toByteArray(); Assert.assertEquals(1, myClass.getList().size()); MyClass myClassNew = schema.newMessage(); // default constructor should not be used ProtostuffIOUtil.mergeFrom(bytes, myClassNew, schema); Assert.assertEquals(1, myClassNew.getList().size()); }
@Test public void testSerializeDeserializeNumericEnum() throws Exception { RuntimeSchema<A> schema = RuntimeSchema.createFrom(A.class); A source = new A(TaggedEnum.TEN); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); LinkedBuffer buffer = LinkedBuffer.allocate(); ProtostuffIOUtil.writeTo(outputStream, source, schema, buffer); byte[] bytes = outputStream.toByteArray(); A newMessage = schema.newMessage(); ProtostuffIOUtil.mergeFrom(bytes, newMessage, schema); Assert.assertEquals(source, newMessage); }
public void testGraph() { System.err.println(RuntimeEnv.COLLECTION_SCHEMA_ON_REPEATED_FIELDS); Bean bean = fill(new Bean()); verify(bean); Schema<Bean> schema = RuntimeSchema.getSchema(Bean.class); // print(schema); byte[] bytes = GraphIOUtil.toByteArray(bean, schema, LinkedBuffer.allocate(256)); Bean deBean = new Bean(); GraphIOUtil.mergeFrom(bytes, deBean, schema); verify(deBean); }
public void testBean() { System.err.println(RuntimeEnv.COLLECTION_SCHEMA_ON_REPEATED_FIELDS); Bean bean = fill(new Bean()); verify(bean); Schema<Bean> schema = RuntimeSchema.getSchema(Bean.class); // print(schema); byte[] bytes = ProtostuffIOUtil.toByteArray(bean, schema, LinkedBuffer.allocate(256)); Bean deBean = new Bean(); ProtostuffIOUtil.mergeFrom(bytes, deBean, schema); verify(deBean); }
public void testBeanCyclic() { System.err.println(RuntimeEnv.COLLECTION_SCHEMA_ON_REPEATED_FIELDS); Bean bean = fillCyclic(new Bean()); verifyCyclic(bean); Schema<Bean> schema = RuntimeSchema.getSchema(Bean.class); // print(schema); byte[] bytes = GraphIOUtil.toByteArray(bean, schema, LinkedBuffer.allocate(256)); Bean deBean = new Bean(); GraphIOUtil.mergeFrom(bytes, deBean, schema); verifyCyclic(deBean); }
public static void checkVarDelimitedBoundry(int initialGap, int secondWriteSize) { int bufferSize = 256; final LinkedBuffer lb = LinkedBuffer.allocate(bufferSize); WriteSession session = new WriteSession(lb, bufferSize); // Should fill up the buffer with initialGap byte(s) left StringSerializer.writeUTF8(repeatChar('a', bufferSize - initialGap), session, lb); // Write a string of length secondWriteSize that should be larger // than the next buffer size assertTrue(secondWriteSize > bufferSize); StringSerializer.writeUTF8VarDelimited(repeatChar('a', secondWriteSize), session, lb); }
@Test public void testEmployee() throws Exception { Schema<Employee> schema = RuntimeSchema.getSchema(Employee.class); Employee p = filledEmployee(); byte[] data = ProtostuffIOUtil.toByteArray(p, schema, LinkedBuffer.allocate(512)); Employee p2 = new Employee(); ProtostuffIOUtil.mergeFrom(data, p2, schema); // System.err.println(p2); assertEquals(p, p2); }
@Test public void testSimpleTask() throws Exception { Schema<Task> schema = RuntimeSchema.getSchema(Task.class); Task p = filledTask(); byte[] data = ProtostuffIOUtil.toByteArray(p, schema, LinkedBuffer.allocate(512)); Task p2 = new Task(); ProtostuffIOUtil.mergeFrom(data, p2, schema); // System.err.println(p2); assertEquals(p, p2); }
@Test public void testITask() throws Exception { // Because we mapped ITask to Task, this is ok. Schema<ITask> schema = RuntimeSchema.getSchema(ITask.class); ITask p = filledTask(); byte[] data = ProtostuffIOUtil.toByteArray(p, schema, LinkedBuffer.allocate(512)); ITask p2 = new Task(); ProtostuffIOUtil.mergeFrom(data, p2, schema); // System.err.println(p2); assertEquals(p, p2); }