A question about ReplayingDecoder

Nicholas Clare nickclare at gmail.com
Sun Apr 5 08:02:23 EDT 2009


Hi all,

As a learning excercise, I'm writing a decoder for the STOMP messaging
protocol. After having a peek at the HTTP decoder in Netty, I was
extremely impressed by the ReplayingDecoder class. It was a more
powerful implementation of what I was planning to build myself.

Not wanting to just copy straight from the HTTP code, but rather learn
myself, I came up with the following way to read a single line of text
from the ChannelBuffer:

String line = buffer.readBytes(LF).toString("UTF-8").trim();

where LF is a static import from ChannelBufferIndexFinders. This works
beautifully, *unless* the connection is terminated half way through a
line. When that happens, a NoSuchElementException is thrown. At the
moment, I'm overriding exceptionCaught() to check for the case where
this exception is thrown, and the connection is no longer open, in
which case I ignore the exception.

So, my question is, supposing I do want to use the
readBytes(ChannelBufferIndexFinder) method, is this the best way to
get around the exception problem? Should I just put a try/catch around
the code above, and if so, what should I do when I catch that
exception? Just return null? Also, is the overhead of
readBytes(ChannelBufferIndexFinder) high, and should I rather do it
the way it's done in the HTTP code?

Thanks for all your help, and for the amazing Netty library,
Nick



More information about the netty-users mailing list