Question

The below code is designed to take a byte[] and several other things (see code it gives it away). It then builds a packet with a certain length and returns this. My problem is when I read in a file to the main program I need the buffer for the datagram packet to be the number of bytes left to sent (if less than 512) + 4. This is to handle the last packet of a read request of a TFTP server. Im getting an index out of bounds exception on the line "buffer[bufferOffset] = data[dataOffset];". Can anyone help me understand why this is happening and what would be the best way around it?

public DatagramPacket doRRQ(byte[] data, int block, InetAddress address, int port, long fileSize, long sentBytes){

//takes file data and creates a buffer for a DATA packet to be used in a RRQ. This packet 
//is passed to MyFirstTFTPServer.java to be modified and sent to client. This is done 
int bufferOffset = 3;
int dataOffset = -1;
byte[] buffer = null;
long difference = 0;
int i = 512;
byte byteBlock = (byte)block++;//possible issue with block no increment. test.

if (sentBytes + 512 > fileSize){

difference = fileSize - sentBytes;
    difference = difference + 4;
    i = (int)difference;
    buffer = new byte[i];

} else {

    buffer = new byte[516];

}

DatagramPacket packet = new DatagramPacket(buffer, buffer.length, address, port);
buffer[0] = 0;
buffer[1] = 3;
buffer[2] = 0;
buffer[3] = byteBlock;
for(byte item:data){

    bufferOffset++;
    dataOffset++;
    buffer[bufferOffset] = data[dataOffset];

    //System.out.println(bufferOffset);

    //System.out.println(dataOffset);

}

return packet;

}
Was it helpful?

Solution

I don't see where you ever take a look at data.length. Is it possible that data.length is greater than sentBytes? If so, your loop should be over sentBytes, not over data.

Basically, your logic to decide the buffer length of buffer seems suspiciously divorced from the size of sentBytes.

OTHER TIPS

This is your code refactored to a compact form:

public DatagramPacket doRRQ(byte[] data, int block, InetAddress address,
    int port, long fileSize, long sentBytes)
{
  final byte[] buffer =
    new byte[sentBytes + 512 > fileSize? (int)(fileSize - sentBytes) + 4 : 516];
  final DatagramPacket packet = 
    new DatagramPacket(buffer, buffer.length, address, port);
  buffer[1] = 3;
  buffer[3] = (byte)block;
  for (int i = 0; i < data.length; i++) buffer[i+3] = data[i];
  return packet;
}

Now it's quite obvious what's going on. Your expression for buffer size doesn't depend directly on data.length, so the problem can lie in the values of the arguments to the method, just as it can lie in the code itself. You be the judge.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top