mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 18:59:06 +01:00
Don't create empty packets.
This commit is contained in:
@@ -14,7 +14,7 @@ import net.sf.briar.api.serial.WriterFactory;
|
||||
class AckWriterImpl implements AckWriter {
|
||||
|
||||
private final OutputStream out;
|
||||
private final SerialComponent serial;
|
||||
private final int headerLength, idLength, footerLength;
|
||||
private final Writer w;
|
||||
|
||||
private boolean started = false;
|
||||
@@ -23,17 +23,19 @@ class AckWriterImpl implements AckWriter {
|
||||
AckWriterImpl(OutputStream out, SerialComponent serial,
|
||||
WriterFactory writerFactory) {
|
||||
this.out = out;
|
||||
this.serial = serial;
|
||||
headerLength = serial.getSerialisedUserDefinedIdLength(Types.ACK)
|
||||
+ serial.getSerialisedListStartLength();
|
||||
idLength = serial.getSerialisedUniqueIdLength(Types.BATCH_ID);
|
||||
footerLength = serial.getSerialisedListEndLength();
|
||||
w = writerFactory.createWriter(out);
|
||||
}
|
||||
|
||||
public boolean writeBatchId(BatchId b) throws IOException {
|
||||
int overhead = started ? footerLength : headerLength + footerLength;
|
||||
if(capacity < idLength + overhead) return false;
|
||||
if(!started) start();
|
||||
int length = serial.getSerialisedUniqueIdLength(Types.BATCH_ID);
|
||||
if(capacity < length + serial.getSerialisedListEndLength())
|
||||
return false;
|
||||
b.writeTo(w);
|
||||
capacity -= length;
|
||||
capacity -= idLength;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -47,9 +49,8 @@ class AckWriterImpl implements AckWriter {
|
||||
|
||||
private void start() throws IOException {
|
||||
w.writeUserDefinedTag(Types.ACK);
|
||||
capacity -= serial.getSerialisedUserDefinedIdLength(Types.ACK);
|
||||
w.writeListStart();
|
||||
capacity -= serial.getSerialisedListStartLength();
|
||||
capacity -= headerLength;
|
||||
started = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ import net.sf.briar.api.serial.WriterFactory;
|
||||
class BatchWriterImpl implements BatchWriter {
|
||||
|
||||
private final DigestOutputStream out;
|
||||
private final SerialComponent serial;
|
||||
private final int headerLength, footerLength;
|
||||
private final Writer w;
|
||||
private final MessageDigest messageDigest;
|
||||
|
||||
@@ -26,15 +26,17 @@ class BatchWriterImpl implements BatchWriter {
|
||||
BatchWriterImpl(OutputStream out, SerialComponent serial,
|
||||
WriterFactory writerFactory, MessageDigest messageDigest) {
|
||||
this.out = new DigestOutputStream(out, messageDigest);
|
||||
this.serial = serial;
|
||||
headerLength = serial.getSerialisedUserDefinedIdLength(Types.BATCH)
|
||||
+ serial.getSerialisedListStartLength();
|
||||
footerLength = serial.getSerialisedListEndLength();
|
||||
w = writerFactory.createWriter(this.out);
|
||||
this.messageDigest = messageDigest;
|
||||
}
|
||||
|
||||
public boolean writeMessage(byte[] message) throws IOException {
|
||||
int overhead = started ? footerLength : headerLength + footerLength;
|
||||
if(capacity < message.length + overhead) return false;
|
||||
if(!started) start();
|
||||
if(capacity < message.length + serial.getSerialisedListEndLength())
|
||||
return false;
|
||||
// Bypass the writer and write the raw message directly
|
||||
out.write(message);
|
||||
capacity -= message.length;
|
||||
@@ -53,9 +55,8 @@ class BatchWriterImpl implements BatchWriter {
|
||||
private void start() throws IOException {
|
||||
messageDigest.reset();
|
||||
w.writeUserDefinedTag(Types.BATCH);
|
||||
capacity -= serial.getSerialisedUserDefinedIdLength(Types.BATCH);
|
||||
w.writeListStart();
|
||||
capacity -= serial.getSerialisedListStartLength();
|
||||
capacity -= headerLength;
|
||||
started = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ import net.sf.briar.api.serial.WriterFactory;
|
||||
class OfferWriterImpl implements OfferWriter {
|
||||
|
||||
private final OutputStream out;
|
||||
private final SerialComponent serial;
|
||||
private final int headerLength, idLength, footerLength;
|
||||
private final Writer w;
|
||||
|
||||
private boolean started = false;
|
||||
@@ -23,17 +23,19 @@ class OfferWriterImpl implements OfferWriter {
|
||||
OfferWriterImpl(OutputStream out, SerialComponent serial,
|
||||
WriterFactory writerFactory) {
|
||||
this.out = out;
|
||||
this.serial = serial;
|
||||
headerLength = serial.getSerialisedUserDefinedIdLength(Types.OFFER)
|
||||
+ serial.getSerialisedListStartLength();
|
||||
idLength = serial.getSerialisedUniqueIdLength(Types.MESSAGE_ID);
|
||||
footerLength = serial.getSerialisedListEndLength();
|
||||
w = writerFactory.createWriter(out);
|
||||
}
|
||||
|
||||
public boolean writeMessageId(MessageId m) throws IOException {
|
||||
int overhead = started ? footerLength : headerLength + footerLength;
|
||||
if(capacity < idLength + overhead) return false;
|
||||
if(!started) start();
|
||||
int length = serial.getSerialisedUniqueIdLength(Types.MESSAGE_ID);
|
||||
if(capacity < length + serial.getSerialisedListEndLength())
|
||||
return false;
|
||||
m.writeTo(w);
|
||||
capacity -= length;
|
||||
capacity -= idLength;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -48,6 +50,7 @@ class OfferWriterImpl implements OfferWriter {
|
||||
private void start() throws IOException {
|
||||
w.writeUserDefinedTag(Types.OFFER);
|
||||
w.writeListStart();
|
||||
capacity -= headerLength;
|
||||
started = true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user