0

I'm trying to decode message pack payload. They payload is written in go with the following code

var w bytes.Buffer

testBatch := []Event{
    exampleEvent,
    exampleEvent,
}

for _, e := range testBatch {
    data, err := e.MarshalMsg(nil)
    if err != nil {
        t.Fatalf("MarshalMsg failed: %v", err)
    }
    if _, err := w.Write(data); err != nil {
        t.Fatalf("Write: %v", err)
    }
}

// w.Bytes() now contain bytes that java will read.

If I understand correctly, the payload now contains 2 msgpack encoded stack which is appended together.

for decoding in java, I'm using the following libaray

https://github.com/msgpack/msgpack-java

that project contain msgpack-jackson that handle decoding message to POJO

I manage to decode the first message in java using the following code:

ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
Event events1 = objectMapper.readValue(in, Event.class);

// Event events2 = objectMapper.readValue(in, Event.class); // this doesn't work

events1 contain the first struct which is decoded correctly, but I did not manage to get the second struct.

un-commenting the events1 line give the following error

com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
 at [Source: java.io.ByteArrayInputStream@2374d36a; line: -1, column: 0]

My objective is to get all the struct in the payload decoded correctly in java. I could find example.

ahmy
  • 4,095
  • 4
  • 27
  • 36

1 Answers1

0

Well, I'm assuming your in variable is an InputStream:

InputStream in = [..]// get the input stream somehow
Event events1 = objectMapper.readValue(in, Event.class);
Event events2 = objectMapper.readValue(in, Event.class);

When you read bytes from an InputStream, the position of the stream moves ahead. Each read() reads just the next byte.

After the first objectValue.readValue() the stream's position is at the end of the data. That's why you get "end-of-input".

You just need to reposition the InputStream (if it is supported). (Or create a new InputStream, if that is what you want.)

For repositioning the InputStream, read about mark and reset: https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html

Matruskan
  • 325
  • 3
  • 11