End of protocol implementation

pull/1/head
Louis Royer 5 years ago
parent 462b376eee
commit 4fec744616

@ -73,8 +73,8 @@ public class FilePart extends Payload {
* @throws InternalError
*/
protected byte[] toDatagram() throws InternalError {
//TODO : calculate payload size
int size = 24 + filename.length() + 1;
// compute payload size
int size = 28 + filename.length() + partialContent.length;
byte[] datagram = new byte[size]; // java initialize all to zero
// size is keep blank (ProtocolP2PDatagram will handle it)
// set request/response code
@ -87,14 +87,22 @@ public class FilePart extends Payload {
BytesArrayTools.write(datagram, 16, totalSize);
// write filenames size to datagram
BytesArrayTools.write(datagram, 24, filename.length());
//TODO : write filename to datagram
// write filename to datagram
try {
byte[] bFilename = filename.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new InternalError();
}
//TODO : write partialContent to datagram
int i = filename.length() + 24;
for (byte b : bFilename) {
datagram[i] = b;
i += 1;
}
// write partialContent to datagram
for (byte b: partialContent) {
datagram[i] = b;
i += 1;
}
return datagram;
}
@ -113,7 +121,7 @@ public class FilePart extends Payload {
totalSize = BytesArrayTools.readLong(datagram, 16);
}
/** Read filenames size from bytes 24 to 28 of datagram.
/** Read filenames size from bytes 24 to 27 of datagram.
* @param datagram received datagram
* @throws ProtocolError
* @throws SizeError
@ -132,7 +140,7 @@ public class FilePart extends Payload {
return size;
}
/** Write filename from byte 24 to byte (24 + (filenameSize - 1)) of datagram.
/** Write filename from byte 28 to byte (28 + (filenameSize - 1)) of datagram.
* @param datagram received datagram
* @throws ProtocolError
* @throws SizeError
@ -141,19 +149,19 @@ public class FilePart extends Payload {
private void setFilename(byte[] datagram) throws ProtocolError, SizeError, InternalError {
int filenameSize = getFilenameSize(datagram); // this can throw ProtocolError or SizeError
try {
filename = new String(datagram, 24, filenameSize, "UTF-8");
filename = new String(datagram, 28, filenameSize, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new InternalError();
}
}
/** Write partialContent from byte (24 + filenameSize) to byte (8 + payloadSize) of datagram.
/** Write partialContent from byte (28 + filenameSize) to byte (8 + payloadSize) of datagram.
* @param datagram received datagram
* @throws SizeError
* @throws ProtocolError
*/
private void setPartialContent(byte[] datagram) throws ProtocolError, SizeError {
int start = 24 + getFilenameSize(datagram); // this can throw SizeError or ProtocolError
int start = 28 + getFilenameSize(datagram); // this can throw SizeError or ProtocolError
int end = 8 + getPayloadSize(datagram); // this can throw SizeError
try {
partialContent = Arrays.copyOfRange(datagram, start, end+1);

Loading…
Cancel
Save