io.netty.handler.codec.xml.XmlFrameDecoder Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2013 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.handler.codec.xml;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.CorruptedFrameException;
import io.netty.handler.codec.TooLongFrameException;
import java.util.List;
/**
* 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 {@code byte} to {@code char} cast and then compares that {@code char} to a few low range
* ASCII characters like {@code '<'}, {@code '>'} or {@code '/'}. 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.
*/
public class XmlFrameDecoder extends ByteToMessageDecoder {
private final int maxFrameLength;
public XmlFrameDecoder(int maxFrameLength) {
if (maxFrameLength < 1) {
throw new IllegalArgumentException("maxFrameLength must be a positive int");
}
this.maxFrameLength = maxFrameLength;
}
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List