io.protostuff.Input Java Examples

The following examples show how to use io.protostuff.Input. 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: DefaultFacets.java    From datawave with Apache License 2.0 6 votes vote down vote up
public void mergeFrom(Input input, DefaultFacets message) throws IOException {
    int number;
    while ((number = input.readFieldNumber(this)) != 0) {
        switch (number) {
            case 1:
                if (message.fields == null)
                    message.fields = new ArrayList<FieldCardinalityBase>();
                DefaultFieldCardinality f = input.mergeObject(null, DefaultFieldCardinality.getSchema());
                message.fields.add(f);
                break;
            default:
                input.handleUnknownField(number, this);
                break;
        }
    }
}
 
Example #2
Source File: ObjectSchema.java    From protostuff with Apache License 2.0 6 votes vote down vote up
static ArrayWrapper newArrayWrapper(Input input, Schema<?> schema,
        boolean mapped, IdStrategy strategy) throws IOException
{
    final Class<?> componentType = strategy.resolveArrayComponentTypeFrom(
            input, mapped);

    if (input.readFieldNumber(schema) != ID_ARRAY_LEN)
        throw new ProtostuffException("Corrupt input.");
    final int len = input.readUInt32();

    if (input.readFieldNumber(schema) != ID_ARRAY_DIMENSION)
        throw new ProtostuffException("Corrupt input.");
    final int dimensions = input.readUInt32();

    if (dimensions == 1)
        return new ArrayWrapper(Array.newInstance(componentType, len));

    final int[] arg = new int[dimensions];
    arg[0] = len;
    return new ArrayWrapper(Array.newInstance(componentType, arg));
}
 
Example #3
Source File: ObjectSchema.java    From protostuff with Apache License 2.0 6 votes vote down vote up
static void transferArray(Pipe pipe, Input input, Output output, int number,
        Pipe.Schema<?> pipeSchema, boolean mapped, IdStrategy strategy) throws IOException
{
    strategy.transferArrayId(input, output, number, mapped);

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

    output.writeUInt32(ID_ARRAY_LEN, input.readUInt32(), false);

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

    output.writeUInt32(ID_ARRAY_DIMENSION, input.readUInt32(), false);

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

    Pipe.transferDirect(strategy.ARRAY_PIPE_SCHEMA, pipe, input, output);
}
 
Example #4
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 #5
Source File: ArraySchemas.java    From protostuff with Apache License 2.0 6 votes vote down vote up
protected Object readPrimitiveFrom(Input input, Object owner, int len)
        throws IOException
{
    short[] array = new short[len];
    if (input instanceof GraphInput)
    {
        // update the actual reference.
        ((GraphInput) input).updateLast(array, owner);
    }

    for (int i = 0; i < len; i++)
    {
        if (ID_ARRAY_DATA != input.readFieldNumber(this))
            throw new ProtostuffException("Corrupt input.");

        array[i] = (short) input.readUInt32();
    }

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

    return array;
}
 
Example #6
Source File: OffsetDateTimeSchema.java    From joyrpc with Apache License 2.0 6 votes vote down vote up
@Override
public void mergeFrom(final Input input, final OffsetDateTime message) throws IOException {
    while (true) {
        int number = input.readFieldNumber(this);
        switch (number) {
            case 0:
                return;
            case 1:
                LocalDateTime localDateTime = LocalDateTime.now();
                input.mergeObject(localDateTime, LocalDateTimeSchema.INSTANCE);
                setValue(FIELD_DATE_TIME, message, localDateTime);
                break;
            case 2:
                //不能使用0,0会缓存结果对象
                ZoneOffset offset = ZoneOffset.ofTotalSeconds(1);
                input.mergeObject(offset, ZoneOffsetSchema.INSTANCE);
                setValue(FIELD_ZONE_OFFSET, message, offset);
                break;
            default:
                input.handleUnknownField(number, this);
        }
    }
}
 
Example #7
Source File: LocalDateTimeSchema.java    From joyrpc with Apache License 2.0 6 votes vote down vote up
@Override
public void mergeFrom(final Input input, final LocalDateTime message) throws IOException {
    while (true) {
        int number = input.readFieldNumber(this);
        switch (number) {
            case 0:
                return;
            case 1:
                LocalDate localDate = LocalDate.now();
                input.mergeObject(localDate, LocalDateSchema.INSTANCE);
                setValue(FIELD_DATE, message, localDate);
                break;
            case 2:
                LocalTime localTime = LocalTime.now();
                input.mergeObject(localTime, LocalTimeSchema.INSTANCE);
                setValue(FIELD_TIME, message, localTime);
                break;
            default:
                input.handleUnknownField(number, this);
        }
    }
}
 
Example #8
Source File: ArraySchemas.java    From protostuff with Apache License 2.0 6 votes vote down vote up
protected Object readPrimitiveFrom(Input input, Object owner, int len)
        throws IOException
{
    boolean[] array = new boolean[len];
    if (input instanceof GraphInput)
    {
        // update the actual reference.
        ((GraphInput) input).updateLast(array, owner);
    }

    for (int i = 0; i < len; i++)
    {
        if (ID_ARRAY_DATA != input.readFieldNumber(this))
            throw new ProtostuffException("Corrupt input.");

        array[i] = input.readBool();
    }

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

    return array;
}
 
Example #9
Source File: LocalDateSchema.java    From joyrpc with Apache License 2.0 6 votes vote down vote up
@Override
public void mergeFrom(final Input input, final LocalDate message) throws IOException {
    while (true) {
        int number = input.readFieldNumber(this);
        switch (number) {
            case 0:
                return;
            case 1:
                setValue(FIELD_YEAR, message, input.readInt32());
                break;
            case 2:
                setValue(FIELD_MONTH, message, (short) input.readInt32());
                break;
            case 3:
                setValue(FIELD_DAY, message, (short) input.readInt32());
                break;
            default:
                input.handleUnknownField(number, this);
        }
    }
}
 
Example #10
Source File: ArraySchemas.java    From protostuff with Apache License 2.0 6 votes vote down vote up
protected Object readPrimitiveFrom(Input input, Object owner, int len)
        throws IOException
{
    float[] array = new float[len];
    if (input instanceof GraphInput)
    {
        // update the actual reference.
        ((GraphInput) input).updateLast(array, owner);
    }

    for (int i = 0; i < len; i++)
    {
        if (ID_ARRAY_DATA != input.readFieldNumber(this))
            throw new ProtostuffException("Corrupt input.");

        array[i] = input.readFloat();
    }

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

    return array;
}
 
Example #11
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 #12
Source File: IncrementalIdStrategy.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected <T> HasSchema<T> transferPojoId(Input input, Output output, int fieldNumber)
        throws IOException
{
    final int id = input.readUInt32();

    final BaseHS<T> wrapper = id < pojos.size() ? (BaseHS<T>) pojos.get(id) : null;
    if (wrapper == null)
        throw new UnknownTypeException("unknown pojo id: " + id);

    output.writeUInt32(fieldNumber, id, false);

    return wrapper;
}
 
Example #13
Source File: ArraySchemas.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
public Object readFrom(Input input, Object owner) throws IOException
{
    if (ID_ARRAY_LEN != input.readFieldNumber(this))
        throw new ProtostuffException("Corrupt input.");

    final int len = input.readInt32();

    Object[] array = (Object[])Array.newInstance(delegate.typeClass(), len);
    if (input instanceof GraphInput)
    {
        // update the actual reference.
        ((GraphInput) input).updateLast(array, owner);
    }

    for (int i = 0; i < len;)
    {
        switch (input.readFieldNumber(this))
        {
            case ID_ARRAY_DATA:
                array[i++] = delegate.readFrom(input);
                break;
            case ID_ARRAY_NULLCOUNT:
                i += input.readUInt32();
                break;
            default:
                throw new ProtostuffException("Corrupt input.");
        }
    }

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

    return array;
}
 
Example #14
Source File: DefaultIdStrategy.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
protected <T> HasSchema<T> resolvePojoFrom(Input input, int fieldNumber)
        throws IOException
{
    final String className = input.readString();

    final HasSchema<T> wrapper = getSchemaWrapper(className,
            0 != (AUTO_LOAD_POLYMORPHIC_CLASSES & flags));
    if (wrapper == null)
        throw new ProtostuffException("polymorphic pojo not registered: "
                + className);

    return wrapper;
}
 
Example #15
Source File: NumericIdStrategy.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
protected Class<?> resolveArrayComponentTypeFrom(Input input, boolean mapped)
        throws IOException
{
    return mapped ? RuntimeEnv.loadClass(input.readString()) :
            resolveClass(input.readUInt32());
}
 
Example #16
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 #17
Source File: ZoneIdSchema.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
@Override
public void mergeFrom(final Input input, final ZoneId message) throws IOException {
    while (true) {
        int number = input.readFieldNumber(this);
        switch (number) {
            case 0:
                return;
            case 1:
                setValue(FIELD_ID, message, input.readString());
                break;
            default:
                input.handleUnknownField(number, this);
        }
    }
}
 
Example #18
Source File: ArraySchemas.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
public Object readFrom(Input input, Object owner) throws IOException
{
    if (ID_ARRAY_LEN != input.readFieldNumber(this))
        throw new ProtostuffException("Corrupt input.");

    final int len = input.readInt32();

    Object[] array = (Object[])Array.newInstance(eio.enumClass, len);
    if (input instanceof GraphInput)
    {
        // update the actual reference.
        ((GraphInput) input).updateLast(array, owner);
    }

    for (int i = 0; i < len;)
    {
        switch (input.readFieldNumber(this))
        {
            case ID_ARRAY_DATA:
                array[i++] = eio.readFrom(input);
                break;
            case ID_ARRAY_NULLCOUNT:
                i += input.readUInt32();
                break;
            default:
                throw new ProtostuffException("Corrupt input.");
        }
    }

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

    return array;
}
 
Example #19
Source File: PolymorphicPojoSchema.java    From protostuff with Apache License 2.0 5 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)
        throw new ProtostuffException("Corrupt input.");
    
    return readObjectFrom(input, schema, owner, strategy, number);
}
 
Example #20
Source File: ArraySchemas.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
public Object readFrom(Input input, Object owner) throws IOException
{
    if (ID_ARRAY_LEN != input.readFieldNumber(this))
        throw new ProtostuffException("Corrupt input.");

    final int len = input.readInt32();
    return primitive ? readPrimitiveFrom(input, owner, len) : 
            readBoxedFrom(input, owner, len);
}
 
Example #21
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.SHORT);
}
 
Example #22
Source File: NumericIdStrategy.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
protected Class<?> resolveClassFrom(Input input, boolean mapped,
        boolean array) throws IOException
{
    return mapped ? RuntimeEnv.loadClass(input.readString()) :
            resolveClass(input.readUInt32());
}
 
Example #23
Source File: PolymorphicCollectionSchema.java    From protostuff with Apache License 2.0 5 votes vote down vote up
private static Object readCheckedCollectionFrom(Input input,
        Schema<?> schema, Object owner, IdStrategy strategy, boolean graph,
        Object collection, boolean ss, boolean list) throws IOException
{
    if (graph)
    {
        // update the actual reference.
        ((GraphInput) input).updateLast(collection, owner);
    }

    final Wrapper wrapper = new Wrapper();
    Object c = input.mergeObject(wrapper,
            strategy.POLYMORPHIC_COLLECTION_SCHEMA);
    if (!graph || !((GraphInput) input).isCurrentMessageReference())
        c = wrapper.value;

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

    Object type = input.mergeObject(wrapper, strategy.CLASS_SCHEMA);
    if (!graph || !((GraphInput) input).isCurrentMessageReference())
        type = wrapper.value;
    try
    {
        fCheckedCollection_c.set(collection, c);
        fCheckedCollection_type.set(collection, type);

        if (ss)
            fCheckedSortedSet_ss.set(collection, c);

        if (list)
            fCheckedList_list.set(collection, c);
    }
    catch (Exception e)
    {
        throw new RuntimeException(e);
    }

    return collection;
}
 
Example #24
Source File: MapReduceInfoResponseList.java    From datawave with Apache License 2.0 5 votes vote down vote up
public void mergeFrom(Input input, MapReduceInfoResponseList message) throws IOException {
    List<MapReduceInfoResponse> responses = null;
    LinkedList<QueryExceptionType> exceptions = null;
    int number;
    while ((number = input.readFieldNumber(this)) != 0) {
        switch (number) {
            case 1:
                if (responses == null)
                    responses = new ArrayList<MapReduceInfoResponse>();
                responses.add(input.mergeObject(null, MapReduceInfoResponse.getSchema()));
                break;
            case 2:
                message.setOperationTimeMS(input.readUInt64());
                break;
            case 3:
                message.addMessage(input.readString());
                break;
            case 4:
                if (exceptions == null)
                    exceptions = new LinkedList<QueryExceptionType>();
                exceptions.add(input.mergeObject(null, QueryExceptionType.getSchema()));
                break;
            default:
                input.handleUnknownField(number, this);
                break;
        }
    }
    if (exceptions != null)
        message.setExceptions(exceptions);
    if (responses != null)
        message.setResults(responses);
}
 
Example #25
Source File: DefaultEdgeQueryResponse.java    From datawave with Apache License 2.0 5 votes vote down vote up
public void mergeFrom(Input input, DefaultEdgeQueryResponse message) throws IOException {
    LinkedList<QueryExceptionType> exceptions = null;
    int number;
    while ((number = input.readFieldNumber(this)) != 0) {
        switch (number) {
            case 1:
                message.setQueryId(input.readString());
                break;
            case 2:
                message.setLogicName(input.readString());
                break;
            case 3:
                message.setOperationTimeMS(input.readUInt64());
                break;
            case 4:
                message.addMessage(input.readString());
                break;
            case 5:
                if (exceptions == null)
                    exceptions = new LinkedList<QueryExceptionType>();
                exceptions.add(input.mergeObject(null, QueryExceptionType.getSchema()));
                break;
            case 6:
                message.securityMarkings = input.readString();
                break;
            case 7:
                if (message.edges == null)
                    message.edges = new ArrayList<DefaultEdge>();
                message.edges.add(input.mergeObject(null, DefaultEdge.getSchema()));
                break;
            default:
                input.handleUnknownField(number, this);
                break;
        }
    }
    if (exceptions != null)
        message.setExceptions(exceptions);
}
 
Example #26
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 #27
Source File: ExplicitIdStrategy.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
protected EnumIO<?> resolveEnumFrom(Input input) throws IOException
{
    final int id = input.readUInt32();

    final RegisteredEnumIO reio = id < enums.size() ? enums.get(id) : null;
    if (reio == null)
        throw new UnknownTypeException("enum id: " + id + " (Outdated registry)");

    return reio.eio;
}
 
Example #28
Source File: EnumIO.java    From protostuff with Apache License 2.0 5 votes vote down vote up
/**
 * Transfers the {@link Enum} from the input to the output.
 */
public static void transfer(Pipe pipe, Input input, Output output,
        int number, boolean repeated, IdStrategy strategy) throws IOException
{
    if (0 == (IdStrategy.ENUMS_BY_NAME & strategy.flags))
        output.writeEnum(number, input.readEnum(), repeated);
    else
        input.transferByteRangeTo(output, true, number, repeated);
}
 
Example #29
Source File: ArraySchemas.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
public Object readFrom(Input input, Object owner) throws IOException
{
    if (ID_ARRAY_LEN != input.readFieldNumber(this))
        throw new ProtostuffException("Corrupt input.");

    final int len = input.readInt32();
    return primitive ? readPrimitiveFrom(input, owner, len) : 
            readBoxedFrom(input, owner, len);
}
 
Example #30
Source File: ArraySchemas.java    From protostuff with Apache License 2.0 5 votes vote down vote up
protected Object readBoxedFrom(Input input, Object owner, int len)
        throws IOException
{
    final Long[] array = new Long[len];
    if (input instanceof GraphInput)
    {
        // update the actual reference.
        ((GraphInput) input).updateLast(array, owner);
    }

    for (int i = 0; i < len;)
    {
        switch (input.readFieldNumber(this))
        {
            case ID_ARRAY_DATA:
                array[i++] = input.readInt64();
                break;
            case ID_ARRAY_NULLCOUNT:
                i += input.readUInt32();
                break;
            default:
                throw new ProtostuffException("Corrupt input.");
        }
    }

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

    return array;
}