FileList is done

pull/1/head
Louis Royer 5 years ago
parent 2afb87303f
commit eeb22e74cc

@ -1,10 +1,11 @@
package protocolP2P;
import java.util.ArrayList;
import java.util.Arrays;
import protocolP2P.Payload;
import protocolP2P.RequestResponseCode;
import exception.ProtocolError;
import exception.InternalError;
import exception.SizeError;
import java.io.UnsupportedEncodingException;
/** Representation of payload for list response.
* @author Louis Royer
@ -13,18 +14,18 @@ import exception.SizeError;
* @version 1.0
*/
public class FileList extends Payload {
private ArrayList<String> content;
private String[] content;
/** Constructor (typically used by the server) with an ArrayList parameter containing
* filenames.
* @param content a list of files. Must not be empty.
* @throws InternalError
*/
public FileList(ArrayList<String> content) throws InternalError {
public FileList(String[] content) throws InternalError {
super(RequestResponseCode.LIST_RESPONSE);
/* assert to help debugging */
assert !content.isEmpty() : "Payload size of FileList must not be empty, use EmptyDirectory from Payload instead";
if (content.isEmpty()) {
assert content.length != 0 : "Payload size of FileList must not be empty, use EmptyDirectory from Payload instead";
if (content.length == 0) {
throw new InternalError();
}
this.content = content;
@ -45,7 +46,11 @@ public class FileList extends Payload {
throw new InternalError();
}
int size = getPayloadSize(datagram);
//TODO
try {
content = (new String(datagram, 8, size, "UTF-8")).split("\n");
} catch (UnsupportedEncodingException e) {
throw new InternalError();
}
}
/** Returns a byte[] containing datagram with padding.
@ -54,15 +59,41 @@ public class FileList extends Payload {
* @return datagram with padding
* @throws InternalError
*/
/*protected byte[] toDatagram() throws InternalError {
//TODO compute size
int size = 8 + ;
protected byte[] toDatagram() throws InternalError {
// compute size
int size = 8;
for (String s : content) {
size += s.length();
}
byte[] datagram = new byte[size]; // java initialize all to zero
// size is keep blank (ProtocolP2PDatagram will handle it)
// set request/response code
datagram[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue;
// bits 16-31 are reserved for future use
datagram = setPayloadSize(size, datagram);
//TODO Write content
}*/
setPayloadSize(size, datagram);
// Write content
int bCount = 8;
for(String s : content) {
if (bCount != 8) { // not on first iteration
try {
datagram[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) {
datagram[bCount] = b;
bCount += 1;
}
} catch (UnsupportedEncodingException e) {
throw new InternalError();
}
}
return datagram;
}
}

Loading…
Cancel
Save