Moved writers into their own package, replaced public static fields in

CryptoModule with provider methods.
This commit is contained in:
akwizgran
2011-07-23 18:50:40 +01:00
parent 13f18d9e40
commit 65be63dc0c
19 changed files with 205 additions and 78 deletions

View File

@@ -0,0 +1,49 @@
package net.sf.briar.protocol.writers;
import java.io.IOException;
import java.io.OutputStream;
import net.sf.briar.api.protocol.Ack;
import net.sf.briar.api.protocol.BatchId;
import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.protocol.writers.AckWriter;
import net.sf.briar.api.serial.Writer;
import net.sf.briar.api.serial.WriterFactory;
class AckWriterImpl implements AckWriter {
private final OutputStream out;
private final Writer w;
private boolean started = false, finished = false;
AckWriterImpl(OutputStream out, WriterFactory writerFactory) {
this.out = out;
this.w = writerFactory.createWriter(out);
}
public boolean addBatchId(BatchId b) throws IOException {
if(finished) throw new IllegalStateException();
if(!started) {
w.writeUserDefinedTag(Tags.ACK);
w.writeListStart();
started = true;
}
int capacity = Ack.MAX_SIZE - (int) w.getBytesWritten() - 1;
if(capacity < BatchId.SERIALISED_LENGTH) return false;
b.writeTo(w);
return true;
}
public void finish() throws IOException {
if(finished) throw new IllegalStateException();
if(!started) {
w.writeUserDefinedTag(Tags.ACK);
w.writeListStart();
started = true;
}
w.writeListEnd();
out.flush();
finished = true;
}
}

View File

@@ -0,0 +1,62 @@
package net.sf.briar.protocol.writers;
import java.io.IOException;
import java.io.OutputStream;
import java.security.DigestOutputStream;
import java.security.MessageDigest;
import net.sf.briar.api.protocol.Batch;
import net.sf.briar.api.protocol.BatchId;
import net.sf.briar.api.protocol.Tags;
import net.sf.briar.api.protocol.writers.BatchWriter;
import net.sf.briar.api.serial.Writer;
import net.sf.briar.api.serial.WriterFactory;
class BatchWriterImpl implements BatchWriter {
private final DigestOutputStream out;
private final Writer w;
private final MessageDigest messageDigest;
private boolean started = false, finished = false;
BatchWriterImpl(OutputStream out, WriterFactory writerFactory,
MessageDigest messageDigest) {
this.out = new DigestOutputStream(out, messageDigest);
w = writerFactory.createWriter(this.out);
this.messageDigest = messageDigest;
}
public int getCapacity() {
return Batch.MAX_SIZE - 3;
}
public boolean addMessage(byte[] message) throws IOException {
if(finished) throw new IllegalStateException();
if(!started) {
messageDigest.reset();
w.writeUserDefinedTag(Tags.BATCH);
w.writeListStart();
started = true;
}
int capacity = Batch.MAX_SIZE - (int) w.getBytesWritten() - 1;
if(capacity < message.length) return false;
// Bypass the writer and write each raw message directly
out.write(message);
return true;
}
public BatchId finish() throws IOException {
if(finished) throw new IllegalStateException();
if(!started) {
messageDigest.reset();
w.writeUserDefinedTag(Tags.BATCH);
w.writeListStart();
started = true;
}
w.writeListEnd();
out.flush();
finished = true;
return new BatchId(messageDigest.digest());
}
}

View File

@@ -0,0 +1,44 @@
package net.sf.briar.protocol.writers;
import java.io.OutputStream;
import java.security.MessageDigest;
import net.sf.briar.api.protocol.writers.AckWriter;
import net.sf.briar.api.protocol.writers.BatchWriter;
import net.sf.briar.api.protocol.writers.PacketWriterFactory;
import net.sf.briar.api.protocol.writers.SubscriptionWriter;
import net.sf.briar.api.protocol.writers.TransportWriter;
import net.sf.briar.api.serial.WriterFactory;
import com.google.inject.Inject;
class PacketWriterFactoryImpl implements PacketWriterFactory {
private final MessageDigest messageDigest;
private final WriterFactory writerFactory;
@Inject
PacketWriterFactoryImpl(MessageDigest messageDigest,
WriterFactory writerFactory) {
this.messageDigest = messageDigest;
this.writerFactory = writerFactory;
}
public AckWriter createAckWriter(OutputStream out) {
return new AckWriterImpl(out, writerFactory);
}
public BatchWriter createBatchWriter(OutputStream out) {
return new BatchWriterImpl(out, writerFactory, messageDigest);
}
public SubscriptionWriter createSubscriptionWriter(OutputStream out) {
// TODO Auto-generated method stub
return null;
}
public TransportWriter createTransportWriter(OutputStream out) {
// TODO Auto-generated method stub
return null;
}
}

View File

@@ -0,0 +1,14 @@
package net.sf.briar.protocol.writers;
import net.sf.briar.api.protocol.writers.PacketWriterFactory;
import com.google.inject.AbstractModule;
public class WritersModule extends AbstractModule {
@Override
protected void configure() {
bind(PacketWriterFactory.class).to(PacketWriterFactoryImpl.class);
}
}