package protocolP2P; import protocolP2P.Payload; import protocolP2P.RequestResponseCode; import exception.ProtocolError; public class FilePart extends Payload { private static final RequestResponseCode requestResponseCode = RequestResponseCode.LOAD_RESPONSE; private String filename; private int totalSize; private int offset; private byte[] partialContent; public FilePart(String filename, int totalSize, int offset, byte[] partialContent) { this.filename = filename; this.totalSize = totalSize; this.offset = offset; this.partialContent = partialContent; } public FilePart(byte[] datagram) throws ProtocolError { //TODO 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"; int size = (datagram[PAYLOAD_SIZE_POSITON] << (8*3)) | (datagram[PAYLOAD_SIZE_POSITON+1] << (8*2)) | (datagram[PAYLOAD_SIZE_POSITON+2] << 8) | datagram[PAYLOAD_SIZE_POSITON+3]; } /** To datagram with padding */ public byte[] toDatagram() { byte[] datagram; // 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 = ; for(i=0;i<4;i++) { datagram[Payload.PAYLOAD_SIZE_POSITON + i] = (byte) (size >> (8 * (3 - i)) & 0xFF); } //TODO content } }