Début refactoring
This commit is contained in:
parent
7156f6df98
commit
e621ab159c
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user