protobuf-codec icon indicating copy to clipboard operation
protobuf-codec copied to clipboard

XML codec throw java.util.NoSuchElementException when message is too long

Open tutufool opened this issue 12 years ago • 0 comments

Hi,

I got below exception when using XML codec:

java.lang.RuntimeException: java.util.NoSuchElementException at protobuf.codec.AbstractCodec.toMessage(AbstractCodec.java:74) at protobuf.codec.AbstractCodec.toMessage(AbstractCodec.java:88) at protobuf.codec.AbstractCodec.toMessage(AbstractCodec.java:83)

... ... Caused by: java.util.NoSuchElementException at com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1114) at protobuf.codec.xml.XmlReader.parseElement(XmlReader.java:52) at protobuf.codec.xml.XmlReader.parse(XmlReader.java:41) at protobuf.codec.xml.XmlCodec.readFromStream(XmlCodec.java:56) at protobuf.codec.AbstractCodec.toMessage(AbstractCodec.java:70) ... 26 more

to reproduce:

create a simple proto:

message MyObject{ optional string name = 1; optional string value = 2; }

java code:

    try {

        MyObject before = MyObject.newBuilder()
                .setName("123456789")
                //
                .setValue(
                        "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")//
                .build();

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        Codec codec = new XmlCodec();

        codec.fromMessage(before, outputStream);
        System.out.println(outputStream.toString());

        MyObject after = codec.toMessage(MyObject.class, new ByteArrayInputStream(outputStream.toByteArray()));

        Assert.assertEquals(after, before);
        Assert.assertEquals(after.getName(), before.getName());
        Assert.assertEquals(after.getValue(), before.getValue());
    } catch (IOException e) {
        Assert.fail(e.getMessage());
    }

You can try using shorter string for setValue() and longer string for setName() to reproduce.

btw, if you change to use JsonCodec, the above case can pass.

Thanks Ma Ling

tutufool avatar Jan 18 '13 15:01 tutufool