专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »Java » writeutf:writeUTF输出字符串失败的原因分析 »正文

writeutf:writeUTF输出字符串失败的原因分析

来源: 发布时间:星期四, 2008年9月11日 浏览:29次 评论:0
字符串比较长了之后,数据就发不过去了,经检查JDK的源代码,原来有长度限制。
为了保险起见,我们还是不要超过65535/3 我看取20000好了。


public final void writeUTF(String str) 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;
/* use charAt instead of copying String to char array */
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 new UTFDataFormatException("encoded string too long: " + utflen + " bytes");
// 其他的语句
}

相关文章

读者评论

  • 共0条 分0页

发表评论

  • 昵称:
  • 内容: