You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by tayeb <ta...@dr.dk> on 2017/05/01 15:57:14 UTC

Netty Charset (UTF16) - XmlFrameDecoder

Hi,

We have a legacy system we are integrating with through a socket that
produces/consumes XML in UTF16.
We use the netty4 component and XmlFrameDecoder to read the messages, but it
expects UTF8 as default.

I could not find a default charset converter/decoder, so I did it by
creating my own "CharsetAwareXmlFrameDecoder" that extends the
XmlFrameDecoder as shown bellow. 

It works, but it feels wrong. Is this really the way to go?! Is there no
default implementation to do the charset conversion?

Thanks in advance for the help!

ps -->

    private static class CharsetAwareXmlFrameDecoder extends XmlFrameDecoder
{
        Charset charset;

        public CharsetAwareXmlFrameDecoder(int maxFrameLength) {
            super(maxFrameLength);
        }

        public CharsetAwareXmlFrameDecoder(int maxFrameLength, Charset
charset) {
            super(maxFrameLength);
            this.charset = charset;
        }

        private String xml;

        @Override
        protected void decode(ChannelHandlerContext ctx, ByteBuf in, List
out) throws Exception {
            List tempOut = new ArrayList();
            String charsetString = in.toString(CharsetUtil.UTF_16);
            in.skipBytes(in.readableBytes());// CONSUME MESSAGE
            charsetString = (xml != null ? xml : "") + charsetString;
            ByteBuf newIn = ByteBufUtil.encodeString(ctx.alloc(),
CharBuffer.wrap(charsetString), CharsetUtil.UTF_8);
            super.decode(ctx, newIn, tempOut);
            // check if reached end of full xml message
            if(tempOut.size() > 0) {
                out.addAll(tempOut);
                xml = null;
            } else {//.. or accumulate
                xml = charsetString;
            }
        }
    }



--
View this message in context: http://camel.465427.n5.nabble.com/Netty-Charset-UTF16-XmlFrameDecoder-tp5798548.html
Sent from the Camel - Users mailing list archive at Nabble.com.