Socket InputStream 在读取块中的字节时出现故障
Socket InputStream glitches while reading bytes in chunks
我正在通过套接字连接编写简单的文件传输,但遇到了问题
在以字节块的形式下载文件时。当我一次读取一个字节时,一切正常但是当我尝试读取例如 512 字节的块时,在下载文件的所有字节后(我知道长度感谢我的 header),套接字开始收到一些随机字节并最终发生崩溃,因为找到了 2x“%”,并且服务器尝试计算传入消息的长度,当有 none 开头时。
@Override
public void run() {
while (client.isConnected() && !client.isClosed() && !client.isInputShutdown()){
try {
readResponse();
clientWriter.write("0%"); /// IF IT CATCHES EXCEPTION SOCKET CLOSED ON THE OTHER SIDE(DON'T KNOW HOW TO CHECK IT IN ANOTHER WAY)
}
catch (IOException e) {
System.out.println(e.toString());
return;
}
catch (InterruptedException e) {
System.out.println(e.toString());
return;
}
}
}
private void readResponse() throws IOException, InterruptedException {
int length = 0;
int ch;
FileOutputStream file = new FileOutputStream("holder");
boolean bWrite = false;
StringBuilder name = new StringBuilder();
int percentageCount = 0;
String filename="";
length = 0;
int l = 0;
int old = 0;
while ((ch=client.getInputStream().read())!=-1){
if (!bWrite) {
name.append(Character.toChars(ch));
}
if (ch==37 && !bWrite){
if (percentageCount<1){
percentageCount++;
} else {
filename = name.toString();
length = Integer.parseInt(filename.substring(0,filename.indexOf("%")));
l = length;
filename = filename.substring(filename.indexOf("%")+1);
filename = filename.substring(0,filename.indexOf("%"));
file = new FileOutputStream(filename);
bWrite = true;
break;
}
}
}
byte[] bytes = new byte[512];
while (length>=512){
client.getInputStream().read(bytes, 0, 512);
file.write(bytes, 0, 512);
length-=512;
}
if (length>0){
bytes = new byte[length];
client.getInputStream().read(bytes,0,length);
file.write(bytes,0,length);
length = 0;
}
file.flush();
file.close();
}
代码的最后一部分可以改成(未测试):
byte[] bytes = new byte[512];
InputStream is = client.getInputStream();
while (length>0) {
int read = is.read(bytes, 0, Math.min(bytes.length, length));
if (read > 0) {
file.write(bytes, 0, read);
length-=read;
}
if (read < 0)
throw new IOException("end reached before all data read";
}
这正确地检查了返回的大小,它避免了两个循环并检查了流的结尾。
BTW2:getInputStream()
相当重,它应该只执行一次(我的示例中的 is
应该在方法的开头检索,尤其是在读取循环之前)。
我正在通过套接字连接编写简单的文件传输,但遇到了问题 在以字节块的形式下载文件时。当我一次读取一个字节时,一切正常但是当我尝试读取例如 512 字节的块时,在下载文件的所有字节后(我知道长度感谢我的 header),套接字开始收到一些随机字节并最终发生崩溃,因为找到了 2x“%”,并且服务器尝试计算传入消息的长度,当有 none 开头时。
@Override
public void run() {
while (client.isConnected() && !client.isClosed() && !client.isInputShutdown()){
try {
readResponse();
clientWriter.write("0%"); /// IF IT CATCHES EXCEPTION SOCKET CLOSED ON THE OTHER SIDE(DON'T KNOW HOW TO CHECK IT IN ANOTHER WAY)
}
catch (IOException e) {
System.out.println(e.toString());
return;
}
catch (InterruptedException e) {
System.out.println(e.toString());
return;
}
}
}
private void readResponse() throws IOException, InterruptedException {
int length = 0;
int ch;
FileOutputStream file = new FileOutputStream("holder");
boolean bWrite = false;
StringBuilder name = new StringBuilder();
int percentageCount = 0;
String filename="";
length = 0;
int l = 0;
int old = 0;
while ((ch=client.getInputStream().read())!=-1){
if (!bWrite) {
name.append(Character.toChars(ch));
}
if (ch==37 && !bWrite){
if (percentageCount<1){
percentageCount++;
} else {
filename = name.toString();
length = Integer.parseInt(filename.substring(0,filename.indexOf("%")));
l = length;
filename = filename.substring(filename.indexOf("%")+1);
filename = filename.substring(0,filename.indexOf("%"));
file = new FileOutputStream(filename);
bWrite = true;
break;
}
}
}
byte[] bytes = new byte[512];
while (length>=512){
client.getInputStream().read(bytes, 0, 512);
file.write(bytes, 0, 512);
length-=512;
}
if (length>0){
bytes = new byte[length];
client.getInputStream().read(bytes,0,length);
file.write(bytes,0,length);
length = 0;
}
file.flush();
file.close();
}
代码的最后一部分可以改成(未测试):
byte[] bytes = new byte[512];
InputStream is = client.getInputStream();
while (length>0) {
int read = is.read(bytes, 0, Math.min(bytes.length, length));
if (read > 0) {
file.write(bytes, 0, read);
length-=read;
}
if (read < 0)
throw new IOException("end reached before all data read";
}
这正确地检查了返回的大小,它避免了两个循环并检查了流的结尾。
BTW2:getInputStream()
相当重,它应该只执行一次(我的示例中的 is
应该在方法的开头检索,尤其是在读取循环之前)。