Conversion of ChannelBuffer to InputStream
이희승 (Trustin Lee)
trustin at gmail.com
Tue Aug 25 04:05:36 EDT 2009
Hi Mike,
On Fri, 21 Aug 2009 03:10:42 +0900 (KST)
MikeQ <michaelquilleash at hotmail.com> wrote:
>
> Hi all,
>
> I am looking at using Netty for the first time. I am trying to build
> a simple server that when a message is received a method of the form
>
> handleRequest( InputStream inputStream, OutputStream )
>
> I'm not quite sure how to implement the InputStream part of this. I
> want the called code to "pull" data off the InputStream as and when
> it chooses to rather than a callback handler pushing data
> continually. (You could liken it to comparing SAX with StAX in XML
> world).
>
> I looked at the decoder/framing stuff but I don't want to buffer the
> whole request in memory first as it may be very large, want to stream
> it off the network as it arrives. Similar for the response.
>
> Is there a common/suitable method for making this conversion? It
> seems that I need something like the following.
>
> - create an InputStream backed by some custom buffer
> - on messageReceived() callback copy ChannelBuffer contents into the
> custom buffer
> - invoke the method with the InputStream
> - the first messageReceived() may not contain all the data so
> subsequent calls would need to push the new data behind the same
> InputStream to be consumed by the client if necessary
> - when the called code is finished it returns
> - if there is still data buffered then the method is invoked again
> - repeat until data buffer is empty, go back to waiting for a new
> messageReceived() call
>
> Is there anything out there that does the above? Or should I just
> write something from scratch?
You might want to use something like PipeInput/OutputStream combo and
copy the content of the received ChannelBuffer to the PipeOutputStream
so that your 'handleRequest' method reads the data from
PipeInputStream.
To write the content of a ChannelBuffer to an OutputStream, you could
call ChannelBuffer.readBytes(OutputStream, ...).
> If I need to write from scratch are there any recommendations?
>
> - Can I just copy the ChannelBuffer references on each callback and
> queue them somewhere? Or do I need to drain the contents to a
> separate buffer before messageReceived() returns?
You don't need to drain the content of the received buffer. Netty
I/O thread does not reuse nor modify the content of the buffer once it
fires a messageReceived event. Therefore, you can keep the buffer
reference as you like.
> - Do I need to use a OrderedMemoryAwareThreadPoolExecutor somewhere?
> If so where?
I'd place it before handing off the buffer to the blocking stream so
that other connections are not blocked by the blocking I/O in your code.
HTH,
Trustin
More information about the netty-users
mailing list