2

I am using a messaging service that returns me a ByteBuffer which contains some XML that I want to use JAXB to deserialize.

Is there a direct way (using JAXBs Unmarshaller) to deserialize the ByteBuffer or is converting this to a string and then deserializing that the only way?

Cheetah
  • 13,785
  • 31
  • 106
  • 190
  • 1
    You may find the following useful: http://stackoverflow.com/questions/4332264/wrapping-a-bytebuffer-with-an-inputstream – bdoughan Jan 06 '15 at 16:21

1 Answers1

1

It should be fairly trivial to wrap a ByteBuffer in an InputStream which JAXB should be able to accept as input:

public class ByteBufferInputStream extends InputStream {
    private ByteBuffer byteBuffer;

    public ByteBufferInputStream(ByteBuffer byteBuffer) {
        this.byteBuffer = byteBuffer;
    }


    @Override
    public int read() throws IOException {
        if(byteBuffer.hasRemaining()) {
            return byteBuffer.get();
        } else {
            return -1;
        }
    }
} 
BarrySW19
  • 3,759
  • 12
  • 26