简体   繁体   中英

Using Ion write(outputStream)

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM