谁能弄清楚为什么我在这里得到索引越界异常?我可以读入第一批数据,但是当我尝试再次循环时,它会出错。我还在 is.read(readBytes, offset, length); 这也是一部分。
InetAddress address = packet.getAddress();
int port = packet.getPort();
RRQ RRQ = new RRQ();
int offset = 0;
int length = 512;
int block = 0;
byte[] readBytes = new byte[512];
File file = new File(filename);
FileInputStream is = new FileInputStream(file);
int fileBytes = is.read(readBytes, offset, length);
DatagramPacket outPacket = RRQ.doRRQ(readBytes, block, address, port);
socket.send(outPacket);
block++;
if (fileBytes != -1)
{
socket.receive(packet);
offset = offset + fileBytes;
System.out.println(offset);
Exceptions here:fileBytes = is.read(readBytes, offset, length);
outPacket = RRQ.doRRQ(readBytes, block, address, port);
socket.send(outPacket);
block++;
}