Netty's XML codec xmlframedecoder

Help me to the Internet cafe 2022-05-14 14:39:42 阅读数:609

nettyxmlcodecxmlframedecoder

brief introduction

stay json Before ,xml Is the most commonly used data transmission format , although xml There are a lot of redundant data , however xml Its structure is simple and clear , It is still used in different parts of the program , about netty Naturally, it also provides for xml Data support .

netty Yes xml Support in two ways , The first aspect is to encode multiple xml Data processing frame Split , Every frame Include a complete xml. On the other hand is to divide the good frame Conduct xml Semantic analysis of .

Conduct frame Split can be used XmlFrameDecoder, Conduct xml For the analysis of file content, you can use XmlDecoder, Next, we will explain two in detail decoder Implement and use .

XmlFrameDecoder

Because what we receive is a data stream , So I'm not sure what the data received is , A normal xml Data may be split into multiple data frame.

As shown below :

 +-------+-----+--------------+
| <this | IsA | XMLElement/> |
+-------+-----+--------------+

This is a normal xml data , But it was split into three frame, So we need to merge it into one frame as follows :

 +-----------------+
| <thisIsAXMLElement/> |
+-----------------+

There may be different xml The data is split into multiple frame Situation in , As shown below :

 +-----+-----+-----------+-----+----------------------------------+
| <an | Xml | Element/> | <ro | ot><child>content</child></root> |
+-----+-----+-----------+-----+----------------------------------+

The above data needs to be split into two frame:

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

The logic of splitting is simple , Mainly through judgment xml To determine the position of the separator xml Whether to start or end .xml There are three delimiters in , Namely '<', '>' and '/'.

stay decode Method only needs to judge these three separators .

There is also some additional judgment logic , For example, whether it is effective xml Start character :

 private static boolean isValidStartCharForXmlElement(final byte b) {
return b >= 'a' && b <= 'z' || b >= 'A' && b <= 'Z' || b == ':' || b == '_';
}

Whether it is a comment :

 private static boolean isCommentBlockStart(final ByteBuf in, final int i) {
return i < in.writerIndex() - 3
&& in.getByte(i + 2) == '-'
&& in.getByte(i + 3) == '-';
}

Whether it is CDATA data :

 private static boolean isCDATABlockStart(final ByteBuf in, final int i) {
return i < in.writerIndex() - 8
&& in.getByte(i + 2) == '['
&& in.getByte(i + 3) == 'C'
&& in.getByte(i + 4) == 'D'
&& in.getByte(i + 5) == 'A'
&& in.getByte(i + 6) == 'T'
&& in.getByte(i + 7) == 'A'
&& in.getByte(i + 8) == '[';

By using these methods to judge good xml After the starting position of the data , You can call extractFrame Method to be used ByteBuf Copy from the original data , Finally put it in out In the middle :

final ByteBuf frame =
extractFrame(in, readerIndex + leadingWhiteSpaceCount, xmlElementLength - leadingWhiteSpaceCount);
in.skipBytes(xmlElementLength);
out.add(frame);

XmlDecoder

take xml Split the data into pieces frame after , The next thing is right xml Analysis of specific data in .

netty Provides a xml The method of data analysis is called XmlDecoder, It is mainly used for a single xml Data frame Analyze the substance , It is defined as follows :

public class XmlDecoder extends ByteToMessageDecoder

XmlDecoder According to the read xml Content , take xml Split part of into XmlElementStart,XmlAttribute,XmlNamespace,XmlElementEnd,XmlProcessingInstruction,XmlCharacters,XmlComment,XmlSpace,XmlDocumentStart,XmlEntityReference,XmlDTD and XmlCdata.

These data basically cover xml All possible elements in .

All these elements are defined in io.netty.handler.codec.xml In bag .

however XmlDecoder Yes xml The reading and parsing of is borrowed from a third party xml tool kit :fasterxml.

XmlDecoder Used fasterxml Medium AsyncXMLStreamReader and AsyncByteArrayFeeder Used for xml Data analysis .

These two attributes are defined as follows :

 private static final AsyncXMLInputFactory XML_INPUT_FACTORY = new InputFactoryImpl();
private final AsyncXMLStreamReader<AsyncByteArrayFeeder> streamReader;
private final AsyncByteArrayFeeder streamFeeder;
this.streamReader = XML_INPUT_FACTORY.createAsyncForByteArray();
this.streamFeeder = (AsyncByteArrayFeeder)this.streamReader.getInputFeeder();

decode The logic is through judgment xml element To read different data respectively , Finally, the read data is encapsulated into the above-mentioned xml object , The final will be xml Object added to out list Back in .

summary

We can use XmlFrameDecoder and XmlDecoder To achieve very convenient xml Data analysis ,netty The wheels have been built for us , We don't need to invent ourselves anymore .

版权声明:本文为[Help me to the Internet cafe]所创,转载请带上原文链接,感谢。 https://javamana.com/2022/134/202205141436333757.html