Java Code Examples for io.protostuff.Schema#newMessage()
The following examples show how to use
io.protostuff.Schema#newMessage() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProtoStuffSerializer.java From sofa-jraft with Apache License 2.0 | 6 votes |
@Override public <T> T readObject(final InputBuf inputBuf, final Class<T> clazz) { final Schema<T> schema = RuntimeSchema.getSchema(clazz); final T msg = schema.newMessage(); final Input input = Inputs.getInput(inputBuf); try { schema.mergeFrom(input, msg); Inputs.checkLastTagWas(input, 0); } catch (final IOException e) { ThrowUtil.throwException(e); } finally { inputBuf.release(); } return msg; }
Example 2
Source File: AbstractRuntimeCollectionSchemaTest.java From protostuff with Apache License 2.0 | 6 votes |
private void verifyObjectMapField() throws IOException { ObjectWrapper wrapper = new ObjectWrapper(); wrapper.obj = new PersistentObjectMap<String,Object>(15); Schema<ObjectWrapper> schema = getSchema(ObjectWrapper.class); byte[] data = toByteArray(wrapper, schema); ObjectWrapper parsed = schema.newMessage(); mergeFrom(data, 0, data.length, parsed, schema); assertNotNull(parsed.obj); assertTrue(parsed.obj instanceof PersistentObjectMap); @SuppressWarnings("unchecked") PersistentObjectMap<String,Object> map = (PersistentObjectMap<String,Object>)parsed.obj; assertEquals(15, map.id); }
Example 3
Source File: AbstractRuntimeCollectionSchemaTest.java From protostuff with Apache License 2.0 | 6 votes |
private void verifyMapField() throws IOException { MapWrapper wrapper = new MapWrapper(); wrapper.obj = new PersistentObjectMap<String,Object>(15); Schema<MapWrapper> schema = getSchema(MapWrapper.class); byte[] data = toByteArray(wrapper, schema); MapWrapper parsed = schema.newMessage(); mergeFrom(data, 0, data.length, parsed, schema); assertNotNull(parsed.obj); assertTrue(parsed.obj instanceof PersistentObjectMap); @SuppressWarnings("unchecked") PersistentObjectMap<String,Object> map = (PersistentObjectMap<String,Object>)parsed.obj; assertEquals(15, map.id); }
Example 4
Source File: AbstractRuntimeCollectionSchemaTest.java From protostuff with Apache License 2.0 | 6 votes |
private void verifyObjectListField() throws IOException { ObjectWrapper wrapper = new ObjectWrapper(); wrapper.obj = new PersistentObjectList<Object>(15); Schema<ObjectWrapper> schema = getSchema(ObjectWrapper.class); byte[] data = toByteArray(wrapper, schema); ObjectWrapper parsed = schema.newMessage(); mergeFrom(data, 0, data.length, parsed, schema); assertNotNull(parsed.obj); assertTrue(parsed.obj instanceof PersistentObjectList); @SuppressWarnings("unchecked") PersistentObjectList<Object> list = (PersistentObjectList<Object>)parsed.obj; assertEquals(15, list.id); }
Example 5
Source File: FileFormat.java From dremio-oss with Apache License 2.0 | 6 votes |
private static FileFormat get(FileConfig fileConfig) { // TODO (Amit H) Remove after defining classes for tsv, csv, and psv FileType fileType = fileConfig.getType(); if (fileType == FileType.CSV || fileType == FileType.TSV || fileType == FileType.PSV) { fileType = FileType.TEXT; } final Class<? extends FileFormat> fileFormatClass = FileFormatDefinitions.CLASS_TYPES.get(fileType); final Schema<FileFormat> schema = (Schema<FileFormat>) FileFormatDefinitions.SCHEMAS.get(fileFormatClass); final FileFormat fileFormat = schema.newMessage(); if (fileConfig.getExtendedConfig() != null) { ProtobufIOUtil.mergeFrom(fileConfig.getExtendedConfig().toByteArray(), fileFormat, schema); } fileFormat.setCtime(fileConfig.getCtime()); fileFormat.setName(fileConfig.getName()); fileFormat.setOwner(fileConfig.getOwner()); fileFormat.setFullPath(fileConfig.getFullPathList()); fileFormat.setVersion(fileConfig.getTag()); fileFormat.setLocation(fileConfig.getLocation()); return fileFormat; }
Example 6
Source File: UnsafeNioBufInput.java From Jupiter with Apache License 2.0 | 5 votes |
@Override public <T> T mergeObject(T value, final Schema<T> schema) throws IOException { if (decodeNestedMessageAsGroup) { return mergeObjectEncodedAsGroup(value, schema); } final int length = readRawVarInt32(); if (length < 0) { throw ProtocolException.negativeSize(); } if (nioBuffer.remaining() < length) { throw ProtocolException.misreportedSize(); } ByteBuffer dup = nioBuffer.slice(); dup.limit(length); if (value == null) { value = schema.newMessage(); } ByteBufferInput nestedInput = new ByteBufferInput(dup, false); schema.mergeFrom(nestedInput, value); if (!schema.isInitialized(value)) { throw new UninitializedMessageException(value, schema); } nestedInput.checkLastTagWas(0); nioBuffer.position(nioBuffer.position() + length); return value; }
Example 7
Source File: ProtoStuffSerializer.java From sofa-jraft with Apache License 2.0 | 5 votes |
@Override public <T> T readObject(final byte[] bytes, final int offset, final int length, final Class<T> clazz) { final Schema<T> schema = RuntimeSchema.getSchema(clazz); final T msg = schema.newMessage(); final Input input = Inputs.getInput(bytes, offset, length); try { schema.mergeFrom(input, msg); Inputs.checkLastTagWas(input, 0); } catch (final IOException e) { ThrowUtil.throwException(e); } return msg; }
Example 8
Source File: UnsafeNioBufInput.java From sofa-jraft with Apache License 2.0 | 5 votes |
@Override public <T> T mergeObject(T value, final Schema<T> schema) throws IOException { if (decodeNestedMessageAsGroup) { return mergeObjectEncodedAsGroup(value, schema); } final int length = readRawVarInt32(); if (length < 0) { throw ProtocolException.negativeSize(); } if (nioBuffer.remaining() < length) { throw ProtocolException.misreportedSize(); } ByteBuffer dup = nioBuffer.slice(); dup.limit(length); if (value == null) { value = schema.newMessage(); } ByteBufferInput nestedInput = new ByteBufferInput(dup, false); schema.mergeFrom(nestedInput, value); if (!schema.isInitialized(value)) { throw new UninitializedMessageException(value, schema); } nestedInput.checkLastTagWas(0); nioBuffer.position(nioBuffer.position() + length); return value; }
Example 9
Source File: UnsafeNioBufInput.java From sofa-jraft with Apache License 2.0 | 5 votes |
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; }
Example 10
Source File: NioBufInput.java From sofa-jraft with Apache License 2.0 | 5 votes |
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; }
Example 11
Source File: NullArrayElementTest.java From protostuff with Apache License 2.0 | 5 votes |
public void testNullInBetween() throws IOException { Schema<PojoWithNonPrimitiveArrays> schema = getSchema(PojoWithNonPrimitiveArrays.class); Pipe.Schema<PojoWithNonPrimitiveArrays> pipeSchema = ((RuntimeSchema<PojoWithNonPrimitiveArrays>) schema).getPipeSchema(); PojoWithNonPrimitiveArrays p = new PojoWithNonPrimitiveArrays( new Boolean[] { null, true, null, false, null }, new Character[] { null, 'a', null, 'b', null }, new Short[] { null, 1, null, 2, null }, new Integer[] { null, 1, null, 2, null }, new Long[] { null, 1l, null, 2l, null }, new Float[] { null, 1.1f, null, 2.2f, null }, new Double[] { null, 1.1d, null, 2.2d, null }, new String[] { null, "a", null, "b", null }, new ByteString[] { null, ByteString.copyFromUtf8("a"), null, ByteString.copyFromUtf8("b"), null }, new byte[][] { null, new byte[] { 'a' }, null, new byte[] { 'b' }, null }, new BigDecimal[] { null, new BigDecimal(1.1d), null, new BigDecimal(2.2d), null }, new BigInteger[] { null, new BigInteger("1"), null, new BigInteger("2"), null }, new Date[] { null, new Date(), null, new Date(), null }, new Size[] { null, Size.MEDIUM, null, Size.LARGE, null }, new SomePojo[] { null, new SomePojo("a"), null, new SomePojo("b"), null } ); byte[] data = toByteArray(p, schema); PojoWithNonPrimitiveArrays pFromByteArray = schema.newMessage(); mergeFrom(data, 0, data.length, pFromByteArray, schema); assertEquals(p, pFromByteArray); PojoWithNonPrimitiveArrays pFromStream = schema.newMessage(); ByteArrayInputStream in = new ByteArrayInputStream(data); mergeFrom(in, pFromStream, schema); assertEquals(p, pFromStream); roundTrip(p, schema, pipeSchema); }
Example 12
Source File: DubboProtostuffReader.java From joyrpc with Apache License 2.0 | 5 votes |
@Override public Object readObject() throws IOException { 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); Object result; try { String className = new String(classNameBytes); Schema mySchema = className.equals(schema.typeClass().getName()) ? schema : getSchema(forName(className)); result = mySchema.newMessage(); GraphIOUtil.mergeFrom(bytes, result, mySchema); if (result instanceof Wrapper) { result = ((Wrapper) result).getData(); } } catch (ClassNotFoundException e) { throw new IOException(e.getMessage(), e); } return result; }
Example 13
Source File: NullArrayElementTest.java From protostuff with Apache License 2.0 | 5 votes |
public void testArrayPrimitiveInMap() throws IOException { Schema<PojoWithArrayPrimitiveInMap> schema = getSchema(PojoWithArrayPrimitiveInMap.class); Pipe.Schema<PojoWithArrayPrimitiveInMap> pipeSchema = ((RuntimeSchema<PojoWithArrayPrimitiveInMap>) schema).getPipeSchema(); PojoWithArrayPrimitiveInMap p = new PojoWithArrayPrimitiveInMap(); p.boolHashMap.put(0, new boolean[] { true, false }); p.charHashMap.put(0, new char[] { 'a', 'b' }); p.shortHashMap.put(0, new short[] { 1, 2 }); p.intHashMap.put(0, new int[] { 1, 2 }); p.longHashMap.put(0, new long[] { 1l, 2l }); p.floatHashMap.put(0, new float[] { 1.1f, 2.2f }); p.doubleHashMap.put(0, new double[] { 1.1d, 2.2d }); byte[] data = toByteArray(p, schema); PojoWithArrayPrimitiveInMap pFromByteArray = schema.newMessage(); mergeFrom(data, 0, data.length, pFromByteArray, schema); assertEquals(p, pFromByteArray); PojoWithArrayPrimitiveInMap pFromStream = schema.newMessage(); ByteArrayInputStream in = new ByteArrayInputStream(data); mergeFrom(in, pFromStream, schema); assertEquals(p, pFromStream); roundTrip(p, schema, pipeSchema); }
Example 14
Source File: ProtoStuffSerializer.java From Jupiter with Apache License 2.0 | 5 votes |
@Override public <T> T readObject(byte[] bytes, int offset, int length, Class<T> clazz) { Schema<T> schema = RuntimeSchema.getSchema(clazz); T msg = schema.newMessage(); Input input = Inputs.getInput(bytes, offset, length); try { schema.mergeFrom(input, msg); Inputs.checkLastTagWas(input, 0); } catch (IOException e) { ThrowUtil.throwException(e); } return msg; }
Example 15
Source File: ProtostuffRuntimeMapTest.java From protostuff with Apache License 2.0 | 5 votes |
@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)); }
Example 16
Source File: NullArrayElementInObjectArrayTest.java From protostuff with Apache License 2.0 | 4 votes |
public void testNullLast() throws IOException { Schema<PojoWithNonPrimitiveArrays> schema = getSchema(PojoWithNonPrimitiveArrays.class); Pipe.Schema<PojoWithNonPrimitiveArrays> pipeSchema = ((RuntimeSchema<PojoWithNonPrimitiveArrays>) schema).getPipeSchema(); Object[] objectArray = new Object[] { new Boolean[] { true, false, null }, Arrays.asList(new Boolean[] { true, false, null }), new Character[] { 'a', 'b', null }, Arrays.asList(new Character[] { 'a', 'b', null }), new Short[] { 1, 2, null }, Arrays.asList(new Short[] { 1, 2, null }), new Integer[] { 1, 2, null }, Arrays.asList(new Integer[] { 1, 2, null }), new Long[] { 1l, 2l, null }, Arrays.asList(new Long[] { 1l, 2l, null }), new Float[] { 1.1f, 2.2f, null }, Arrays.asList(new Float[] { 1.1f, 2.2f, null }), new Double[] { 1.1d, 2.2d, null }, Arrays.asList(new Double[] { 1.1d, 2.2d, null }), new String[] { "a", "b", null }, Arrays.asList(new String[] { "a", "b", null }), new ByteString[] { ByteString.copyFromUtf8("a"), ByteString.copyFromUtf8("b"), null }, Arrays.asList(new ByteString[] { ByteString.copyFromUtf8("a"), ByteString.copyFromUtf8("b"), null }), new byte[][] { new byte[] { 'a' }, new byte[] { 'b' }, null }, Arrays.asList(new byte[][] { new byte[] { 'a' }, new byte[] { 'b' }, null }), new BigDecimal[] { new BigDecimal(1.1d), new BigDecimal(2.2d), null }, Arrays.asList(new BigDecimal[] { new BigDecimal(1.1d), new BigDecimal(2.2d), null }), new BigInteger[] { new BigInteger("1"), new BigInteger("2"), null }, Arrays.asList(new BigInteger[] { new BigInteger("1"), new BigInteger("2"), null }), new Date[] { new Date(), new Date(), null }, Arrays.asList(new Date[] { new Date(), new Date(), null }), new Baz[] { new Baz(0, "0", 0), new Baz(1, "1", 1), null }, Arrays.asList(new Baz[] { new Baz(0, "0", 0), new Baz(1, "1", 1), null }) }; PojoWithNonPrimitiveArrays p = new PojoWithNonPrimitiveArrays(objectArray); byte[] data = toByteArray(p, schema); PojoWithNonPrimitiveArrays pFromByteArray = schema.newMessage(); mergeFrom(data, 0, data.length, pFromByteArray, schema); assertArrayObjectEquals(p, pFromByteArray); PojoWithNonPrimitiveArrays pFromStream = schema.newMessage(); ByteArrayInputStream in = new ByteArrayInputStream(data); mergeFrom(in, pFromStream, schema); assertArrayObjectEquals(p, pFromStream); roundTrip(p, schema, pipeSchema); }
Example 17
Source File: NullArrayElementInObjectArrayTest.java From protostuff with Apache License 2.0 | 4 votes |
public void testNullFirst() throws IOException { Schema<PojoWithNonPrimitiveArrays> schema = getSchema(PojoWithNonPrimitiveArrays.class); Pipe.Schema<PojoWithNonPrimitiveArrays> pipeSchema = ((RuntimeSchema<PojoWithNonPrimitiveArrays>) schema).getPipeSchema(); Object[] objectArray = new Object[] { new Boolean[] { null, true, false }, Arrays.asList(new Boolean[] { null, true, false }), new Character[] { null, 'a', 'b' }, Arrays.asList(new Character[] { null, 'a', 'b' }), new Short[] { null, 1, 2 }, Arrays.asList(new Short[] { null, 1, 2 }), new Integer[] { null, 1, 2 }, Arrays.asList(new Integer[] { null, 1, 2 }), new Long[] { null, 1l, 2l }, Arrays.asList(new Long[] { null, 1l, 2l }), new Float[] { null, 1.1f, 2.2f }, Arrays.asList(new Float[] { null, 1.1f, 2.2f }), new Double[] { null, 1.1d, 2.2d }, Arrays.asList(new Double[] { null, 1.1d, 2.2d }), new String[] { null, "a", "b" }, Arrays.asList(new String[] { null, "a", "b" }), new ByteString[] { null, ByteString.copyFromUtf8("a"), ByteString.copyFromUtf8("b") }, Arrays.asList(new ByteString[] { null, ByteString.copyFromUtf8("a"), ByteString.copyFromUtf8("b") }), new byte[][] { null, new byte[] { 'a' }, new byte[] { 'b' } }, Arrays.asList(new byte[][] { null, new byte[] { 'a' }, new byte[] { 'b' } }), new BigDecimal[] { null, new BigDecimal(1.1d), new BigDecimal(2.2d) }, Arrays.asList(new BigDecimal[] { null, new BigDecimal(1.1d), new BigDecimal(2.2d) }), new BigInteger[] { null, new BigInteger("1"), new BigInteger("2") }, Arrays.asList(new BigInteger[] { null, new BigInteger("1"), new BigInteger("2") }), new Date[] { null, new Date(), new Date() }, Arrays.asList(new Date[] { null, new Date(), new Date() }), new Baz[] { null, new Baz(0, "0", 0), new Baz(1, "1", 1) }, Arrays.asList(new Baz[] { null, new Baz(0, "0", 0), new Baz(1, "1", 1) }) }; PojoWithNonPrimitiveArrays p = new PojoWithNonPrimitiveArrays(objectArray); byte[] data = toByteArray(p, schema); PojoWithNonPrimitiveArrays pFromByteArray = schema.newMessage(); mergeFrom(data, 0, data.length, pFromByteArray, schema); assertArrayObjectEquals(p, pFromByteArray); PojoWithNonPrimitiveArrays pFromStream = schema.newMessage(); ByteArrayInputStream in = new ByteArrayInputStream(data); mergeFrom(in, pFromStream, schema); assertArrayObjectEquals(p, pFromStream); roundTrip(p, schema, pipeSchema); }
Example 18
Source File: ProtostuffNullArrayElementTest.java From protostuff with Apache License 2.0 | 4 votes |
@Override protected <T> void roundTrip(T message, Schema<T> schema, Pipe.Schema<T> pipeSchema) throws IOException { 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(protostuff.length == protostuffFromStream.length); assertEquals(STRING.deser(protostuff), STRING.deser(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(protobufRoundTrip.length == protobufRoundTripFromStream.length); String strProtobufRoundTrip = STRING.deser(protobufRoundTrip); assertEquals(strProtobufRoundTrip, STRING.deser(protobufRoundTripFromStream)); assertTrue(protobufRoundTrip.length == protobuf.length); assertEquals(strProtobufRoundTrip, STRING.deser(protobuf)); }
Example 19
Source File: BusProtoStuffMessageConverter.java From hawkbit with Eclipse Public License 1.0 | 4 votes |
private static EventType readClassHeader(final byte[] typeInformation) { final Schema<EventType> schema = RuntimeSchema.getSchema(EventType.class); final EventType deserializedType = schema.newMessage(); ProtobufIOUtil.mergeFrom(typeInformation, deserializedType, schema); return deserializedType; }
Example 20
Source File: JobsProtoUtil.java From dremio-oss with Apache License 2.0 | 3 votes |
/** * Generic method to convert Protobuf to Protostuff. Safe to use Protostuff's serializer as it can properly * deserialize messages serialized by Protobuf. * @param protostuffSchema Protostuff object schema * @param protobuf Protobuf object to convert * @param <M> Type of Protobuf * @param <T> Type of Protostuff * @return Converted object as Protostuff */ private static <M extends GeneratedMessageV3, T extends Message<T> & Schema<T>> T toStuff(Schema<T> protostuffSchema, M protobuf) { T message = protostuffSchema.newMessage(); ProtobufIOUtil.mergeFrom(protobuf.toByteArray(), message, protostuffSchema); return message; }