0

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.

Shane
  • 1,071
  • 6
  • 7

1 Answers1

0

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);
Community
  • 1
  • 1
Shane
  • 1,071
  • 6
  • 7
  • You do not need the withResponse() call, that's only useful if you want the headers, etc. – koush Aug 30 '13 at 07:25
  • Also, I think that jackson call will block on inputStream, so be careful that it is not being done on the UI thread. I'll see if I can add an asStream() method that will give you an easier way to do this. – koush Aug 30 '13 at 07:27
  • Yeah, I do want headers, mainly to check the status code. Yes, that Jackson call will block on the network, but that's what I'm looking for. I'm calling this all in a background thread already. An asStream() method might be nice, but it'd have to block, unlike all the other as*() methods, so that might be inconsistent. – Shane Sep 04 '13 at 00:40