|
|
|
@ -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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|