io.protostuff.Output Java Examples

The following examples show how to use io.protostuff.Output. 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: DefaultEvent.java    From datawave with Apache License 2.0 6 votes vote down vote up
public void writeTo(Output output, DefaultEvent message) throws IOException {
    if (message.markings != null)
        output.writeObject(1, message.markings, MapSchema.SCHEMA, false);
    
    if (message.metadata != null) {
        output.writeObject(2, message.metadata, Metadata.getSchema(), false);
    }
    
    if (message.fields != null) {
        Schema<DefaultField> schema = null;
        for (DefaultField field : message.fields) {
            if (field != null) {
                if (schema == null) {
                    schema = field.cachedSchema();
                }
                output.writeObject(3, field, schema, true);
            }
        }
    }
}
 
Example #3
Source File: DefaultIdStrategy.java    From protostuff with Apache License 2.0 6 votes vote down vote up
@Override
protected void writeMapIdTo(Output output, int fieldNumber, Class<?> clazz)
        throws IOException
{
    final MapSchema.MessageFactory factory = mapMapping.get(clazz);
    if (factory == null && clazz.getName().startsWith("java.util")
        && MapSchema.MessageFactories.accept(clazz.getSimpleName()))
    {
        // jdk map
        // better not to register the jdk map if using this strategy
        // as it saves space by not writing the full package
        output.writeString(fieldNumber, clazz.getSimpleName(), false);
    }
    else
    {
        output.writeString(fieldNumber, clazz.getName(), false);
    }
}
 
Example #4
Source File: PolymorphicCollectionSchema.java    From protostuff with Apache License 2.0 6 votes vote down vote up
private static void writeCheckedCollectionTo(Output output, Object value,
        Schema<?> currentSchema, IdStrategy strategy, int id)
        throws IOException
{
    final Object c, type;
    try
    {
        c = fCheckedCollection_c.get(value);
        type = fCheckedCollection_type.get(value);
    }
    catch (Exception e)
    {
        throw new RuntimeException(e);
    }

    output.writeObject(id, c, strategy.POLYMORPHIC_COLLECTION_SCHEMA, false);
    output.writeObject(1, type, strategy.CLASS_SCHEMA, false);
}
 
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: PolymorphicEnumSchema.java    From protostuff with Apache License 2.0 6 votes vote down vote up
static void transferObject(Pipe.Schema<Object> pipeSchema, Pipe pipe,
        Input input, Output output, IdStrategy strategy) throws IOException
{
    if (ID_ENUM != input.readFieldNumber(pipeSchema.wrappedSchema))
        throw new ProtostuffException("Corrupt input.");

    strategy.transferEnumId(input, output, ID_ENUM);

    if (ID_ENUM_VALUE != input.readFieldNumber(pipeSchema.wrappedSchema))
        throw new ProtostuffException("Corrupt input.");

    EnumIO.transfer(pipe, input, output, 1, false, strategy);

    if (0 != input.readFieldNumber(pipeSchema.wrappedSchema))
        throw new ProtostuffException("Corrupt input.");
}
 
Example #7
Source File: FacetQueryResponse.java    From datawave with Apache License 2.0 5 votes vote down vote up
public void writeTo(Output output, FacetQueryResponse message) throws IOException {
    
    if (message.getQueryId() != null) {
        output.writeString(1, message.getQueryId(), false);
    }
    
    if (message.getLogicName() != null) {
        output.writeString(2, message.getLogicName(), false);
    }
    
    output.writeUInt64(3, message.getOperationTimeMS(), false);
    
    List<String> messages = message.getMessages();
    if (messages != null) {
        for (String msg : messages) {
            if (msg != null)
                output.writeString(4, msg, true);
        }
    }
    
    List<QueryExceptionType> exceptions = message.getExceptions();
    if (exceptions != null) {
        for (QueryExceptionType exception : exceptions) {
            if (exception != null)
                output.writeObject(5, exception, QueryExceptionType.getSchema(), true);
        }
    }
    
    if (message.facets != null) {
        for (DefaultFacets facet : message.facets) {
            if (facet != null)
                output.writeObject(6, facet, DefaultFacets.getSchema(), true);
        }
    }
}
 
Example #8
Source File: PolymorphicCollectionSchema.java    From protostuff with Apache License 2.0 5 votes vote down vote up
private static void writeSynchronizedCollectionTo(Output output,
        Object value, Schema<?> currentSchema, IdStrategy strategy, int id)
        throws IOException
{
    final Object c, mutex;
    try
    {
        c = fSynchronizedCollection_c.get(value);
        mutex = fSynchronizedCollection_mutex.get(value);
    }
    catch (Exception e)
    {
        throw new RuntimeException(e);
    }

    if (mutex != value)
    {
        // TODO for future release, introduce an interface(GraphOutput) so
        // we
        // can check whether the output can retain references.
        throw new RuntimeException(
                "This exception is thrown to fail fast. "
                        + "Synchronized collections with a different mutex would only "
                        + "work if graph format is used, since the reference is retained.");
    }

    output.writeObject(id, c, strategy.POLYMORPHIC_COLLECTION_SCHEMA, false);
}
 
Example #9
Source File: ArraySchemas.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
public void writeTo(Output output, Object value) throws IOException
{
    Date[] array = (Date[]) value;
    output.writeInt32(ID_ARRAY_LEN, array.length, false);

    int nullCount = 0;
    for (int i = 0, len = array.length; i < len; i++)
    {
        Date v = array[i];
        if (v != null)
        {
            if (nullCount != 0)
            {
                output.writeUInt32(ID_ARRAY_NULLCOUNT, nullCount, false);
                nullCount = 0;
            }

            output.writeFixed64(ID_ARRAY_DATA, v.getTime(), true);
        }
        else if (allowNullArrayElement)
        {
            nullCount++;
        }
    }

    // if last element is null
    if (nullCount != 0)
        output.writeUInt32(ID_ARRAY_NULLCOUNT, nullCount, false);
}
 
Example #10
Source File: DefaultIdStrategy.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
protected <T> HasSchema<T> tryWritePojoIdTo(Output output, int fieldNumber,
        Class<T> clazz, boolean registered) throws IOException
{
    HasSchema<T> hs = getSchemaWrapper(clazz, false);
    if (hs == null || (registered && hs instanceof Lazy<?>))
        return null;
    
    output.writeString(fieldNumber, clazz.getName(), false);
    
    return hs;
}
 
Example #11
Source File: LocalTimeSchema.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
@Override
public void writeTo(final Output output, final LocalTime message) throws IOException {
    output.writeInt32(1, message.getHour(), false);
    output.writeInt32(2, message.getMinute(), false);
    output.writeInt32(3, message.getSecond(), false);
    output.writeInt32(4, message.getNano(), false);
}
 
Example #12
Source File: TracerDelegate.java    From turbo-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public void writeTo(Output output, int number, Tracer tracer, boolean repeated) throws IOException {
	if (!(output instanceof ByteBufOutput)) {
		throw new IOException("only support ByteBufOutput");
	}

	ByteBuf byteBuf = ((ByteBufOutput) output).getByteBuf();
	ByteBufUtils.writeVarInt(byteBuf, makeTag(number, WIRETYPE_LENGTH_DELIMITED));
	tracerSerializer.write(byteBuf, tracer);
}
 
Example #13
Source File: ArraySchemas.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
protected void transfer(Pipe pipe, Input input, Output output)
        throws IOException
{
    transferObject(this, pipe, input, output, strategy,
            RuntimeFieldFactory.INT32);
}
 
Example #14
Source File: PolymorphicPojoMapSchema.java    From protostuff with Apache License 2.0 5 votes vote down vote up
static void transferObject(Pipe.Schema<Object> pipeSchema, Pipe pipe,
        Input input, Output output, IdStrategy strategy) throws IOException
{
    final int number = input.readFieldNumber(pipeSchema.wrappedSchema);
    if (number != ID_POJO)
        PolymorphicMapSchema.transferObject(pipeSchema, pipe, input, output, strategy, number);
    else
        transferObject(pipeSchema, pipe, input, output, strategy, number);
}
 
Example #15
Source File: ArraySchemas.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
public void writeTo(Output output, Object value) throws IOException
{
    ByteString[] array = (ByteString[]) value;
    output.writeInt32(ID_ARRAY_LEN, array.length, false);

    int nullCount = 0;
    for (int i = 0, len = array.length; i < len; i++)
    {
        ByteString v = array[i];
        if (v != null)
        {
            if (nullCount != 0)
            {
                output.writeUInt32(ID_ARRAY_NULLCOUNT, nullCount, false);
                nullCount = 0;
            }

            output.writeBytes(ID_ARRAY_DATA, v, true);
        }
        else if (allowNullArrayElement)
        {
            nullCount++;
        }
    }

    // if last element is null
    if (nullCount != 0)
        output.writeUInt32(ID_ARRAY_NULLCOUNT, nullCount, false);
}
 
Example #16
Source File: NioBufInput.java    From Jupiter with Apache License 2.0 5 votes vote down vote up
@Override
public void transferByteRangeTo(Output output, boolean utf8String, int fieldNumber,
                                boolean repeated) throws IOException {
    final int length = readRawVarInt32();
    if (length < 0) {
        throw ProtocolException.negativeSize();
    }

    if (utf8String) {
        // if it is a UTF string, we have to call the writeByteRange.

        if (nioBuffer.hasArray()) {
            output.writeByteRange(true, fieldNumber, nioBuffer.array(),
                    nioBuffer.arrayOffset() + nioBuffer.position(), length, repeated);
            nioBuffer.position(nioBuffer.position() + length);
        } else {
            byte[] bytes = new byte[length];
            nioBuffer.get(bytes);
            output.writeByteRange(true, fieldNumber, bytes, 0, bytes.length, repeated);
        }
    } else {
        // Do the potentially vastly more efficient potential splice call.
        if (nioBuffer.remaining() < length) {
            throw ProtocolException.misreportedSize();
        }

        ByteBuffer dup = nioBuffer.slice();
        dup.limit(length);

        output.writeBytes(fieldNumber, dup, repeated);

        nioBuffer.position(nioBuffer.position() + length);
    }
}
 
Example #17
Source File: DefaultField.java    From datawave with Apache License 2.0 5 votes vote down vote up
@Override
public void writeTo(Output output, DefaultField message) throws IOException {
    if (message.markings != null)
        output.writeObject(1, message.markings, MapSchema.SCHEMA, false);
    if (message.columnVisibility != null)
        output.writeString(2, message.columnVisibility, false);
    output.writeUInt64(3, message.timestamp, false);
    if (message.name != null)
        output.writeString(4, message.name, false);
    if (message.value != null)
        output.writeObject(5, message.value, message.value.cachedSchema(), false);
}
 
Example #18
Source File: ArraySchemas.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
public void writeTo(Output output, Object value) throws IOException
{
    BigDecimal[] array = (BigDecimal[]) value;
    output.writeInt32(ID_ARRAY_LEN, array.length, false);

    int nullCount = 0;
    for (int i = 0, len = array.length; i < len; i++)
    {
        BigDecimal v = array[i];
        if (v != null)
        {
            if (nullCount != 0)
            {
                output.writeUInt32(ID_ARRAY_NULLCOUNT, nullCount, false);
                nullCount = 0;
            }

            output.writeString(ID_ARRAY_DATA, v.toString(), true);
        }
        else if (allowNullArrayElement)
        {
            nullCount++;
        }
    }

    // if last element is null
    if (nullCount != 0)
        output.writeUInt32(ID_ARRAY_NULLCOUNT, nullCount, false);
}
 
Example #19
Source File: ArraySchemas.java    From protostuff with Apache License 2.0 5 votes vote down vote up
static void transferObject(Pipe.Schema<Object> pipeSchema, Pipe pipe,
        Input input, Output output, IdStrategy strategy,
        Delegate<?> delegate) throws IOException
{
    if (ID_ARRAY_LEN != input.readFieldNumber(pipeSchema.wrappedSchema))
        throw new ProtostuffException("Corrupt input.");

    int len = input.readInt32();
    // write it back
    output.writeInt32(ID_ARRAY_LEN, len, false);
    
    // if from derived schema and the array is boxed, the length written
    // during serialization is: -(len + 1)
    if (len < 0)
        len = -len - 1;

    for (int i = 0, nullCount = 0; i < len;)
    {
        switch (input.readFieldNumber(pipeSchema.wrappedSchema))
        {
            case ID_ARRAY_DATA:
                i++;
                delegate.transfer(pipe, input, output, ID_ARRAY_DATA, true);
                break;
            case ID_ARRAY_NULLCOUNT:
                nullCount = input.readUInt32();
                i += nullCount;
                output.writeUInt32(ID_ARRAY_NULLCOUNT, nullCount, false);
                break;
            default:
                throw new ProtostuffException("Corrupt input.");
        }
    }

    if (0 != input.readFieldNumber(pipeSchema.wrappedSchema))
        throw new ProtostuffException("Corrupt input.");
}
 
Example #20
Source File: ArraySchemas.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
protected void transfer(Pipe pipe, Input input, Output output)
        throws IOException
{
    if (ID_ARRAY_LEN != input
            .readFieldNumber(pipeSchema.wrappedSchema))
        throw new ProtostuffException("Corrupt input.");

    final int len = input.readInt32();
    // write it back
    output.writeInt32(ID_ARRAY_LEN, len, false);

    for (int i = 0, nullCount = 0; i < len;)
    {
        switch (input.readFieldNumber(pipeSchema.wrappedSchema))
        {
            case ID_ARRAY_DATA:
                i++;
                output.writeObject(ID_ARRAY_DATA, pipe, hs.getPipeSchema(),
                        true);
                break;
            case ID_ARRAY_NULLCOUNT:
                nullCount = input.readUInt32();
                i += nullCount;
                output.writeUInt32(ID_ARRAY_NULLCOUNT, nullCount, false);
                break;
            default:
                throw new ProtostuffException("Corrupt input.");
        }
    }

    if (0 != input.readFieldNumber(pipeSchema.wrappedSchema))
        throw new ProtostuffException("Corrupt input.");
}
 
Example #21
Source File: BulkResultsInfoResponseList.java    From datawave with Apache License 2.0 5 votes vote down vote up
public void writeTo(Output output, BulkResultsInfoResponseList message) throws IOException {
    
    if (message.getQueryId() != null) {
        output.writeString(1, message.getQueryId(), false);
    }
    
    if (message.getLogicName() != null) {
        output.writeString(2, message.getLogicName(), false);
    }
    
    output.writeUInt64(3, message.getOperationTimeMS(), false);
    
    List<String> messages = message.getMessages();
    if (messages != null) {
        for (String msg : messages) {
            if (msg != null)
                output.writeString(4, msg, true);
        }
    }
    
    List<QueryExceptionType> exceptions = message.getExceptions();
    if (exceptions != null) {
        for (QueryExceptionType exception : exceptions) {
            if (exception != null)
                output.writeObject(5, exception, QueryExceptionType.getSchema(), true);
        }
    }
    
    if (message.bulkResults != null) {
        for (BulkResultsInfoResponse result : message.bulkResults) {
            if (result != null)
                output.writeObject(6, result, BulkResultsInfoResponse.getSchema(), true);
        }
    }
}
 
Example #22
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 #23
Source File: ArraySchemas.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
protected void transfer(Pipe pipe, Input input, Output output)
        throws IOException
{
    transferObject(this, pipe, input, output, strategy,
            RuntimeFieldFactory.INT64);
}
 
Example #24
Source File: JobExecution.java    From datawave with Apache License 2.0 5 votes vote down vote up
public void writeTo(Output output, JobExecution message) throws IOException {
    
    if (message.getMapReduceJobId() != null) {
        output.writeString(1, message.getMapReduceJobId(), false);
    }
    
    output.writeUInt64(2, message.getTimestamp(), false);
    
    if (message.getState() != null) {
        output.writeString(3, message.getState(), false);
    }
}
 
Example #25
Source File: Outputs.java    From Jupiter with Apache License 2.0 5 votes vote down vote up
public static byte[] toByteArray(Output output) {
    if (output instanceof WriteSession) {
        return ((WriteSession) output).toByteArray();
    }
    throw new IllegalArgumentException("Output [" + Reflects.simpleClassName(output)
            + "] must be a WriteSession's implementation");
}
 
Example #26
Source File: MapReduceInfoResponseList.java    From datawave with Apache License 2.0 5 votes vote down vote up
public void writeTo(Output output, MapReduceInfoResponseList message) throws IOException {
    
    if (message.getResults() != null) {
        for (MapReduceInfoResponse response : message.getResults()) {
            if (null != response) {
                output.writeObject(1, response, MapReduceInfoResponse.getSchema(), true);
            }
        }
    }
    
    output.writeUInt64(2, message.getOperationTimeMS(), false);
    
    List<String> messages = message.getMessages();
    if (messages != null) {
        for (String msg : messages) {
            if (msg != null)
                output.writeString(3, msg, true);
        }
    }
    
    List<QueryExceptionType> exceptions = message.getExceptions();
    if (exceptions != null) {
        for (QueryExceptionType exception : exceptions) {
            if (exception != null)
                output.writeObject(4, exception, QueryExceptionType.getSchema(), true);
        }
    }
    
}
 
Example #27
Source File: ArraySchema.java    From protostuff with Apache License 2.0 5 votes vote down vote up
static void writeObjectTo(Output output, Object value,
        Schema<?> currentSchema, IdStrategy strategy) throws IOException
{
    final Class<?> clazz = value.getClass();
    int dimensions = 1;
    Class<?> componentType = clazz.getComponentType();
    while (componentType.isArray())
    {
        dimensions++;
        componentType = componentType.getComponentType();
    }

    strategy.writeArrayIdTo(output, componentType);
    // write the length of the array
    output.writeUInt32(ID_ARRAY_LEN, ((Object[])value).length, false);
    // write the dimensions of the array
    output.writeUInt32(ID_ARRAY_DIMENSION, dimensions, false);

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

    strategy.ARRAY_SCHEMA.writeTo(output, value);
}
 
Example #28
Source File: FileDetails.java    From datawave with Apache License 2.0 5 votes vote down vote up
public void writeTo(Output output, FileDetails message) throws IOException {
    
    if (message.getPath() != null) {
        output.writeString(1, message.getPath(), false);
    }
    if (message.getDate() != null) {
        output.writeUInt64(2, message.getDate().getTime(), false);
    }
    output.writeUInt64(3, message.getSize(), false);
}
 
Example #29
Source File: NumericIdStrategy.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
protected void transferClassId(Input input, Output output, int fieldNumber,
        boolean mapped, boolean array) throws IOException
{
    if (mapped)
        input.transferByteRangeTo(output, true, fieldNumber, false);
    else
        output.writeUInt32(fieldNumber, input.readUInt32(), false);
}
 
Example #30
Source File: ArraySchemas.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
protected void transfer(Pipe pipe, Input input, Output output)
        throws IOException
{
    transferObject(this, pipe, input, output, strategy,
            RuntimeFieldFactory.BOOL);
}