From e621ab159cacde6588d85be3fbe312775d5507f6 Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 5 Mar 2020 09:38:36 +0100 Subject: [PATCH 1/2] =?UTF-8?q?D=C3=A9but=20refactoring?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/protocolP2P/FileList.java | 31 +++-------------------- src/tools/BytesArrayTools.java | 45 ++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 28 deletions(-) diff --git a/src/protocolP2P/FileList.java b/src/protocolP2P/FileList.java index bc2ca69..103dec3 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 @@ -63,40 +64,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.writeStringArray(packet, fileList, PAYLOAD_START_POSITION, "\n"); return packet; } diff --git a/src/tools/BytesArrayTools.java b/src/tools/BytesArrayTools.java index 924f4fc..e09d9a8 100644 --- a/src/tools/BytesArrayTools.java +++ b/src/tools/BytesArrayTools.java @@ -1,5 +1,8 @@ package tools; import exception.SizeError; +import java.io.UnsupportedEncodingException; + + /** Helper to manipulate byte[]. * @author Louis Royer * @author Flavien Haas @@ -116,4 +119,46 @@ public class BytesArrayTools { } return new String(bArray, 0, cpt + 1); } + + /** Write a list of string to a byte array. String are separated by a separator. + * @param byteArray array to write + * @param strArray string array to read + * @param start start position to write in byteArray + * @param separator separator to use + * @throws InternalError + */ + public static void writeStringArray(byte[] byteArray, String[] strArray, int start, String separator) throws InternalError { + int bCount = start; + try { + byte[] sepb = separator.getBytes("UTF-8"); + for(String s : strArray) { + // write separator + if (bCount != start) { // not on first iteration + for (byte b : sepb) { + byteArray[bCount] = b; + bCount += 1; + } + } + // Copy string + byte[] sb = s.getBytes("UTF-8"); + for(byte b : sb) { + byteArray[bCount] = b; + bCount += 1; + } + } + } catch (UnsupportedEncodingException e) { + throw new InternalError(); + } + } + + public static int computeStringArraySize(String[] strArray, String separator) { + int size = 0; + for (String s: strArray) { + if (size != 0) { + size += separator.length(); + } + size += s.length(); + } + return size; + } } From 44b96d9f2cf8c2962113eade05d70fc625d02ff2 Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 5 Mar 2020 16:35:33 +0100 Subject: [PATCH 2/2] Refactoring --- src/clientP2P/ClientManagementTCP.java | 11 ++++ src/clientP2P/ClientManagementUDP.java | 11 ++++ src/protocolP2P/FileList.java | 8 +-- src/protocolP2P/FilePart.java | 33 +++------- src/protocolP2P/HashRequest.java | 84 ++++++------------------- src/protocolP2P/HashResponse.java | 78 +++++++---------------- src/protocolP2P/LoadRequest.java | 23 +------ src/tools/BytesArrayTools.java | 87 +++++++++++++++++++++++++- 8 files changed, 162 insertions(+), 173 deletions(-) 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 103dec3..1c04f30 100644 --- a/src/protocolP2P/FileList.java +++ b/src/protocolP2P/FileList.java @@ -49,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. @@ -71,7 +67,7 @@ public class FileList extends Payload { // set payload size setPayloadSize(size - PAYLOAD_START_POSITION, packet); // Write fileList - BytesArrayTools.writeStringArray(packet, fileList, PAYLOAD_START_POSITION, "\n"); + 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