From e621ab159cacde6588d85be3fbe312775d5507f6 Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 5 Mar 2020 09:38:36 +0100 Subject: [PATCH] =?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; + } }