Java Code Examples for io.protostuff.StringSerializer.STRING#deser()

The following examples show how to use io.protostuff.StringSerializer.STRING#deser() . 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: JsonCompareOutputsTest.java    From protostuff with Apache License 2.0 6 votes vote down vote up
public void testBazNumeric() throws Exception
{
    for (Baz bazCompare : new Baz[] { baz, negativeBaz })
    {
        byte[] jo = JSON_OUTPUT_NUMERIC.serialize(bazCompare);
        byte[] jbo = JSON_BUFFERED_OUTPUT_NUMERIC.serialize(bazCompare);
        byte[] jso = JSON_STREAMED_OUTPUT_NUMERIC.serialize(bazCompare);

        assertTrue(jo.length == jbo.length);
        assertTrue(jso.length == jo.length);

        String joString = STRING.deser(jo);
        String jboString = STRING.deser(jbo);

        assertEquals(joString, jboString);
        assertEquals(joString, STRING.deser(jso));
    }
}
 
Example 2
Source File: YamlPipeTest.java    From protostuff with Apache License 2.0 6 votes vote down vote up
static <T> void protobufRoundTrip(T message, Schema<T> schema,
        Pipe.Schema<T> pipeSchema) throws Exception
{
    byte[] protobuf = ProtobufIOUtil.toByteArray(message, schema, buf());

    ByteArrayInputStream protobufStream = new ByteArrayInputStream(protobuf);

    byte[] yaml = YamlIOUtil.toByteArray(
            ProtobufIOUtil.newPipe(protobuf, 0, protobuf.length), pipeSchema, buf());

    byte[] yamlFromStream = YamlIOUtil.toByteArray(
            ProtobufIOUtil.newPipe(protobufStream), pipeSchema, buf());

    assertTrue(yaml.length == yamlFromStream.length);

    String strYaml = STRING.deser(yaml);
    String strYamlFromStream = STRING.deser(yamlFromStream);
    assertEquals(strYaml, strYamlFromStream);
}
 
Example 3
Source File: B64CodeTest.java    From protostuff with Apache License 2.0 6 votes vote down vote up
public void testDecodeTo() throws Exception
{
    for (String str : new String[] { "abcdefgh", "1", "12", "123", "1234", "12345" })
    {
        byte[] b64Encoded = B64Code.encode(str.getBytes("UTF-8"));

        byte[] decoded = new byte[16];

        int decodedLen = B64Code.decodeTo(decoded, 0, b64Encoded, 0, b64Encoded.length);

        byte[] decodedFromString = B64Code.decode(new String(b64Encoded, "UTF-8"));

        String a = STRING.deser(decoded, 0, decodedLen);
        String b = STRING.deser(decodedFromString);

        boolean x = a.equals(b);
        int lenA = a.length();
        int lenB = b.length();

        assertEquals(STRING.deser(decoded, 0, decodedLen), STRING.deser(decodedFromString));

        assertEquals(str, STRING.deser(decoded, 0, decodedLen));
    }
}
 
Example 4
Source File: NumberParser.java    From protostuff with Apache License 2.0 6 votes vote down vote up
/**
 * Parse an ascii int from a raw buffer.
 */
public static int parseInt(final byte[] buffer, final int start, final int length,
        final int radix) throws NumberFormatException
{
    if (length == 0)
        throw new NumberFormatException(STRING.deser(buffer, start, length));

    if (buffer[start] == '-')
    {
        if (length == 1)
            throw new NumberFormatException(STRING.deser(buffer, start, length));

        return parseInt(buffer, start + 1, length - 1, radix, false);
    }

    return parseInt(buffer, start, length, radix, true);
}
 
Example 5
Source File: YamlSerTest.java    From protostuff with Apache License 2.0 6 votes vote down vote up
public void testEmptyDeeperFoo() throws Exception
{
    Bar bar = new Bar();
    Baz baz = new Baz();
    bar.setSomeBaz(baz);
    ArrayList<Bar> bars = new ArrayList<Bar>();
    bars.add(bar);
    Foo foo = new Foo();
    foo.setSomeBar(bars);

    byte[] data = toByteArray(foo, foo.cachedSchema());

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    writeTo(out, foo, foo.cachedSchema());
    byte[] data2 = out.toByteArray();

    String text = STRING.deser(data);
    String text2 = STRING.deser(data2);

    assertEquals(text, text2);
    print(text);
}
 
Example 6
Source File: CodedInput.java    From protostuff with Apache License 2.0 6 votes vote down vote up
/**
 * Read a {@code string} field value from the stream.
 */
@Override
public String readString() throws IOException
{
    final int size = readRawVarint32();
    if (size <= (bufferSize - bufferPos) && size > 0)
    {
        // Fast path: We already have the bytes in a contiguous buffer, so
        // just copy directly from it.
        final String result = STRING.deser(buffer, bufferPos, size);
        bufferPos += size;
        return result;
    }
    else
    {
        // Slow path: Build a byte array first then copy it.
        return STRING.deser(readRawBytes(size));
    }
}
 
Example 7
Source File: JsonCompareOutputsTest.java    From protostuff with Apache License 2.0 6 votes vote down vote up
public void testBar() throws Exception
{
    for (Bar barCompare : new Bar[] { bar, negativeBar })
    {
        byte[] jo = JSON_OUTPUT.serialize(barCompare);
        byte[] jbo = JSON_BUFFERED_OUTPUT.serialize(barCompare);
        byte[] jso = JSON_STREAMED_OUTPUT.serialize(barCompare);

        assertTrue(jo.length == jbo.length);
        assertTrue(jso.length == jo.length);

        String joString = STRING.deser(jo);
        String jboString = STRING.deser(jbo);

        assertEquals(joString, jboString);
        assertEquals(joString, STRING.deser(jso));
    }
}
 
Example 8
Source File: KvpByteArrayInput.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
public String readString() throws IOException
{
    final int size = buffer[offset++] | (buffer[offset++] << 8);
    if (size == 0)
        return ByteString.EMPTY_STRING;

    if (offset + size > limit)
        throw new ProtostuffException("Misreported size.");

    final String str = STRING.deser(buffer, offset, size);
    offset += size;
    return str;
}
 
Example 9
Source File: ProtobufNullArrayElementInObjectArrayTest.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@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));

    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 10
Source File: StreamedStringSerializerTest.java    From protostuff with Apache License 2.0 5 votes vote down vote up
static void checkEncodedStringWithVarDelimited(byte[] buffer, String str, int offset, int len) throws Exception
{
    assertEquals(len, readRawVarint32(buffer, offset));

    String deser = STRING.deser(buffer, offset + 2, len);

    assertEquals(str.length(), deser.length());
    assertEquals(str, deser);
}
 
Example 11
Source File: ByteBufferInput.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
public String readString() throws IOException
{
    final int length = readRawVarint32();
    if (length < 0)
        throw ProtobufException.negativeSize();

    if (buffer.remaining() < length)
        throw ProtobufException.misreportedSize();

    // if(offset + length > limit)

    if (buffer.hasArray())
    {
        final int currPosition = buffer.position();
        buffer.position(buffer.position() + length);
        return STRING.deser(buffer.array(),
                buffer.arrayOffset() + currPosition,
                length);
    }
    else
    {
        byte[] tmp = new byte[length];
        buffer.get(tmp);
        return STRING.deser(tmp);
    }

    // final int offset = this.offset;

    // this.offset += length;

    // return STRING.deser(buffer, offset, length);
}
 
Example 12
Source File: JsonEscapeTest.java    From protostuff with Apache License 2.0 5 votes vote down vote up
public void testFoo() throws Exception
{
    Bar bar = new Bar();
    bar.setSomeInt(1);
    bar.setSomeBytes(ByteString.copyFromUtf8(ESCAPE_TARGET));
    bar.setSomeString(ESCAPE_TARGET);

    ArrayList<Bar> bars = new ArrayList<Bar>();
    bars.add(bar);
    bars.add(bar);

    ArrayList<String> strings = new ArrayList<String>();
    strings.add(ESCAPE_TARGET);
    strings.add("");
    strings.add(ESCAPE_TARGET);

    Foo foo = new Foo();
    foo.setSomeBar(bars);
    foo.setSomeString(strings);

    byte[] protostuff = ProtostuffIOUtil.toByteArray(foo, foo.cachedSchema(), buf());

    byte[] json = JsonIOUtil.toByteArray(ProtostuffIOUtil.newPipe(protostuff),
            Foo.getPipeSchema(), false);

    byte[] json2 = JsonXIOUtil.toByteArray(ProtostuffIOUtil.newPipe(protostuff),
            Foo.getPipeSchema(), false, buf());

    assertTrue(json.length == json2.length);

    String strJson = STRING.deser(json);
    String strJson2 = STRING.deser(json2);

    assertEquals(strJson, strJson2);

    System.err.println(strJson);
    System.err.println(strJson2);
}
 
Example 13
Source File: YamlCompareOutputsTest.java    From protostuff with Apache License 2.0 5 votes vote down vote up
public void testBaz() throws Exception
{
    for (Baz bazCompare : new Baz[] { baz, negativeBaz })
    {
        byte[] yo = YAML_BUFFERED_OUTPUT.serialize(bazCompare);
        byte[] yso = YAML_STREAMED_OUTPUT.serialize(bazCompare);

        assertTrue(yo.length == yso.length);

        String yoString = STRING.deser(yo);
        String ysoString = STRING.deser(yso);

        assertEquals(yoString, ysoString);
    }
}
 
Example 14
Source File: ProtostuffNullArrayElementTest.java    From protostuff with Apache License 2.0 4 votes vote down vote up
@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 15
Source File: ProtostuffRuntimeCollectionSchemaTest.java    From protostuff with Apache License 2.0 4 votes vote down vote up
@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(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 16
Source File: XmlXRuntimeCollectionSchemaTest.java    From protostuff with Apache License 2.0 3 votes vote down vote up
@Override
protected <T> void roundTrip(T message, Schema<T> schema,
        Pipe.Schema<T> pipeSchema) throws Exception
{
    byte[] xml = XmlXIOUtil.toByteArray(message, schema, buf());

    ByteArrayInputStream xmlStream = new ByteArrayInputStream(xml);

    byte[] protostuff = ProtostuffIOUtil.toByteArray(
            XmlIOUtil.newPipe(xml, 0, xml.length), pipeSchema, buf());

    byte[] protostuffFromStream = ProtostuffIOUtil.toByteArray(
            XmlIOUtil.newPipe(xmlStream), 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[] xmlRoundTrip = XmlXIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuff, 0, protostuff.length), pipeSchema, buf());
    byte[] xmlRoundTripFromStream = XmlXIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuffStream), pipeSchema, buf());

    assertTrue(xmlRoundTrip.length == xmlRoundTripFromStream.length);

    String strXmlRoundTrip = STRING.deser(xmlRoundTrip);

    assertEquals(strXmlRoundTrip, STRING.deser(xmlRoundTripFromStream));

    assertTrue(xmlRoundTrip.length == xml.length);

    assertEquals(strXmlRoundTrip, STRING.deser(xml));
}
 
Example 17
Source File: AbstractJsonRuntimeCollectionSchemaTest.java    From protostuff with Apache License 2.0 3 votes vote down vote up
@Override
protected <T> void roundTrip(T message, Schema<T> schema,
        Pipe.Schema<T> pipeSchema) throws Exception
{
    byte[] json = JsonIOUtil.toByteArray(message, schema, isNumeric());

    ByteArrayInputStream jsonStream = new ByteArrayInputStream(json);

    byte[] protostuff = ProtostuffIOUtil.toByteArray(
            JsonIOUtil.newPipe(json, 0, json.length, isNumeric()), pipeSchema, buf());

    byte[] protostuffFromStream = ProtostuffIOUtil.toByteArray(
            JsonIOUtil.newPipe(jsonStream, isNumeric()), 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[] jsonRoundTrip = JsonIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuff, 0, protostuff.length), pipeSchema, isNumeric());

    byte[] jsonRoundTripFromStream = JsonIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuffStream), pipeSchema, isNumeric());

    assertTrue(jsonRoundTrip.length == jsonRoundTripFromStream.length);

    String strJsonRoundTrip = STRING.deser(jsonRoundTrip);

    assertEquals(strJsonRoundTrip, STRING.deser(jsonRoundTripFromStream));

    assertTrue(jsonRoundTrip.length == json.length);

    assertEquals(strJsonRoundTrip, STRING.deser(json));
}
 
Example 18
Source File: XmlRuntimeMapTest.java    From protostuff with Apache License 2.0 3 votes vote down vote up
@Override
protected <T> void roundTrip(T message, Schema<T> schema,
        Pipe.Schema<T> pipeSchema) throws Exception
{
    byte[] xml = XmlIOUtil.toByteArray(message, schema);

    ByteArrayInputStream xmlStream = new ByteArrayInputStream(xml);

    byte[] protostuff = ProtostuffIOUtil.toByteArray(
            XmlIOUtil.newPipe(xml, 0, xml.length), pipeSchema, buf());

    byte[] protostuffFromStream = ProtostuffIOUtil.toByteArray(
            XmlIOUtil.newPipe(xmlStream), 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[] xmlRoundTrip = XmlIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuff, 0, protostuff.length), pipeSchema);
    byte[] xmlRoundTripFromStream = XmlIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuffStream), pipeSchema);

    assertTrue(xmlRoundTrip.length == xmlRoundTripFromStream.length);

    String strXmlRoundTrip = STRING.deser(xmlRoundTrip);

    assertEquals(strXmlRoundTrip, STRING.deser(xmlRoundTripFromStream));

    assertTrue(xmlRoundTrip.length == xml.length);

    assertEquals(strXmlRoundTrip, STRING.deser(xml));
}
 
Example 19
Source File: XmlXRuntimeObjectSchemaTest.java    From protostuff with Apache License 2.0 3 votes vote down vote up
@Override
protected <T> void roundTrip(T message, Schema<T> schema,
        Pipe.Schema<T> pipeSchema) throws Exception
{
    byte[] xml = XmlXIOUtil.toByteArray(message, schema, buf());

    ByteArrayInputStream xmlStream = new ByteArrayInputStream(xml);

    byte[] protostuff = ProtostuffIOUtil.toByteArray(
            XmlIOUtil.newPipe(xml, 0, xml.length), pipeSchema, buf());

    byte[] protostuffFromStream = ProtostuffIOUtil.toByteArray(
            XmlIOUtil.newPipe(xmlStream), 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[] xmlRoundTrip = XmlXIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuff, 0, protostuff.length), pipeSchema, buf());
    byte[] xmlRoundTripFromStream = XmlXIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuffStream), pipeSchema, buf());

    assertTrue(xmlRoundTrip.length == xmlRoundTripFromStream.length);

    String strXmlRoundTrip = STRING.deser(xmlRoundTrip);

    assertEquals(strXmlRoundTrip, STRING.deser(xmlRoundTripFromStream));

    assertTrue(xmlRoundTrip.length == xml.length);

    assertEquals(strXmlRoundTrip, STRING.deser(xml));
}
 
Example 20
Source File: XmlRuntimeObjectSchemaTest.java    From protostuff with Apache License 2.0 3 votes vote down vote up
@Override
protected <T> void roundTrip(T message, Schema<T> schema,
        Pipe.Schema<T> pipeSchema) throws Exception
{
    byte[] xml = XmlIOUtil.toByteArray(message, schema);

    ByteArrayInputStream xmlStream = new ByteArrayInputStream(xml);

    byte[] protostuff = ProtostuffIOUtil.toByteArray(
            XmlIOUtil.newPipe(xml, 0, xml.length), pipeSchema, buf());

    byte[] protostuffFromStream = ProtostuffIOUtil.toByteArray(
            XmlIOUtil.newPipe(xmlStream), 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[] xmlRoundTrip = XmlIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuff, 0, protostuff.length), pipeSchema);
    byte[] xmlRoundTripFromStream = XmlIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuffStream), pipeSchema);

    assertTrue(xmlRoundTrip.length == xmlRoundTripFromStream.length);

    String strXmlRoundTrip = STRING.deser(xmlRoundTrip);

    assertEquals(strXmlRoundTrip, STRING.deser(xmlRoundTripFromStream));

    assertTrue(xmlRoundTrip.length == xml.length);

    assertEquals(strXmlRoundTrip, STRING.deser(xml));
}