Domanda

I'm not understanding how the write(outputStream) method is supposed to be used in Ion. My goal is to get an InputStream that I can feed directly into Jackson like so:

Response<OutputStream> response = Ion.with(context, "http://example.com/mydata.json").write(outputStream).withResponse().get();
MyModel m = jacksonJsonMapper.convertValue(inputStream, MyModel.class);

But I'm lost as to where to get the input and output streams, and how to connect them to each other.

È stato utile?

Soluzione

Ok, figured out a solution based on this answer: Most efficient way to create InputStream from OutputStream

Looks like I want to use a PipedOutputStream and PipedInputStream. Because we're now blocking on reading from the InputStream, I no longer want to block on the Ion call, so I removed the .get(). The final code looks something like:

PipedInputStream inputStream = new PipedInputStream();
PipedOutputStream outputStream = new PipedOutputStream(inputStream);
Ion.with(context, "http://example.com/mydata.json").write(outputStream).withResponse();
MyModel m = jacksonJsonMapper.convertValue(inputStream, MyModel.class);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top