程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> writeUTF輸出字符串失敗的原因分析

writeUTF輸出字符串失敗的原因分析

編輯:關於JAVA

字符串比較長了之後,數據就發不過去了,經檢查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");
  // 其他的語句
 }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved