diff --git a/src/clientP2P/ClientManagementUDP.java b/src/clientP2P/ClientManagementUDP.java index 3b8a811..445bbea 100644 --- a/src/clientP2P/ClientManagementUDP.java +++ b/src/clientP2P/ClientManagementUDP.java @@ -125,6 +125,7 @@ public class ClientManagementUDP implements Runnable { throw new ProtocolError(); } if (fp.getOffset() != 0 || fp.getPartialContent().length != fp.getTotalSize()) { + System.err.println("offset: " + fp.getOffset() + " ; content.length: " + fp.getPartialContent().length + " ; totalSize: " + fp.getTotalSize()); System.err.println("Error: cannot handle partial files (not implemented)"); throw new InternalError(); } diff --git a/src/protocolP2P/FilePart.java b/src/protocolP2P/FilePart.java index 32deb5f..b2d539a 100644 --- a/src/protocolP2P/FilePart.java +++ b/src/protocolP2P/FilePart.java @@ -163,7 +163,7 @@ public class FilePart extends Payload { int start = 28 + getFilenameSize(datagram); // this can throw SizeError or ProtocolError int end = 8 + getPayloadSize(datagram); // this can throw SizeError try { - partialContent = Arrays.copyOfRange(datagram, start, end+1); + partialContent = Arrays.copyOfRange(datagram, start, end); } catch (ArrayIndexOutOfBoundsException e) { throw new ProtocolError(); } diff --git a/src/protocolP2P/ProtocolP2PDatagram.java b/src/protocolP2P/ProtocolP2PDatagram.java index caa8874..744c5e8 100644 --- a/src/protocolP2P/ProtocolP2PDatagram.java +++ b/src/protocolP2P/ProtocolP2PDatagram.java @@ -78,6 +78,13 @@ public class ProtocolP2PDatagram { socket.send(datagramPacket); } + /** Send a response. + * @param socket DatagramSocket used to send response + * @param host host to send response + * @param port port to send response + * @throws InternalError + * @throws IOException + */ protected void sendResponse(DatagramSocket socket, InetAddress host, int port) throws InternalError, IOException { assert port != 0 && host != null : "This method should be used only as response to a request"; if (port == 0 || host == null) { diff --git a/src/tools/BytesArrayTools.java b/src/tools/BytesArrayTools.java index 9547f64..3813349 100644 --- a/src/tools/BytesArrayTools.java +++ b/src/tools/BytesArrayTools.java @@ -23,8 +23,8 @@ public class BytesArrayTools { * @param value long to write */ public static void write(byte[] array, int start, long value) { - for(int i=0;i<4;i++) { - array[start + i] = (byte) ((value >> (8 * (4 - i))) & 0xFF); + for(int i=0;i<8;i++) { + array[start + i] = (byte) ((value >> (8 * (7 - i))) & 0xFF); } } @@ -36,7 +36,7 @@ public class BytesArrayTools { public static int readInt(byte[] array, int start) throws SizeError { int size = 0; for(int i=0;i<4;i++) { - size |= ((int)array[start + i]) << (8* i); + size |= ((int)array[start + i]) << (8* (3 -i)); } if (size < 0) { // Size in array is probably correct @@ -53,7 +53,7 @@ public class BytesArrayTools { public static long readLong(byte[] array, int start) throws SizeError { long size = 0; for(int i=0;i<8;i++) { - size |= ((int)array[start + i]) << (8* i); + size |= ((int)array[start + i]) << (8* (7 - i)); } if (size < 0) { // Size in array is probably correct