Wrapping an InputStream into a ReadStream<Buffer>? - AsyncInputStream

1,331 views
Skip to first unread message

Stephan Wissel

unread,
Apr 24, 2016, 8:21:15 AM4/24/16
to vert.x
I have a backend system that delivers large amount of data in an InputStream. To deliver that data back to a calling client I'd like to use ReadStream/WriteStream and the Pump.pump(in,out).

So I tried to wrap an InputStream into a class I called AsyncInputStream that implements ReadStream<Buffer>. I had a look at the AsyncFile implementation for inspiration. However java.nio.Channels.createChannel(InputStream) doesn't provide a Channel with a completion Callback, so my code fails.

The sample class is here:

When I run an example I get an Exception:

Apr 24, 2016 8:14:04 PM demo.AsyncInputStream

SEVERE: java.nio.channels.ClosedChannelException

Apr 24, 2016 8:14:04 PM demo.AsyncInputStream

SEVERE: Unhandled exception

java.nio.channels.ClosedChannelException


I wonder what I missed? Or is there an AsyncInputStream implementation I can reuse?

Stephan Wissel

unread,
Apr 24, 2016, 8:33:05 AM4/24/16
to vert.x
I check this:
and SO says: not possible, that means: need to implement myself

Stephan Wissel

unread,
Apr 24, 2016, 10:38:10 AM4/24/16
to vert.x
I tried a few more things and got to this version:

Is that the right approach? How can it be improved?


On Sunday, 24 April 2016 20:21:15 UTC+8, Stephan Wissel wrote:

Clement Escoffier

unread,
Apr 24, 2016, 11:11:17 AM4/24/16
to ve...@googlegroups.com
-- 
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/743e4d65-8d3f-46c6-b08f-759832033bbd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stephan Wissel

unread,
Apr 24, 2016, 5:26:13 PM4/24/16
to ve...@googlegroups.com
Cool. I'll give it a try. In case I decide on my own implementation, what package should I pick to make the pull request easy. After all wrapping an InputStream into an AsyncInputstream feels like something more people can use
You received this message because you are subscribed to a topic in the Google Groups "vert.x" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vertx/XwgHFfC4vKI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vertx+un...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/638B8EEA-3881-463F-8330-469669B261E9%40gmail.com.

For more options, visit https://groups.google.com/d/optout.


--

Create a nice day!
Stephan H. Wissel
Blog Twitter Google+ Facebook LinkedIn Xing Sametime Skype

Stephan Wissel

unread,
Apr 29, 2016, 4:21:19 PM4/29/16
to vert.x
So I implemented my version of an AsyncInputStream here:

https://gist.github.com/Stwissel/161c38dd6cc99a92e6194ec77a449b10

The "core" of the function starts at line 158 with the doRead() function.

Is that approach correct or can it be optimized? If it is nice, I wouldn't mind contributing it back to Vert.x.

:-) stw

Mihai Cazacu

unread,
Apr 17, 2018, 5:59:25 PM4/17/18
to vert.x
Hi,

There was another request for this and some other people needed this adaptor. Can it be provided by Vertx?

Thanks!

Julien Viet

unread,
Apr 20, 2018, 2:08:07 PM4/20/18
to ve...@googlegroups.com
it could be provided in an utils class, we've thoughts of doing such separate utils project.

-- 
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
Reply all
Reply to author
Forward
0 new messages