@ -13,8 +13,8 @@ import exception.InternalError;
public class FilePart extends Payload {
private static final RequestResponseCode requestResponseCode = RequestResponseCode . LOAD_RESPONSE ;
private String filename ;
private int totalSize ;
private int offset ;
private long totalSize ;
private long offset ;
private byte [ ] partialContent ;
/ * * Constructor ( typically used by server ) with informations about file part to send as parameters .
@ -22,8 +22,15 @@ public class FilePart extends Payload {
* @param totalSize total size of the file to send
* @param offset where in the file begins the part we are sending
* @param partialContent content of the file we send
* @throws InternalError
* /
public FilePart ( String filename , int totalSize , int offset , byte [ ] partialContent ) {
public FilePart ( String filename , long totalSize , long offset , byte [ ] partialContent ) throws InternalError {
/* asserts to help debugging */
assert totalSize > = 0 : "totalSize cannot be negative" ;
assert offset > = 0 : "offset cannot be negative" ;
if ( totalSize < 0 | | offset < 0 ) {
throw new InternalError ( ) ;
}
this . filename = filename ;
this . totalSize = totalSize ;
this . offset = offset ;
@ -36,14 +43,17 @@ public class FilePart extends Payload {
* @throws InternalError
* /
public FilePart ( byte [ ] datagram ) throws ProtocolError , InternalError {
//TODO
/* assert to help debugging */
assert RequestResponseCode . fromCode ( datagram [ RequestResponseCode . RRCODE_POSITION ] ) = = RequestResponseCode . LOAD_RESPONSE : "FilePart subclass is incompatible with this datagram, request/response code must be checked before using this constructor" ;
/* InternalErrorException */
if ( RequestResponseCode . fromCode ( datagram [ RequestResponseCode . RRCODE_POSITION ] ) ! = RequestResponseCode . LOAD_RESPONSE ) {
throw new InternalError ( ) ;
}
int size = getPayloadSize ( datagram ) ;
setOffset ( datagram ) ; // this can throw ProtocolError
setTotalFileSize ( datagram ) ; // this can throw ProtocolError
int size = getPayloadSize ( datagram ) ; // this can throw ProtocolError
int partialContentStart = setFilename ( datagram , size ) + 1 ; // this can throw ProtocolError
setPartialContent ( datagram , partialContentStart , size ) ;
}
/ * * Returns a byte [ ] containing datagram with padding .
@ -52,15 +62,39 @@ public class FilePart extends Payload {
* @return datagram with padding
* /
public byte [ ] toDatagram ( ) {
byte [ ] datagram ; // java initialize all to zero
//TODO : calculate payload size
int size = ;
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
//TODO size
int size = ;
datagram = setPayloadSize ( size , datagram ) ;
//TODO content
//TODO : write offset to datagram
//TODO : write totalFileSize to datagram
//TODO : write filename\n to datagram
//TODO : write partialContent to datagram
}
private setOffset ( byte [ ] datagram ) throws ProtocolError {
//TODO: copy from 8 to 15 into long and write into offset.
// throw protocolerror if offset is < 0
}
private setTotalFileSize ( byte [ ] datagram ) throw ProtocolError {
// TODO: convert from byte 16 to 23 (included)
// into long and write into totalFileSize
// throw protocolerror if offset is < 0
}
private int setFilename ( byte [ ] datagram , size ) throws ProtocolError {
// TODO: copy datagram from byte 24 to the first \n (excluded)
// into filename unless we excess size
// (in this case the wrong size has been
// set by the emitter, it is a protocolerror)
// return position of the \n
}
private setPartialContent ( byte [ ] datagram , int start , size ) {
// TODO: copy datagram from start to size into partialContent
}
}