Is there any restriction on the minimum size of the data that should be present in ByteBuffer so that Jackson will be able to serialize it ? I get BufferUnderflowException while doing so. But it works fine when the size of data present is large.
public class MyTest {
private static class Wrapper {
private ByteBuffer buffer;
public void setBuffer(ByteBuffer buffer) {
this.buffer = buffer;
}
public ByteBuffer getBuffer() {
return buffer;
}
}
@Test
public void fails() throws Exception {
// Fails
ByteBuffer smallBuffer = ByteBuffer.wrap("small".getBytes());
Wrapper wrapper1 = new Wrapper();
wrapper1.setBuffer(smallBuffer);
System.out.println(new ObjectMapper().writeValueAsBytes(wrapper1));
}
@Test
public void works() throws Exception {
// Works
ByteBuffer smallBuffer = ByteBuffer.wrap("larger string works, wonder why".getBytes());
Wrapper wrapper1 = new Wrapper();
wrapper1.setBuffer(smallBuffer);
System.out.println(new ObjectMapper().writeValueAsBytes(wrapper1));
}
}
Exception stack trace:
org.codehaus.jackson.map.JsonMappingException: (was java.nio.BufferUnderflowException) (through reference chain: com.test.Wrapper["buffer"]->java.nio.HeapByteBuffer["int"])
at org.codehaus.jackson.map.JsonMappingException.wrapWithPath(JsonMappingException.java:218)
at org.codehaus.jackson.map.JsonMappingException.wrapWithPath(JsonMappingException.java:183)
at org.codehaus.jackson.map.ser.std.SerializerBase.wrapAndThrow(SerializerBase.java:140)
at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:158)