Class XmlFrameDecoder

All Implemented Interfaces:
ChannelHandler, ChannelInboundHandler

public class XmlFrameDecoder extends ByteToMessageDecoder
A frame decoder for single separate XML based message streams.

A couple examples will better help illustrate what this decoder actually does.

Given an input array of bytes split over 3 frames like this:

+-----+-----+-----------+
| <an | Xml | Element/> |
+-----+-----+-----------+

this decoder would output a single frame:

+-----------------+
| <anXmlElement/> |
+-----------------+
Given an input array of bytes split over 5 frames like this:
+-----+-----+-----------+-----+----------------------------------+
| <an | Xml | Element/> | <ro | ot><child>content</child></root> |
+-----+-----+-----------+-----+----------------------------------+

this decoder would output two frames:

+-----------------+-------------------------------------+
| <anXmlElement/> | <root><child>content</child></root> |
+-----------------+-------------------------------------+

The byte stream is expected to be in UTF-8 character encoding or ASCII. The current implementation uses direct byte to char cast and then compares that char to a few low range ASCII characters like '<', '>' or '/'. UTF-8 is not using low range [0..0x7F] byte values for multibyte codepoint representations therefore fully supported by this implementation.

Please note that this decoder is not suitable for xml streaming protocols such as XMPP, where an initial xml element opens the stream and only gets closed at the end of the session, although this class could probably allow for such type of message flow with minor modifications.

  • Field Details

    • maxFrameLength

      private final int maxFrameLength
    • openingBracketFound

      private boolean openingBracketFound
    • atLeastOneXmlElementFound

      private boolean atLeastOneXmlElementFound
    • inCDATASection

      private boolean inCDATASection
    • inCommentBlock

      private boolean inCommentBlock
    • inProcessingInstruction

      private boolean inProcessingInstruction
    • inClosingTag

      private boolean inClosingTag
    • openBracketsCount

      private long openBracketsCount
    • length

      private int length
      Index, relative to the current reader index, of the byte right after the last '>' seen so far. Netty's cumulation buffer may be compacted (rebasing both the reader index and the bytes) in between two decode(ChannelHandlerContext, ByteBuf, List) invocations, which would silently invalidate an absolute buffer index stored across calls; storing this relative to the reader index keeps it valid across such compactions, just like scanOffset.
    • leadingWhiteSpaceCount

      private int leadingWhiteSpaceCount
    • scanOffset

      private int scanOffset
      Index, relative to the current reader index, of the next byte that still needs to be scanned. This lets decode(ChannelHandlerContext, ByteBuf, List) resume scanning where the previous invocation left off instead of rescanning the whole accumulated buffer every time, which would otherwise make a slowly trickled, never-balanced element run in quadratic time.
    • CDATA_START_SUFFIX

      private static final byte[] CDATA_START_SUFFIX
  • Constructor Details

    • XmlFrameDecoder

      public XmlFrameDecoder(int maxFrameLength)
  • Method Details

    • decode

      protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception
      Description copied from class: ByteToMessageDecoder
      Decode the from one ByteBuf to an other. This method will be called till either the input ByteBuf has nothing to read when return from this method or till nothing was read from the input ByteBuf.
      Specified by:
      decode in class ByteToMessageDecoder
      Parameters:
      ctx - the ChannelHandlerContext which this ByteToMessageDecoder belongs to
      in - the ByteBuf from which to read data
      out - the List to which decoded messages should be added
      Throws:
      Exception - is thrown if an error occurs
    • resetState

      private void resetState()
    • fail

      private void fail(long frameLength)
    • fail

      private static void fail(ChannelHandlerContext ctx)
    • extractFrame

      private static ByteBuf extractFrame(ByteBuf buffer, int index, int length)
    • isValidStartCharForXmlElement

      private static boolean isValidStartCharForXmlElement(byte b)
      Asks whether the given byte is a valid start char for an xml element name.

      Please refer to the NameStartChar formal definition in the W3C XML spec for further info.

      Parameters:
      b - the input char
      Returns:
      true if the char is a valid start char
    • classifyBangMarkup

      private static XmlFrameDecoder.BangMarkupKind classifyBangMarkup(ByteBuf in, int i, int bufferLength)
      Classifies the markup declaration starting at in.getByte(i) == '<' followed by '!', matching it incrementally against the <!-- and <![CDATA[ prefixes so a genuinely different declaration (e.g. <!DOCTYPE ...> or an unknown <!x>) is recognised as such immediately, without waiting for bytes that would only be needed to confirm a comment or CDATA start.