关于一些输出流中writeUTF()无法输…

来源:互联网 发布:手机求导软件 编辑:程序博客网 时间:2024/05/21 09:33
字符串比较长了之后,数据就发不过去了,经检查JDK的源代码,原来有长度限制。

为了保险起见,我们还是不要超过65535/3 。

 

public final void writeUTF(Stringstr) throws IOException {

    writeUTF(str, this);

  }

 static int writeUTF(String str,DataOutput out) throws IOException {

  int strlen =str.length();

  int utflen = 0;

  int c, count = 0;

  

  for (int i = 0; i< strlen; i ) {

   c = str.charAt(i);

   if ((c >= 0x0001)&& (c <= 0x007F)){

    utflen ;

   } else if (c >0x07FF) {

    utflen = 3;

   } else {

    utflen = 2;

   }

  }

  if (utflen >65535)

   throw newUTFDataFormatException("encoded string too long: " utflen "bytes");

  // 其他的语句

 }




 
原创粉丝点击