Java Code Examples for io.protostuff.Schema#mergeFrom()

The following examples show how to use io.protostuff.Schema#mergeFrom() . 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: PolymorphicPojoCollectionSchema.java    From protostuff with Apache License 2.0 6 votes vote down vote up
static Object readObjectFrom(Input input, Schema<?> schema, Object owner,
        IdStrategy strategy, int number) throws IOException
{
    final Schema<Object> derivedSchema = strategy.resolvePojoFrom(input,
            number).getSchema();
    
    final Object pojo = derivedSchema.newMessage();
    
    if (input instanceof GraphInput)
    {
        // update the actual reference.
        ((GraphInput) input).updateLast(pojo, owner);
    }
    
    derivedSchema.mergeFrom(input, pojo);
    return pojo;
}
 
Example 2
Source File: PolymorphicPojoSchema.java    From protostuff with Apache License 2.0 6 votes vote down vote up
static Object readObjectFrom(Input input, Schema<?> schema, Object owner,
        IdStrategy strategy, int number) throws IOException
{
    final Schema<Object> derivedSchema = strategy.resolvePojoFrom(input,
            number).getSchema();
    
    final Object pojo = derivedSchema.newMessage();
    
    if (input instanceof GraphInput)
    {
        // update the actual reference.
        ((GraphInput) input).updateLast(pojo, owner);
    }
    
    derivedSchema.mergeFrom(input, pojo);
    return pojo;
}
 
Example 3
Source File: PolymorphicPojoMapSchema.java    From protostuff with Apache License 2.0 6 votes vote down vote up
static Object readObjectFrom(Input input, Schema<?> schema, Object owner,
        IdStrategy strategy, int number) throws IOException
{
    final Schema<Object> derivedSchema = strategy.resolvePojoFrom(input,
            number).getSchema();
    
    final Object pojo = derivedSchema.newMessage();
    
    if (input instanceof GraphInput)
    {
        // update the actual reference.
        ((GraphInput) input).updateLast(pojo, owner);
    }
    
    derivedSchema.mergeFrom(input, pojo);
    return pojo;
}
 
Example 4
Source File: ProtostuffSerializer.java    From turbo-rpc with Apache License 2.0 6 votes vote down vote up
public Request readRequest(ByteBuf byteBuf) throws IOException {
	int requestId = byteBuf.readInt();
	int serviceId = ByteBufUtils.readVarInt(byteBuf);
	Tracer tracer = TRACER_SERIALIZER.read(byteBuf);

	Schema<MethodParam> schema = schema(serviceId);
	MethodParam methodParam = null;

	if (EmptyMethodParam.class.equals(schema.typeClass())) {
		methodParam = EmptyMethodParam.empty();
	} else {
		ByteBufInput input = getOrUpdate(INPUT_ATTACHMENT_INDEX, INPUT_SUPPLIER);
		input.setByteBuf(byteBuf, true);

		methodParam = schema.newMessage();
		schema.mergeFrom(input, methodParam);
	}

	Request request = RecycleRequest.newInstance(requestId, serviceId, tracer, methodParam);

	return request;
}
 
Example 5
Source File: ProtoStuffSerializer.java    From Jupiter with Apache License 2.0 6 votes vote down vote up
@Override
public <T> T readObject(InputBuf inputBuf, Class<T> clazz) {
    Schema<T> schema = RuntimeSchema.getSchema(clazz);
    T msg = schema.newMessage();

    Input input = Inputs.getInput(inputBuf);
    try {
        schema.mergeFrom(input, msg);
        Inputs.checkLastTagWas(input, 0);
    } catch (IOException e) {
        ThrowUtil.throwException(e);
    } finally {
        inputBuf.release();
    }

    return msg;
}
 
Example 6
Source File: ProtoStuffSerializer.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@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 7
Source File: ProtoStuffSerializer.java    From Jupiter with Apache License 2.0 5 votes vote down vote up
@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 8
Source File: OLogProtostuffContent.java    From c5-replicator with Apache License 2.0 5 votes vote down vote up
public static <T extends Schema<T> & Message<T>> OLogProtostuffContent<T> deserialize(ByteBuffer buffer,
                                                                                      Schema<T> schema) {
  final ByteBufferInput input = new ByteBufferInput(buffer, false);
  final T message = schema.newMessage();

  try {
    schema.mergeFrom(input, message);
    return new OLogProtostuffContent<>(message);
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example 9
Source File: NioBufInput.java    From Jupiter with Apache License 2.0 5 votes vote down vote up
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 Jupiter with Apache License 2.0 5 votes vote down vote up
@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);

    // save old limit
    // final int oldLimit = this.limit;

    // this.limit = offset + 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);
    // checkLastTagWas(0);

    // restore old limit
    // this.limit = oldLimit;

    nioBuffer.position(nioBuffer.position() + length);
    return value;
}
 
Example 11
Source File: UnsafeNioBufInput.java    From Jupiter with Apache License 2.0 5 votes vote down vote up
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 12
Source File: UnsafeNioBufInput.java    From Jupiter with Apache License 2.0 5 votes vote down vote up
@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 13
Source File: NioBufInput.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
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 14
Source File: NioBufInput.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@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);

    // save old limit
    // final int oldLimit = this.limit;

    // this.limit = offset + 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);
    // checkLastTagWas(0);

    // restore old limit
    // this.limit = oldLimit;

    nioBuffer.position(nioBuffer.position() + length);
    return value;
}
 
Example 15
Source File: UnsafeNioBufInput.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
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 16
Source File: UnsafeNioBufInput.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@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 17
Source File: ProtoStuffSerializer.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@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 18
Source File: ProtoStuffSerializer.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T readObject(final ByteBuffer buf, final Class<T> clazz) {
    final Schema<T> schema = RuntimeSchema.getSchema(clazz);
    final T msg = schema.newMessage();

    final Input input = Inputs.getInput(buf);
    try {
        schema.mergeFrom(input, msg);
        Inputs.checkLastTagWas(input, 0);
    } catch (final IOException e) {
        ThrowUtil.throwException(e);
    }

    return msg;
}
 
Example 19
Source File: NumberSchema.java    From protostuff with Apache License 2.0 4 votes vote down vote up
static Object readObjectFrom(Input input, Schema<?> schema, Object owner,
        IdStrategy strategy) throws IOException
{
    final int number = input.readFieldNumber(schema);

    if (number == ID_POJO)
    {
        // AtomicInteger/AtomicLong
        final Schema<Object> derivedSchema = strategy.resolvePojoFrom(
                input, number).getSchema();

        final Object pojo = derivedSchema.newMessage();

        if (input instanceof GraphInput)
        {
            // update the actual reference.
            ((GraphInput) input).updateLast(pojo, owner);
        }

        derivedSchema.mergeFrom(input, pojo);
        return pojo;
    }

    final Object value;
    switch (number)
    {
        case ID_BYTE:
            value = BYTE.readFrom(input);
            break;
        case ID_SHORT:
            value = SHORT.readFrom(input);
            break;
        case ID_INT32:
            value = INT32.readFrom(input);
            break;
        case ID_INT64:
            value = INT64.readFrom(input);
            break;
        case ID_FLOAT:
            value = FLOAT.readFrom(input);
            break;
        case ID_DOUBLE:
            value = DOUBLE.readFrom(input);
            break;
        case ID_BIGDECIMAL:
            value = BIGDECIMAL.readFrom(input);
            break;
        case ID_BIGINTEGER:
            value = BIGINTEGER.readFrom(input);
            break;
        default:
            throw new ProtostuffException("Corrupt input.");
    }

    if (input instanceof GraphInput)
    {
        // update the actual reference.
        ((GraphInput) input).updateLast(value, owner);
    }

    if (0 != input.readFieldNumber(schema))
        throw new ProtostuffException("Corrupt input.");

    return value;
}