这里显示了两个代码示例,说明如何在 inputStream 中读取套接字连接,其中哪个最好,为什么?
代码示例 1
while(totalBytesRead < fileSizeFromClient){
int bytesRemaining = fileSizeFromClient - totalBytesRead;
int bytesRead = dataInputStream.read(buffer, 0, (int)Math.min(buffer.length, bytesRemaining));
if(bytesRead == -1){
break;
}else{
dataOutputSream.write(buffer, 0, bytesRead);
totalBytesRead += bytesRead;
}
}
代码示例 2
while(totalBytesRead < fileSizeFromClient){
int bytesRemaining = fileSizeFromClient - totalBytesRead;
int bytesRead = dataInputStream.read(buffer, totalBytesRead, bytesRemaining);
if(bytesRead == -1){
break;
}else{
dataOutputStream.write(buffer, totalBytesRead, bytesRead);
totalBytesRead += bytesRead;
}
}