您的位置:首页 > 大数据 > 人工智能

content-length bytes were read and there was no trailing null byte

2017-10-29 15:28 369 查看
主要的就是编码问题,不支持中文,当我们发送中文的时候会报错。

为了解决这一问题,我们改动了源码,因为他把字符都存在了byte数组中,英文的话一个英文占一个字节,但是中文的话一个汉字需要占用两个字节,对于中文来说长度不够,所以会报错。

最后我们改动了activemq-stomp-5.12.1.jar里面的StompWireformat文件,改动了unmarshal方法,

原来的判断:

 if (((action.equals("SEND")) || (action.equals("MESSAGE"))) && (contentLength != null))

      {

        int length = parseContentLength(contentLength, this.frameSize);

 

        data = new byte[length];

        in.readFully(data);

 

        if (in.readByte() != 0) {

          throw new ProtocolException("content-length bytes were read and there was no trailing null byte", true);

        }

 

      }

      else

      {

        ByteArrayOutputStream baos = null;

        while ((b = in.readByte()) != 0) {

          if (baos == null) {

            baos = new ByteArrayOutputStream(); } else {

            if (baos.size() > getMaxDataLength()) {

              throw new ProtocolException("The maximum data length was exceeded", true);

            }

            if (this.frameSize.incrementAndGet() > getMaxFrameSize()) {

              throw new ProtocolException("The maximum frame size was exceeded", true);

            }

          }

 

          baos.write(b);

        }

 

现在我们将判断去掉之后就可以发送中文了。

try {

            // parse action

            String action = parseAction(in, frameSize);

            // Parse the headers

            HashMap<String, String> headers = parseHeaders(in, frameSize);

 

            // Read in the data part.

            byte[] data = NO_DATA;

            //String contentLength = headers.get(Stomp.Headers.CONTENT_LENGTH);

            byte b;

            ByteArrayOutputStream baos = null;

            while ((b = in.readByte()) != 0) {

                if (baos == null) {

                    baos = new ByteArrayOutputStream();

                } else if (baos.size() > getMaxDataLength()) {

                    throw new ProtocolException("The maximum data length was exceeded", true);

                } else {

                    if (frameSize.incrementAndGet() > getMaxFrameSize()) {

                        throw new ProtocolException("The maximum frame size was exceeded", true);

                    }

                }

 

                baos.write(b);

            }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐