Java的移植性
作者Brian Slesinsky
这里我们触及Java移植性方面的一个小小的缺陷。当我在Mac机上
测试该小程序时,每行之间的距离被加倍,很显然,Mac机中的
Netscape 将return和newline字符都处理为硬回车。所以我们修改
run()方法以修正这个毛病。
Chat.java (局部):
public void run() {
try {
InputStream is = s.getInputStream();
byte[] buf = new byte[200];
while(true) {
int avail = is.available();
if(avail<1) avail=1;
if(avail>buf.length) avail=buf.length;
int bytes_read = is.read(buf,0,avail);
int j = 0;
for(int i=0; i<bytes_read; i++) {
if(buf[i]!='\r') {
buf[j++] = buf[i];
}
}
ta.appendText(new String(buf, 0, 0, j));
}
} catch(Exception e) {
System.err.print(e);
}
}