Factorization of setPayloadSize and getPayloadSize

This commit is contained in:
Louis Royer 2020-01-22 13:13:41 +01:00
parent 855e4ed73e
commit 04457fdac5
3 changed files with 20 additions and 9 deletions

View File

@ -21,8 +21,7 @@ public class FileList extends Payload {
if (RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LIST_RESPONSE) {
throw new InternalError();
}
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];
int size = getPayloadSize(datagram);
}
/** To datagram with padding */
@ -34,9 +33,7 @@ public class FileList extends Payload {
// 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);
}
datagram = setPayloadSize(size, datagram);
//TODO content
}
}

View File

@ -26,7 +26,7 @@ public class FilePart extends Payload {
if (RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LOAD_RESPONSE) {
throw new InternalError();
}
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];
int size = getPayloadSize(datagram);
}
/** To datagram with padding */
@ -38,9 +38,7 @@ public class FilePart extends Payload {
// 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);
}
datagram = setPayloadSize(size, datagram);
//TODO content
}

View File

@ -43,4 +43,20 @@ public class Payload {
// bits 16-31 are reserved for future use
// payload size is 0 (this is what java have initialized datagram)
}
protected static byte[] setPayloadSize(int size, byte[] datagram) {
for(int i=0;i<4;i++) {
datagram[Payload.PAYLOAD_SIZE_POSITON + i] = (byte) (size >> (8 * (3 - i)) & 0xFF);
}
}
return datagram;
}
protected static int getPayloadSize(byte[] datagram) {
int size = 0;
for(int i=0;i<4;i++) {
size |= ((int)datagram[PAYLOAD_SIZE_POSITON + i]) << (8* i);
}
return size;
}
}