Tests and bugfixes for XOR encoder and decoder.

This commit is contained in:
akwizgran
2012-01-20 19:09:27 +00:00
parent 74dd4e277f
commit 114ca203d8
6 changed files with 112 additions and 21 deletions

View File

@@ -4,10 +4,18 @@ import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
class Frame {
private final byte[] buf = new byte[MAX_FRAME_LENGTH];
private final byte[] buf;
private int length = -1;
Frame() {
this(MAX_FRAME_LENGTH);
}
Frame(int length) {
buf = new byte[length];
}
public byte[] getBuffer() {
return buf;
}

View File

@@ -36,27 +36,31 @@ class XorErasureDecoder implements ErasureDecoder {
// We don't need no stinkin' parity segment
for(int i = 0; i < n - 1; i++) {
byte[] src = set[i].getBuffer();
System.arraycopy(src, 0, dest, offset, length);
int copyLength = Math.min(length, dest.length - offset);
System.arraycopy(src, 0, dest, offset, copyLength);
offset += length;
}
} else {
// Reconstruct the missing segment
byte[] parity = new byte[length];
int missingOffset = -1;
for(int i = 0; i < n; i++) {
for(int i = 0; i < n - 1; i++) {
if(set[i] == null) {
missingOffset = offset;
} else {
byte[] src = set[i].getBuffer();
System.arraycopy(src, 0, dest, offset, length);
for(int j = 0; j < length; j++) parity[j] ^= src[j];
int copyLength = Math.min(length, dest.length - offset);
System.arraycopy(src, 0, dest, offset, copyLength);
}
offset += length;
}
byte[] src = set[n - 1].getBuffer();
for(int i = 0; i < length; i++) parity[i] ^= src[i];
assert missingOffset != -1;
System.arraycopy(parity, 0, dest, missingOffset, length);
int copyLength = Math.min(length, dest.length - missingOffset);
System.arraycopy(parity, 0, dest, missingOffset, copyLength);
}
assert offset == length * (n - 1);
// The frame length may not be an exact multiple of the segment length
int payload = HeaderEncoder.getPayloadLength(dest);
int padding = HeaderEncoder.getPaddingLength(dest);

View File

@@ -21,8 +21,9 @@ class XorErasureEncoder implements ErasureEncoder {
byte[] src = f.getBuffer(), parity = set[n - 1].getBuffer();
int offset = 0;
for(int i = 0; i < n - 1; i++) {
System.arraycopy(src, offset, set[i].getBuffer(), 0, length);
for(int j = 0; j < length; j++) parity[j] ^= src[offset + j];
int copyLength = Math.min(length, src.length - offset);
System.arraycopy(src, offset, set[i].getBuffer(), 0, copyLength);
for(int j = 0; j < copyLength; j++) parity[j] ^= src[offset + j];
offset += length;
}
return set;