diff --git a/src/clientP2P/ClientManagementTCP.java b/src/clientP2P/ClientManagementTCP.java index c7c16e6..b160d26 100644 --- a/src/clientP2P/ClientManagementTCP.java +++ b/src/clientP2P/ClientManagementTCP.java @@ -229,6 +229,17 @@ public class ClientManagementTCP implements Runnable { } while(!fileFullyWritten); if (!Arrays.equals(hash512, computeHashsum(filename, HashAlgorithm.SHA512))) { System.err.println("Error: Hashsum does not match"); + System.err.println("Computed checksum:"); + byte[] c = computeHashsum(filename, HashAlgorithm.SHA512); + for (byte b: c) { + System.err.print(String.format("%02X", b)); + } + System.err.println(""); + System.err.println("Received checksum:"); + for (byte b: hash512) { + System.err.print(String.format("%02X", b)); + } + System.err.println(""); throw new InternalError(); } socket.close(); diff --git a/src/clientP2P/ClientManagementUDP.java b/src/clientP2P/ClientManagementUDP.java index 4d92f84..83b3d7b 100644 --- a/src/clientP2P/ClientManagementUDP.java +++ b/src/clientP2P/ClientManagementUDP.java @@ -216,6 +216,17 @@ public class ClientManagementUDP implements Runnable { } while(!fileFullyWritten); if (!Arrays.equals(hash512, computeHashsum(filename, HashAlgorithm.SHA512))) { System.err.println("Error: Hashsum does not match"); + System.err.println("Computed checksum:"); + byte[] c = computeHashsum(filename, HashAlgorithm.SHA512); + for (byte b: c) { + System.err.print(String.format("%02X", b)); + } + System.err.println(""); + System.err.println("Received checksum:"); + for (byte b: hash512) { + System.err.print(String.format("%02X", b)); + } + System.err.println(""); throw new InternalError(); } } diff --git a/src/protocolP2P/FileList.java b/src/protocolP2P/FileList.java index bc2ca69..1c04f30 100644 --- a/src/protocolP2P/FileList.java +++ b/src/protocolP2P/FileList.java @@ -7,6 +7,7 @@ import exception.ProtocolError; import exception.InternalError; import exception.SizeError; import java.io.UnsupportedEncodingException; +import tools.BytesArrayTools; /** Representation of payload for list response. * @author Louis Royer @@ -48,11 +49,7 @@ public class FileList extends Payload { throw new InternalError(); } int size = getPayloadSize(packet); - try { - fileList = (new String(packet, PAYLOAD_START_POSITION, size, "UTF-8")).split("\n"); - } catch (UnsupportedEncodingException e) { - throw new InternalError(); - } + fileList = BytesArrayTools.readStringArray(packet, PAYLOAD_START_POSITION, size, "\n"); } /** Returns a byte[] containing Packet with padding. @@ -63,40 +60,14 @@ public class FileList extends Payload { */ protected byte[] toPacket() throws InternalError { // compute size - int size = PAYLOAD_START_POSITION; - for (String s : fileList) { - size += s.length(); - size += 1; // size for '\n' - } - size -=1; // remove trailing '\n' + int size = PAYLOAD_START_POSITION + BytesArrayTools.computeStringArraySize(fileList, "\n"); byte[] packet = new byte[size]; // java initialize all to zero // set request/response code packet[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue; // set payload size setPayloadSize(size - PAYLOAD_START_POSITION, packet); // Write fileList - int bCount = PAYLOAD_START_POSITION; - for(String s : fileList) { - if (bCount != PAYLOAD_START_POSITION) { // not on first iteration - try { - packet[bCount] = "\n".getBytes("UTF-8")[0]; // separator - } catch (UnsupportedEncodingException e) { - throw new InternalError(); - } - bCount += 1; - } - // Copy filename - try { - byte[] sb = s.getBytes("UTF-8"); - for(byte b : sb) { - packet[bCount] = b; - bCount += 1; - } - } catch (UnsupportedEncodingException e) { - throw new InternalError(); - } - - } + BytesArrayTools.write(packet, fileList, PAYLOAD_START_POSITION, "\n"); return packet; } diff --git a/src/protocolP2P/FilePart.java b/src/protocolP2P/FilePart.java index 2e2a211..df5699f 100644 --- a/src/protocolP2P/FilePart.java +++ b/src/protocolP2P/FilePart.java @@ -90,22 +90,11 @@ public class FilePart extends Payload { BytesArrayTools.write(packet, TOTAL_FILESIZE_POSITION, totalSize); // write filename’s size to Packet BytesArrayTools.write(packet, FILENAME_SIZE_POSITION, filename.length()); - try { - // write filename to Packet - int i = FILENAME_POSITION; - for (byte b : filename.getBytes("UTF-8")) { - packet[i] = b; - i += 1; - } - // write partialContent to Packet - for (byte b: partialContent) { - packet[i] = b; - i += 1; - } - return packet; - } catch (UnsupportedEncodingException e) { - throw new InternalError(); - } + // write filename to Packet + BytesArrayTools.write(packet, filename, FILENAME_POSITION); + // write partialContent to Packet + BytesArrayTools.write(packet, partialContent, FILENAME_POSITION + filename.length()); + return packet; } /** Write from Packet into offset. @@ -150,11 +139,7 @@ public class FilePart extends Payload { */ private void setFilename(byte[] packet) throws ProtocolError, SizeError, InternalError { int filenameSize = getFilenameSize(packet); // this can throw ProtocolError or SizeError - try { - filename = new String(packet, FILENAME_POSITION, filenameSize, "UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new InternalError(); - } + filename = BytesArrayTools.readString(packet, FILENAME_POSITION, filenameSize); } /** Write from Packet into partialContent. @@ -165,11 +150,7 @@ public class FilePart extends Payload { private void setPartialContent(byte[] packet) throws ProtocolError, SizeError { int start = FILENAME_POSITION + getFilenameSize(packet); // this can throw SizeError or ProtocolError int end = OFFSET_POSITION + getPayloadSize(packet); // this can throw SizeError - try { - partialContent = Arrays.copyOfRange(packet, start, end); - } catch (ArrayIndexOutOfBoundsException e) { - throw new ProtocolError(); - } + partialContent = BytesArrayTools.readByteArray(packet, start, end); } /** partialContent getter. diff --git a/src/protocolP2P/HashRequest.java b/src/protocolP2P/HashRequest.java index 8caf2d7..92ab812 100644 --- a/src/protocolP2P/HashRequest.java +++ b/src/protocolP2P/HashRequest.java @@ -60,23 +60,18 @@ public class HashRequest extends Payload { } /* Read filename */ int filenameSize = BytesArrayTools.readInt(packet, FILENAME_SIZE_POSITION); - try { - filename = new String(packet, FILENAME_POSITION, filenameSize, "UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new InternalError(); - } + /* Read filename */ + filename = BytesArrayTools.readString(packet, FILENAME_POSITION, filenameSize); + + /* Read algo list */ int size = getPayloadSize(packet); - try { - String[] l = (new String(packet, FILENAME_POSITION + filenameSize, size, "UTF-8")).split("\n"); - int i = 0; - algoList = new HashAlgorithm[l.length]; - for(String algo : l) { - algoList[i] = HashAlgorithm.fromName(algo); - i++; - } - } catch (UnsupportedEncodingException e) { - throw new InternalError(); + String[] algoListStr = BytesArrayTools.readStringArray(packet, FILENAME_POSITION + filenameSize, size, "\n"); + int i = 0; + algoList = new HashAlgorithm[algoListStr.length]; + for(String algo : algoListStr) { + algoList[i] = HashAlgorithm.fromName(algo); + i++; } } @@ -89,63 +84,24 @@ public class HashRequest extends Payload { protected byte[] toPacket() throws InternalError { // compute size int filenameSize = filename.length(); - int size = FILENAME_POSITION + filenameSize; + String[] algoListStr = new String[algoList.length]; + int i = 0; for (HashAlgorithm h : algoList) { - if (h == null) { - continue; - } - size += h.getName().length(); - size += 1; // size for '\n' + algoListStr[i] = h.getName(); + i++; } - size -=1; // remove trailing '\n' + int size = FILENAME_POSITION + filenameSize + BytesArrayTools.computeStringArraySize(algoListStr, "\n"); byte[] packet = new byte[size + 1]; // java initialize all to zero - // set request/response code packet[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue; - // set Payload size setPayloadSize(size - FILENAME_SIZE_POSITION, packet); - + // write filename size BytesArrayTools.write(packet, FILENAME_SIZE_POSITION, filenameSize); - - // Write filename - int bCount = FILENAME_POSITION; - try { - byte[] sb = filename.getBytes("UTF-8"); - for(byte b : sb) { - packet[bCount] = b; - bCount += 1; - } - } catch (UnsupportedEncodingException e) { - throw new InternalError(); - } - boolean firstIter = true; - for(HashAlgorithm h : algoList) { - if (h == null) { - continue; - } - String s = h.getName(); - if (!firstIter) { - firstIter = false; - try { - packet[bCount] = "\n".getBytes("UTF-8")[0]; // separator - } catch (UnsupportedEncodingException e) { - throw new InternalError(); - } - bCount += 1; - } - // Copy algoname - try { - byte[] sb = s.getBytes("UTF-8"); - for(byte b : sb) { - packet[bCount] = b; - bCount += 1; - } - } catch (UnsupportedEncodingException e) { - throw new InternalError(); - } - - } + // write filename + BytesArrayTools.write(packet, filename, FILENAME_POSITION); + // write algo list + BytesArrayTools.write(packet, algoListStr, FILENAME_POSITION + filename.length(), "\n"); return packet; } diff --git a/src/protocolP2P/HashResponse.java b/src/protocolP2P/HashResponse.java index 64ea71c..2ff0440 100644 --- a/src/protocolP2P/HashResponse.java +++ b/src/protocolP2P/HashResponse.java @@ -63,33 +63,22 @@ public class HashResponse extends Payload { } /* Read filename */ int filenameSize = BytesArrayTools.readInt(packet, FILENAME_SIZE_POSITION); - try { - filename = new String(packet, FILENAME_POSITION, filenameSize, "UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new InternalError(); - } + filename = BytesArrayTools.readString(packet, FILENAME_POSITION, filenameSize); int size = getPayloadSize(packet); int start = FILENAME_POSITION + filenameSize; do { int algoNameSize = BytesArrayTools.readInt(packet, start); start +=4; - try { - String algoName = new String(packet, start, algoNameSize, "UTF-8"); - start += algoNameSize; - int hashSize = BytesArrayTools.readInt(packet, start); - start += 4; - if (hashSize != 0) { - byte[] b = new byte[hashSize]; - for (int i=0;i