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

The following examples show how to use io.protostuff.Schema#writeTo() . 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 vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> byte[] writeObject(final T obj) {
    final Schema<T> schema = RuntimeSchema.getSchema((Class<T>) obj.getClass());

    final LinkedBuffer buf = LinkedBuffers.getLinkedBuffer();
    final Output output = Outputs.getOutput(buf);
    try {
        schema.writeTo(output, obj);
        return Outputs.toByteArray(output);
    } catch (final IOException e) {
        ThrowUtil.throwException(e);
    } finally {
        LinkedBuffers.resetBuf(buf); // for reuse
    }

    return null; // never get here
}
 
Example 2
Source File: ProtoStuffSerializer.java    From Jupiter with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> byte[] writeObject(T obj) {
    Schema<T> schema = RuntimeSchema.getSchema((Class<T>) obj.getClass());

    LinkedBuffer buf = LinkedBuffers.getLinkedBuffer();
    Output output = Outputs.getOutput(buf);
    try {
        schema.writeTo(output, obj);
        return Outputs.toByteArray(output);
    } catch (IOException e) {
        ThrowUtil.throwException(e);
    } finally {
        LinkedBuffers.resetBuf(buf); // for reuse
    }

    return null; // never get here
}
 
Example 3
Source File: EntryEncodingUtil.java    From c5-replicator with Apache License 2.0 6 votes vote down vote up
/**
 * Serialize a protostuff message object, prefixed with message length, and suffixed with a 4-byte CRC.
 *
 * @param schema  Protostuff message schema
 * @param message Object to serialize
 * @param <T>     Message type
 * @return A list of ByteBuffers containing a varInt length, followed by the message, followed by a 4-byte CRC.
 */
public static <T> List<ByteBuffer> encodeWithLengthAndCrc(Schema<T> schema, T message) {
  final LinkBuffer messageBuf = new LinkBuffer();
  final LowCopyProtobufOutput lcpo = new LowCopyProtobufOutput(messageBuf);

  try {
    schema.writeTo(lcpo, message);

    final int length = Ints.checkedCast(lcpo.buffer.size());
    final LinkBuffer lengthBuf = new LinkBuffer().writeVarInt32(length);

    return appendCrcToBufferList(
        Lists.newArrayList(
            Iterables.concat(lengthBuf.finish(), messageBuf.finish()))
    );
  } catch (IOException e) {
    // This method performs no IO, so it should not actually be possible for an IOException to be thrown.
    // But just in case...
    throw new RuntimeException(e);
  }
}
 
Example 4
Source File: ProtostuffEncoder.java    From c5-replicator with Apache License 2.0 6 votes vote down vote up
@Override
protected void encode(ChannelHandlerContext ctx, Message<T> msg, List<Object> out) throws Exception {
  Schema<T> schema = msg.cachedSchema();

  LowCopyProtobufOutput lcpo = new LowCopyProtobufOutput();
  schema.writeTo(lcpo, (T) msg);

  List<ByteBuffer> buffers = lcpo.buffer.finish();

  long size = lcpo.buffer.size();
  if (size > Integer.MAX_VALUE) {
    throw new EncoderException("Serialized form was too large, actual size: " + size);
  }

  out.add(Unpooled.wrappedBuffer(buffers.toArray(new ByteBuffer[]{})));
}
 
Example 5
Source File: PolymorphicPojoMapSchema.java    From protostuff with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
static void writeObjectTo(Output output, Object value,
        Schema<?> currentSchema, IdStrategy strategy) throws IOException
{
    final HasSchema<Object> hs = strategy.tryWritePojoIdTo(output, ID_POJO, 
            (Class<Object>)value.getClass(), true);
    
    if (hs == null)
    {
        PolymorphicMapSchema.writeObjectTo(output, value, currentSchema, strategy);
        return;
    }
    
    final Schema<Object> schema = hs.getSchema();
    
    if (output instanceof StatefulOutput)
    {
        // update using the derived schema.
        ((StatefulOutput) output).updateLast(schema, currentSchema);
    }
    
    schema.writeTo(output, value);
}
 
Example 6
Source File: DerivativeSchema.java    From protostuff with Apache License 2.0 6 votes vote down vote up
/**
 * Delegates to the schema derived from the {@code value}.
 */
@Override
@SuppressWarnings("unchecked")
public void writeTo(final Output output, final Object value)
        throws IOException
{
    final Schema<Object> schema = strategy.writePojoIdTo(output, ID_POJO,
            (Class<Object>) value.getClass()).getSchema();

    if (output instanceof StatefulOutput)
    {
        // update using the derived schema.
        ((StatefulOutput) output).updateLast(schema, this);
    }

    // write the rest of the fields of the exact type
    schema.writeTo(output, value);
}
 
Example 7
Source File: PolymorphicPojoCollectionSchema.java    From protostuff with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
static void writeObjectTo(Output output, Object value,
        Schema<?> currentSchema, IdStrategy strategy) throws IOException
{
    final HasSchema<Object> hs = strategy.tryWritePojoIdTo(output, ID_POJO, 
            (Class<Object>)value.getClass(), true);
    
    if (hs == null)
    {
        PolymorphicCollectionSchema.writeObjectTo(output, value, currentSchema, 
                strategy);
        return;
    }
    
    final Schema<Object> schema = hs.getSchema();
    
    if (output instanceof StatefulOutput)
    {
        // update using the derived schema.
        ((StatefulOutput) output).updateLast(schema, currentSchema);
    }
    
    schema.writeTo(output, value);
}
 
Example 8
Source File: PolymorphicThrowableSchema.java    From protostuff with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
static void writeObjectTo(Output output, Object value,
        Schema<?> currentSchema, IdStrategy strategy) throws IOException
{
    final Schema<Object> schema = strategy.writePojoIdTo(output,
            ID_THROWABLE, (Class<Object>) value.getClass()).getSchema();

    if (output instanceof StatefulOutput)
    {
        // update using the derived schema.
        ((StatefulOutput) output).updateLast(schema, currentSchema);
    }

    if (tryWriteWithoutCause(output, value, schema))
        return;

    schema.writeTo(output, value);
}
 
Example 9
Source File: ProtoStuffSerializer.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> OutputBuf writeObject(final OutputBuf outputBuf, final T obj) {
    final Schema<T> schema = RuntimeSchema.getSchema((Class<T>) obj.getClass());

    final Output output = Outputs.getOutput(outputBuf);
    try {
        schema.writeTo(output, obj);
    } catch (final IOException e) {
        ThrowUtil.throwException(e);
    }

    return outputBuf;
}
 
Example 10
Source File: ProtostuffSerializer.java    From turbo-rpc with Apache License 2.0 5 votes vote down vote up
public void writeRequest(ByteBuf byteBuf, Request request) throws IOException {
	final int beginWriterIndex = byteBuf.writerIndex();

	byteBuf.writeInt(0);
	// byteBuf.writerIndex(beginWriterIndex + TurboConstants.HEADER_FIELD_LENGTH);
	byteBuf.writeInt(request.getRequestId());
	ByteBufUtils.writeVarInt(byteBuf, request.getServiceId());
	TRACER_SERIALIZER.write(byteBuf, request.getTracer());

	ByteBufOutput output = getOrUpdate(OUTPUT_ATTACHMENT_INDEX, OUTPUT_SUPPLIER);
	output.setByteBuf(byteBuf);

	if (request.getMethodParam() == null) {
		emptyMethodParamSchema.writeTo(output, null);
	} else {
		// serviceId 为服务端概念,相同的方法在不同的服务端 serviceId 会不相同,所以没法直接使用 serviceId 获取 schema
		// 如果能够证明这里比较慢,可以考虑在 request 中加入 methodId,这个在客户端是唯一的
		Class<? extends MethodParam> clazz = request.getMethodParam().getClass();
		Schema<MethodParam> schema = schema(clazz);

		schema.writeTo(output, request.getMethodParam());
	}

	int finishWriterIndex = byteBuf.writerIndex();
	int length = finishWriterIndex - beginWriterIndex - TurboConstants.HEADER_FIELD_LENGTH;

	byteBuf.setInt(beginWriterIndex, length);

	// System.out.println("request content: " + new
	// String(ByteBufUtil.getBytes(byteBuf.duplicate())));
	// System.out.println("request length: " + byteBuf.writerIndex());

	RecycleUtils.release(request);
}
 
Example 11
Source File: ProtoStuffSerializer.java    From Jupiter with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> OutputBuf writeObject(OutputBuf outputBuf, T obj) {
    Schema<T> schema = RuntimeSchema.getSchema((Class<T>) obj.getClass());

    Output output = Outputs.getOutput(outputBuf);
    try {
        schema.writeTo(output, obj);
    } catch (IOException e) {
        ThrowUtil.throwException(e);
    }

    return outputBuf;
}
 
Example 12
Source File: NumberSchema.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
static void writeObjectTo(Output output, Object value,
        Schema<?> currentSchema, IdStrategy strategy) throws IOException
{
    final Class<Object> clazz = (Class<Object>) value.getClass();

    final RuntimeFieldFactory<Object> inline = RuntimeFieldFactory
            .getInline(clazz);
    if (inline != null)
    {
        // scalar value
        inline.writeTo(output, inline.id, value, false);
        return;
    }

    // AtomicInteger/AtomicLong
    final Schema<Object> schema = strategy.writePojoIdTo(output, ID_POJO,
            clazz).getSchema();

    if (output instanceof StatefulOutput)
    {
        // update using the derived schema.
        ((StatefulOutput) output).updateLast(schema, currentSchema);
    }

    schema.writeTo(output, value);
}
 
Example 13
Source File: PolymorphicPojoSchema.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
static void writeObjectTo(Output output, Object value,
        Schema<?> currentSchema, IdStrategy strategy) throws IOException
{
    final Schema<Object> schema = strategy.writePojoIdTo(output,
            ID_POJO, (Class<Object>) value.getClass()).getSchema();
    
    if (output instanceof StatefulOutput)
    {
        // update using the derived schema.
        ((StatefulOutput) output).updateLast(schema, currentSchema);
    }
    
    schema.writeTo(output, value);
}
 
Example 14
Source File: NioBufOutput.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
@Override
public <T> void writeObject(int fieldNumber, T value, Schema<T> schema, boolean repeated) throws IOException {
    writeVarInt32(makeTag(fieldNumber, WIRETYPE_START_GROUP));
    schema.writeTo(this, value);
    writeVarInt32(makeTag(fieldNumber, WIRETYPE_END_GROUP));
}
 
Example 15
Source File: NioBufOutput.java    From Jupiter with Apache License 2.0 4 votes vote down vote up
@Override
public <T> void writeObject(int fieldNumber, T value, Schema<T> schema, boolean repeated) throws IOException {
    writeVarInt32(makeTag(fieldNumber, WIRETYPE_START_GROUP));
    schema.writeTo(this, value);
    writeVarInt32(makeTag(fieldNumber, WIRETYPE_END_GROUP));
}