对read(byte[] b)和readFully(byte[] b)的理解
要搞清楚read(byte[] b)和readFully(byte[] b)的区别,我从以下方面着手分析:
1.代码的具体实现
2.方法何时返回
3.字节是以什么方式在网络上传输的
1.read(byte[] b)调用read(byte[] b,0,b.length),其中的部分关键代码如下
Java代码
//发送端: OutputStream out = ......;//通过TCP连接得到输出流对象 String content = "..."; byte[] data = content.getBytes(); output.write(data); int len = data.length; while (len++ < 30) { output.writeByte('\0');//补够30个字节 } //接收端: InputStream in = ......;//通过TCP连接得到输入流对象 byte[] bytes = new byte[30]; in.read(bytes); //发送端:OutputStream out = ......;//通过TCP连接得到输出流对象String content = "...";byte[] data = content.getBytes();output.write(data);int len = data.length;while (len++ < 30) {output.writeByte('\0');//补够30个字节}//接收端:InputStream in = ......;//通过TCP连接得到输入流对象byte[] bytes = new byte[30];in.read(bytes);