Refine search
private <T> T mergeObjectEncodedAsGroup(T value, final Schema<T> schema) throws IOException { if (value == null) value = schema.newMessage(); schema.mergeFrom(this, value); if (!schema.isInitialized(value)) throw new UninitializedMessageException(value, schema); // handling is in #readFieldNumber checkLastTagWas(0); return value; }
/** * Serializes the {@code message} into an {@link OutputStream} using the given schema. * * @return the size of the message */ public static <T> int writeTo(OutputStream out, T message, Schema<T> schema, LinkedBuffer buffer) throws IOException { if (buffer.start != buffer.offset) throw new IllegalArgumentException("Buffer previously used and had not been reset."); final ProtobufOutput output = new ProtobufOutput(buffer); schema.writeTo(output, message); return LinkedBuffer.writeTo(out, buffer); }
@Override public String getFieldName(int number) { return schema.getFieldName(number); }
@Override public void mergeFrom(Input input, final Object message) throws IOException { final Schema<Object> schema = lastSchema; // merge using this input. schema.mergeFrom(this, message); if (!schema.isInitialized(message)) throw new UninitializedMessageException(message, schema); // restore lastSchema = schema; }
/** * Serializes the {@code message} into an {@link XMLStreamWriter} using the given {@code schema}. */ public static <T> void writeTo(XMLStreamWriter writer, T message, Schema<T> schema) throws IOException, XMLStreamException, XmlOutputException { writer.writeStartElement(schema.messageName()); schema.writeTo(new XmlOutput(writer, schema), message); writer.writeEndElement(); }
public void testImmutablePojo() throws Exception { Schema<ImmutablePojo> schema = RuntimeSchema .getSchema(ImmutablePojo.class); ImmutablePojo p = new ImmutablePojo(3, "ip"); byte[] data = ProtostuffIOUtil.toByteArray(p, schema, buf()); ImmutablePojo p2 = schema.newMessage(); ProtostuffIOUtil.mergeFrom(data, 0, data.length, p2, schema); assertEquals(p, p2); List<ImmutablePojo> list = new ArrayList<ImmutablePojo>(); list.add(p); list.add(p2); ByteArrayOutputStream out = new ByteArrayOutputStream(); ProtostuffIOUtil.writeListTo(out, list, schema, buf()); byte[] listData = out.toByteArray(); ByteArrayInputStream in = new ByteArrayInputStream(listData); List<ImmutablePojo> parsedList = ProtostuffIOUtil.parseListFrom(in, schema); assertEquals(list, parsedList); }
@Override protected <T> void mergeFrom(byte[] data, int offset, int length, T message, Schema<T> schema) throws IOException { ByteArrayInputStream in = new ByteArrayInputStream(data, offset, length); try { schema.mergeFrom(new KvpInput(in, numeric), message); } catch (ArrayIndexOutOfBoundsException e) { throw new ProtostuffException("Truncated message.", e); } }
@SuppressWarnings("ResultOfMethodCallIgnored") @Override public Object readObject() throws IOException, ClassNotFoundException { int classNameLength = dis.readInt(); int bytesLength = dis.readInt(); if (classNameLength < 0 || bytesLength < 0) { throw new IOException(); } byte[] classNameBytes = new byte[classNameLength]; dis.readFully(classNameBytes, 0, classNameLength); byte[] bytes = new byte[bytesLength]; dis.readFully(bytes, 0, bytesLength); String className = new String(classNameBytes); Class clazz = Class.forName(className); Object result; if (WrapperUtils.needWrapper(clazz)) { Schema<Wrapper> schema = RuntimeSchema.getSchema(Wrapper.class); Wrapper wrapper = schema.newMessage(); GraphIOUtil.mergeFrom(bytes, wrapper, schema); result = wrapper.getData(); } else { Schema schema = RuntimeSchema.getSchema(clazz); result = schema.newMessage(); GraphIOUtil.mergeFrom(bytes, result, schema); } return result; }
@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()); }
private void deserTest(Message origMsg, Schema sch, ByteBuffer buf) throws IOException { ByteBufferInput input = new ByteBufferInput(buf, false); Object newM = sch.newMessage(); sch.mergeFrom(input, newM); assertEquals(origMsg, newM); }
/** * Used by the code generated messages that implement {@link java.io.Externalizable}. Writes to the * {@link DataOutput} . * * @return the size of the message. */ public static <T> int writeDelimitedTo(DataOutput out, T message, Schema<T> schema) throws IOException { final LinkedBuffer buffer = new LinkedBuffer(LinkedBuffer.MIN_BUFFER_SIZE); final ProtostuffOutput output = new ProtostuffOutput(buffer); schema.writeTo(output, message); ProtobufOutput.writeRawVarInt32Bytes(out, output.size); LinkedBuffer.writeTo(out, buffer); return output.size; }
@Override public void writeTo(Output output, T message) throws IOException { schema.writeTo(output, message); }
@Override public void mergeFrom(Input input, T message) throws IOException { schema.mergeFrom(input, message); }
static <T> byte[] toByteArrayBufferedProtostuff(T message, Schema<T> schema) { final ProtostuffOutput output = new ProtostuffOutput(new LinkedBuffer(BUF_SIZE)); try { schema.writeTo(output, message); } catch (IOException e) { throw new RuntimeException("Serializing to a byte array threw an IOException " + "(should never happen).", e); } return output.toByteArray(); }
@Override public Class<? super T> typeClass() { return schema.typeClass(); }
@Override public boolean isInitialized(T message) { return schema.isInitialized(message); }
public void testFoo() throws Exception { Schema<Foo> schema = Foo.getSchema(); Foo message = SerializableObjects.foo; ByteArrayOutputStream out = new ByteArrayOutputStream(); int size = optWriteDelimitedTo(out, message, schema, buf()); int delimSize = ProtobufOutput.computeRawVarint32Size(size); byte[] data = out.toByteArray(); int expectedSize = size + delimSize; assertEquals(expectedSize, data.length); verifyOptData(data, message, schema, buf()); ByteArrayInputStream in = new ByteArrayInputStream(data); Foo parsedMessage = schema.newMessage(); boolean merged = optMergeDelimitedFrom(in, parsedMessage, schema, buf(512)); assertTrue(merged); assertEquals(message, parsedMessage); }
@SuppressWarnings("ResultOfMethodCallIgnored") @Override public Object readObject() throws IOException, ClassNotFoundException { int classNameLength = dis.readInt(); int bytesLength = dis.readInt(); if (classNameLength < 0 || bytesLength < 0) { throw new IOException(); } byte[] classNameBytes = new byte[classNameLength]; dis.readFully(classNameBytes, 0, classNameLength); byte[] bytes = new byte[bytesLength]; dis.readFully(bytes, 0, bytesLength); String className = new String(classNameBytes); Class clazz = Class.forName(className); Object result; if (WrapperUtils.needWrapper(clazz)) { Schema<Wrapper> schema = RuntimeSchema.getSchema(Wrapper.class); Wrapper wrapper = schema.newMessage(); GraphIOUtil.mergeFrom(bytes, wrapper, schema); result = wrapper.getData(); } else { Schema schema = RuntimeSchema.getSchema(clazz); result = schema.newMessage(); GraphIOUtil.mergeFrom(bytes, result, schema); } return result; }
private void deserTest(Message origMsg, Schema sch, ByteBuffer buf) throws IOException { ByteBufferInput input = new ByteBufferInput(buf, true); Object newM = sch.newMessage(); sch.mergeFrom(input, newM); assertEquals(origMsg, newM); }
/** * Used by the code generated messages that implement {@link java.io.Externalizable}. Writes to the * {@link DataOutput} . * * @return the size of the message. */ public static <T> int writeDelimitedTo(DataOutput out, T message, Schema<T> schema) throws IOException { final LinkedBuffer buffer = new LinkedBuffer(LinkedBuffer.MIN_BUFFER_SIZE); final ProtostuffOutput output = new ProtostuffOutput(buffer); final GraphProtostuffOutput graphOutput = new GraphProtostuffOutput(output); schema.writeTo(graphOutput, message); ProtobufOutput.writeRawVarInt32Bytes(out, output.size); LinkedBuffer.writeTo(out, buffer); return output.size; }