mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 21:29:54 +01:00
Whitespace-only code formatting changes.
This commit is contained in:
@@ -13,7 +13,7 @@ class Ack extends Frame {
|
||||
|
||||
Ack(byte[] buf) {
|
||||
super(buf);
|
||||
if(buf.length != LENGTH) throw new IllegalArgumentException();
|
||||
if (buf.length != LENGTH) throw new IllegalArgumentException();
|
||||
buf[0] = (byte) Frame.ACK_FLAG;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,10 +5,10 @@ class Crc32 {
|
||||
private static final long[] TABLE = new long[256];
|
||||
|
||||
static {
|
||||
for(int i = 0; i < 256; i++) {
|
||||
for (int i = 0; i < 256; i++) {
|
||||
long c = i;
|
||||
for(int j = 0; j < 8; j++) {
|
||||
if((c & 1) != 0) c = 0xedb88320L ^ (c >> 1);
|
||||
for (int j = 0; j < 8; j++) {
|
||||
if ((c & 1) != 0) c = 0xedb88320L ^ (c >> 1);
|
||||
else c >>= 1;
|
||||
}
|
||||
TABLE[i] = c;
|
||||
@@ -16,7 +16,7 @@ class Crc32 {
|
||||
}
|
||||
|
||||
private static long update(long c, byte[] b, int off, int len) {
|
||||
for(int i = off; i < off + len; i++)
|
||||
for (int i = off; i < off + len; i++)
|
||||
c = TABLE[(int) ((c ^ b[i]) & 0xff)] ^ (c >> 8);
|
||||
return c;
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ class Data extends Frame {
|
||||
|
||||
Data(byte[] buf) {
|
||||
super(buf);
|
||||
if(buf.length < MIN_LENGTH || buf.length > MAX_LENGTH)
|
||||
if (buf.length < MIN_LENGTH || buf.length > MAX_LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ class Data extends Frame {
|
||||
}
|
||||
|
||||
void setLastFrame(boolean lastFrame) {
|
||||
if(lastFrame) buf[0] = (byte) Frame.FIN_FLAG;
|
||||
if (lastFrame) buf[0] = (byte) Frame.FIN_FLAG;
|
||||
}
|
||||
|
||||
int getPayloadLength() {
|
||||
|
||||
@@ -48,7 +48,7 @@ abstract class Frame {
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if(o instanceof Frame) {
|
||||
if (o instanceof Frame) {
|
||||
Frame f = (Frame) o;
|
||||
return buf[0] == f.buf[0] &&
|
||||
getSequenceNumber() == f.getSequenceNumber();
|
||||
|
||||
@@ -43,13 +43,13 @@ class Receiver implements ReadHandler {
|
||||
windowLock.lock();
|
||||
try {
|
||||
long now = clock.currentTimeMillis(), end = now + READ_TIMEOUT;
|
||||
while(now < end && valid) {
|
||||
if(dataFrames.isEmpty()) {
|
||||
while (now < end && valid) {
|
||||
if (dataFrames.isEmpty()) {
|
||||
// Wait for a data frame
|
||||
dataFrameAvailable.await(end - now, MILLISECONDS);
|
||||
} else {
|
||||
Data d = dataFrames.first();
|
||||
if(d.getSequenceNumber() == nextSequenceNumber) {
|
||||
if (d.getSequenceNumber() == nextSequenceNumber) {
|
||||
dataFrames.remove(d);
|
||||
// Update the window
|
||||
windowSize += d.getPayloadLength();
|
||||
@@ -63,7 +63,7 @@ class Receiver implements ReadHandler {
|
||||
}
|
||||
now = clock.currentTimeMillis();
|
||||
}
|
||||
if(valid) throw new IOException("Read timed out");
|
||||
if (valid) throw new IOException("Read timed out");
|
||||
throw new IOException("Connection closed");
|
||||
} finally {
|
||||
windowLock.unlock();
|
||||
@@ -81,7 +81,7 @@ class Receiver implements ReadHandler {
|
||||
}
|
||||
|
||||
public void handleRead(byte[] b) throws IOException {
|
||||
if(!valid) throw new IOException("Connection closed");
|
||||
if (!valid) throw new IOException("Connection closed");
|
||||
switch(b[0]) {
|
||||
case 0:
|
||||
case Frame.FIN_FLAG:
|
||||
@@ -99,36 +99,36 @@ class Receiver implements ReadHandler {
|
||||
private void handleData(byte[] b) throws IOException {
|
||||
windowLock.lock();
|
||||
try {
|
||||
if(b.length < Data.MIN_LENGTH || b.length > Data.MAX_LENGTH) {
|
||||
if (b.length < Data.MIN_LENGTH || b.length > Data.MAX_LENGTH) {
|
||||
// Ignore data frame with invalid length
|
||||
return;
|
||||
}
|
||||
Data d = new Data(b);
|
||||
int payloadLength = d.getPayloadLength();
|
||||
if(payloadLength > windowSize) return; // No space in the window
|
||||
if(d.getChecksum() != d.calculateChecksum()) {
|
||||
if (payloadLength > windowSize) return; // No space in the window
|
||||
if (d.getChecksum() != d.calculateChecksum()) {
|
||||
// Ignore data frame with invalid checksum
|
||||
return;
|
||||
}
|
||||
long sequenceNumber = d.getSequenceNumber();
|
||||
if(sequenceNumber == 0) {
|
||||
if (sequenceNumber == 0) {
|
||||
// Window probe
|
||||
} else if(sequenceNumber < nextSequenceNumber) {
|
||||
} else if (sequenceNumber < nextSequenceNumber) {
|
||||
// Duplicate data frame
|
||||
} else if(d.isLastFrame()) {
|
||||
} else if (d.isLastFrame()) {
|
||||
finalSequenceNumber = sequenceNumber;
|
||||
// Remove any data frames with higher sequence numbers
|
||||
Iterator<Data> it = dataFrames.iterator();
|
||||
while(it.hasNext()) {
|
||||
while (it.hasNext()) {
|
||||
Data d1 = it.next();
|
||||
if(d1.getSequenceNumber() >= finalSequenceNumber) it.remove();
|
||||
if (d1.getSequenceNumber() >= finalSequenceNumber) it.remove();
|
||||
}
|
||||
if(dataFrames.add(d)) {
|
||||
if (dataFrames.add(d)) {
|
||||
windowSize -= payloadLength;
|
||||
dataFrameAvailable.signalAll();
|
||||
}
|
||||
} else if(sequenceNumber < finalSequenceNumber) {
|
||||
if(dataFrames.add(d)) {
|
||||
} else if (sequenceNumber < finalSequenceNumber) {
|
||||
if (dataFrames.add(d)) {
|
||||
windowSize -= payloadLength;
|
||||
dataFrameAvailable.signalAll();
|
||||
}
|
||||
@@ -144,8 +144,8 @@ class Receiver implements ReadHandler {
|
||||
|
||||
public int compare(Data d1, Data d2) {
|
||||
long s1 = d1.getSequenceNumber(), s2 = d2.getSequenceNumber();
|
||||
if(s1 < s2) return -1;
|
||||
if(s1 > s2) return 1;
|
||||
if (s1 < s2) return -1;
|
||||
if (s1 > s2) return 1;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,8 +16,8 @@ class ReceiverInputStream extends InputStream {
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
if(length == -1) return -1;
|
||||
while(length == 0) if(!receive()) return -1;
|
||||
if (length == -1) return -1;
|
||||
while (length == 0) if (!receive()) return -1;
|
||||
int b = data.getBuffer()[offset] & 0xff;
|
||||
offset++;
|
||||
length--;
|
||||
@@ -31,8 +31,8 @@ class ReceiverInputStream extends InputStream {
|
||||
|
||||
@Override
|
||||
public int read(byte[] b, int off, int len) throws IOException {
|
||||
if(length == -1) return -1;
|
||||
while(length == 0) if(!receive()) return -1;
|
||||
if (length == -1) return -1;
|
||||
while (length == 0) if (!receive()) return -1;
|
||||
len = Math.min(len, length);
|
||||
System.arraycopy(data.getBuffer(), offset, b, off, len);
|
||||
offset += len;
|
||||
@@ -42,13 +42,13 @@ class ReceiverInputStream extends InputStream {
|
||||
|
||||
private boolean receive() throws IOException {
|
||||
assert length == 0;
|
||||
if(data != null && data.isLastFrame()) {
|
||||
if (data != null && data.isLastFrame()) {
|
||||
length = -1;
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
data = receiver.read();
|
||||
} catch(InterruptedException e) {
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IOException("Interrupted while reading");
|
||||
}
|
||||
|
||||
@@ -54,27 +54,27 @@ class ReliabilityLayerImpl implements ReliabilityLayer, WriteHandler {
|
||||
long now = clock.currentTimeMillis();
|
||||
long next = now + TICK_INTERVAL;
|
||||
try {
|
||||
while(running) {
|
||||
while (running) {
|
||||
byte[] b = null;
|
||||
while(now < next && b == null) {
|
||||
while (now < next && b == null) {
|
||||
b = writes.poll(next - now, MILLISECONDS);
|
||||
if(!running) return;
|
||||
if (!running) return;
|
||||
now = clock.currentTimeMillis();
|
||||
}
|
||||
if(b == null) {
|
||||
if (b == null) {
|
||||
sender.tick();
|
||||
while(next <= now) next += TICK_INTERVAL;
|
||||
while (next <= now) next += TICK_INTERVAL;
|
||||
} else {
|
||||
if(b.length == 0) return; // Poison pill
|
||||
if (b.length == 0) return; // Poison pill
|
||||
writeHandler.handleWrite(b);
|
||||
}
|
||||
}
|
||||
} catch(InterruptedException e) {
|
||||
} catch (InterruptedException e) {
|
||||
LOG.warning("Interrupted while waiting to write");
|
||||
Thread.currentThread().interrupt();
|
||||
running = false;
|
||||
} catch(IOException e) {
|
||||
if(LOG.isLoggable(WARNING))
|
||||
} catch (IOException e) {
|
||||
if (LOG.isLoggable(WARNING))
|
||||
LOG.log(WARNING, e.toString(), e);
|
||||
running = false;
|
||||
}
|
||||
@@ -98,11 +98,11 @@ class ReliabilityLayerImpl implements ReliabilityLayer, WriteHandler {
|
||||
|
||||
// The lower layer calls this method to pass data up to the SLIP decoder
|
||||
public void handleRead(byte[] b) throws IOException {
|
||||
if(running) decoder.handleRead(b);
|
||||
if (running) decoder.handleRead(b);
|
||||
}
|
||||
|
||||
// The SLIP encoder calls this method to pass data down to the lower layer
|
||||
public void handleWrite(byte[] b) {
|
||||
if(running && b.length > 0) writes.add(b);
|
||||
if (running && b.length > 0) writes.add(b);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,12 +53,12 @@ class Sender {
|
||||
}
|
||||
|
||||
void handleAck(byte[] b) throws IOException {
|
||||
if(b.length != Ack.LENGTH) {
|
||||
if (b.length != Ack.LENGTH) {
|
||||
// Ignore ack frame with invalid length
|
||||
return;
|
||||
}
|
||||
Ack a = new Ack(b);
|
||||
if(a.getChecksum() != a.calculateChecksum()) {
|
||||
if (a.getChecksum() != a.calculateChecksum()) {
|
||||
// Ignore ack frame with invalid checksum
|
||||
return;
|
||||
}
|
||||
@@ -70,27 +70,27 @@ class Sender {
|
||||
// Remove the acked data frame if it's outstanding
|
||||
int foundIndex = -1;
|
||||
Iterator<Outstanding> it = outstanding.iterator();
|
||||
for(int i = 0; it.hasNext(); i++) {
|
||||
for (int i = 0; it.hasNext(); i++) {
|
||||
Outstanding o = it.next();
|
||||
if(o.data.getSequenceNumber() == sequenceNumber) {
|
||||
if (o.data.getSequenceNumber() == sequenceNumber) {
|
||||
it.remove();
|
||||
outstandingBytes -= o.data.getPayloadLength();
|
||||
foundIndex = i;
|
||||
// Update the round-trip time and retransmission timeout
|
||||
if(!o.retransmitted) {
|
||||
if (!o.retransmitted) {
|
||||
int sample = (int) (now - o.lastTransmitted);
|
||||
int error = sample - rtt;
|
||||
rtt += (error >> 3);
|
||||
rttVar += (Math.abs(error) - rttVar) >> 2;
|
||||
rto = rtt + (rttVar << 2);
|
||||
if(rto < MIN_RTO) rto = MIN_RTO;
|
||||
else if(rto > MAX_RTO) rto = MAX_RTO;
|
||||
if (rto < MIN_RTO) rto = MIN_RTO;
|
||||
else if (rto > MAX_RTO) rto = MAX_RTO;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
// If any older data frames are outstanding, retransmit the oldest
|
||||
if(foundIndex > 0) {
|
||||
if (foundIndex > 0) {
|
||||
fastRetransmit = outstanding.poll();
|
||||
fastRetransmit.lastTransmitted = now;
|
||||
fastRetransmit.retransmitted = true;
|
||||
@@ -102,13 +102,13 @@ class Sender {
|
||||
// Don't accept an unreasonably large window size
|
||||
windowSize = Math.min(a.getWindowSize(), MAX_WINDOW_SIZE);
|
||||
// If space has become available, notify any waiting writers
|
||||
if(windowSize > oldWindowSize || foundIndex != -1)
|
||||
if (windowSize > oldWindowSize || foundIndex != -1)
|
||||
sendWindowAvailable.signalAll();
|
||||
} finally {
|
||||
windowLock.unlock();
|
||||
}
|
||||
// Fast retransmission
|
||||
if(fastRetransmit != null)
|
||||
if (fastRetransmit != null)
|
||||
writeHandler.handleWrite(fastRetransmit.data.getBuffer());
|
||||
}
|
||||
|
||||
@@ -118,28 +118,28 @@ class Sender {
|
||||
boolean sendProbe = false;
|
||||
windowLock.lock();
|
||||
try {
|
||||
if(outstanding.isEmpty()) {
|
||||
if(dataWaiting && now - lastWindowUpdateOrProbe > rto) {
|
||||
if (outstanding.isEmpty()) {
|
||||
if (dataWaiting && now - lastWindowUpdateOrProbe > rto) {
|
||||
sendProbe = true;
|
||||
rto <<= 1;
|
||||
if(rto > MAX_RTO) rto = MAX_RTO;
|
||||
if (rto > MAX_RTO) rto = MAX_RTO;
|
||||
}
|
||||
} else {
|
||||
Iterator<Outstanding> it = outstanding.iterator();
|
||||
while(it.hasNext()) {
|
||||
while (it.hasNext()) {
|
||||
Outstanding o = it.next();
|
||||
if(now - o.lastTransmitted > rto) {
|
||||
if (now - o.lastTransmitted > rto) {
|
||||
it.remove();
|
||||
if(retransmit == null)
|
||||
if (retransmit == null)
|
||||
retransmit = new ArrayList<Outstanding>();
|
||||
retransmit.add(o);
|
||||
// Update the retransmission timeout
|
||||
rto <<= 1;
|
||||
if(rto > MAX_RTO) rto = MAX_RTO;
|
||||
if (rto > MAX_RTO) rto = MAX_RTO;
|
||||
}
|
||||
}
|
||||
if(retransmit != null) {
|
||||
for(Outstanding o : retransmit) {
|
||||
if (retransmit != null) {
|
||||
for (Outstanding o : retransmit) {
|
||||
o.lastTransmitted = now;
|
||||
o.retransmitted = true;
|
||||
outstanding.add(o);
|
||||
@@ -150,15 +150,15 @@ class Sender {
|
||||
windowLock.unlock();
|
||||
}
|
||||
// Send a window probe if necessary
|
||||
if(sendProbe) {
|
||||
if (sendProbe) {
|
||||
byte[] buf = new byte[Data.MIN_LENGTH];
|
||||
Data probe = new Data(buf);
|
||||
probe.setChecksum(probe.calculateChecksum());
|
||||
writeHandler.handleWrite(buf);
|
||||
}
|
||||
// Retransmit any lost data frames
|
||||
if(retransmit != null) {
|
||||
for(Outstanding o : retransmit)
|
||||
if (retransmit != null) {
|
||||
for (Outstanding o : retransmit)
|
||||
writeHandler.handleWrite(o.data.getBuffer());
|
||||
}
|
||||
}
|
||||
@@ -169,12 +169,12 @@ class Sender {
|
||||
try {
|
||||
// Wait for space in the window
|
||||
long now = clock.currentTimeMillis(), end = now + WRITE_TIMEOUT;
|
||||
while(now < end && outstandingBytes + payloadLength >= windowSize) {
|
||||
while (now < end && outstandingBytes + payloadLength >= windowSize) {
|
||||
dataWaiting = true;
|
||||
sendWindowAvailable.await(end - now, MILLISECONDS);
|
||||
now = clock.currentTimeMillis();
|
||||
}
|
||||
if(outstandingBytes + payloadLength >= windowSize)
|
||||
if (outstandingBytes + payloadLength >= windowSize)
|
||||
throw new IOException("Write timed out");
|
||||
outstanding.add(new Outstanding(d, now));
|
||||
outstandingBytes += payloadLength;
|
||||
@@ -188,7 +188,7 @@ class Sender {
|
||||
void flush() throws IOException, InterruptedException {
|
||||
windowLock.lock();
|
||||
try {
|
||||
while(dataWaiting || !outstanding.isEmpty())
|
||||
while (dataWaiting || !outstanding.isEmpty())
|
||||
sendWindowAvailable.await();
|
||||
} finally {
|
||||
windowLock.unlock();
|
||||
|
||||
@@ -20,7 +20,7 @@ class SenderOutputStream extends OutputStream {
|
||||
send(true);
|
||||
try {
|
||||
sender.flush();
|
||||
} catch(InterruptedException e) {
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IOException("Interrupted while closing");
|
||||
}
|
||||
@@ -28,10 +28,10 @@ class SenderOutputStream extends OutputStream {
|
||||
|
||||
@Override
|
||||
public void flush() throws IOException {
|
||||
if(offset > Data.HEADER_LENGTH) send(false);
|
||||
if (offset > Data.HEADER_LENGTH) send(false);
|
||||
try {
|
||||
sender.flush();
|
||||
} catch(InterruptedException e) {
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IOException("Interrupted while flushing");
|
||||
}
|
||||
@@ -41,7 +41,7 @@ class SenderOutputStream extends OutputStream {
|
||||
public void write(int b) throws IOException {
|
||||
buf[offset] = (byte) b;
|
||||
offset++;
|
||||
if(offset == Data.HEADER_LENGTH + Data.MAX_PAYLOAD_LENGTH) send(false);
|
||||
if (offset == Data.HEADER_LENGTH + Data.MAX_PAYLOAD_LENGTH) send(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -52,7 +52,7 @@ class SenderOutputStream extends OutputStream {
|
||||
@Override
|
||||
public void write(byte[] b, int off, int len) throws IOException {
|
||||
int available = Data.MAX_LENGTH - offset - Data.FOOTER_LENGTH;
|
||||
while(available <= len) {
|
||||
while (available <= len) {
|
||||
System.arraycopy(b, off, buf, offset, available);
|
||||
offset += available;
|
||||
send(false);
|
||||
@@ -73,7 +73,7 @@ class SenderOutputStream extends OutputStream {
|
||||
d.setChecksum(d.calculateChecksum());
|
||||
try {
|
||||
sender.write(d);
|
||||
} catch(InterruptedException e) {
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IOException("Interrupted while writing");
|
||||
}
|
||||
|
||||
@@ -22,13 +22,13 @@ class SlipDecoder implements ReadHandler {
|
||||
}
|
||||
|
||||
public void handleRead(byte[] b) throws IOException {
|
||||
for(int i = 0; i < b.length; i++) {
|
||||
for (int i = 0; i < b.length; i++) {
|
||||
switch(b[i]) {
|
||||
case END:
|
||||
if(escape) {
|
||||
if (escape) {
|
||||
reset(true);
|
||||
} else {
|
||||
if(decodedLength > 0) {
|
||||
if (decodedLength > 0) {
|
||||
byte[] decoded = new byte[decodedLength];
|
||||
System.arraycopy(buf, 0, decoded, 0, decodedLength);
|
||||
readHandler.handleRead(decoded);
|
||||
@@ -37,31 +37,31 @@ class SlipDecoder implements ReadHandler {
|
||||
}
|
||||
break;
|
||||
case ESC:
|
||||
if(escape) reset(true);
|
||||
if (escape) reset(true);
|
||||
else escape = true;
|
||||
break;
|
||||
case TEND:
|
||||
if(escape) {
|
||||
if (escape) {
|
||||
escape = false;
|
||||
if(decodedLength == buf.length) reset(true);
|
||||
if (decodedLength == buf.length) reset(true);
|
||||
else buf[decodedLength++] = END;
|
||||
} else {
|
||||
if(decodedLength == buf.length) reset(true);
|
||||
if (decodedLength == buf.length) reset(true);
|
||||
else buf[decodedLength++] = TEND;
|
||||
}
|
||||
break;
|
||||
case TESC:
|
||||
if(escape) {
|
||||
if (escape) {
|
||||
escape = false;
|
||||
if(decodedLength == buf.length) reset(true);
|
||||
if (decodedLength == buf.length) reset(true);
|
||||
else buf[decodedLength++] = ESC;
|
||||
} else {
|
||||
if(decodedLength == buf.length) reset(true);
|
||||
if (decodedLength == buf.length) reset(true);
|
||||
else buf[decodedLength++] = TESC;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if(escape || decodedLength == buf.length) reset(true);
|
||||
if (escape || decodedLength == buf.length) reset(true);
|
||||
else buf[decodedLength++] = b[i];
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -18,15 +18,15 @@ class SlipEncoder implements WriteHandler {
|
||||
|
||||
public void handleWrite(byte[] b) throws IOException {
|
||||
int encodedLength = b.length + 2;
|
||||
for(int i = 0; i < b.length; i++)
|
||||
if(b[i] == END || b[i] == ESC) encodedLength++;
|
||||
for (int i = 0; i < b.length; i++)
|
||||
if (b[i] == END || b[i] == ESC) encodedLength++;
|
||||
byte[] encoded = new byte[encodedLength];
|
||||
encoded[0] = END;
|
||||
for(int i = 0, j = 1; i < b.length; i++) {
|
||||
if(b[i] == END) {
|
||||
for (int i = 0, j = 1; i < b.length; i++) {
|
||||
if (b[i] == END) {
|
||||
encoded[j++] = ESC;
|
||||
encoded[j++] = TEND;
|
||||
} else if(b[i] == ESC) {
|
||||
} else if (b[i] == ESC) {
|
||||
encoded[j++] = ESC;
|
||||
encoded[j++] = TESC;
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user