Implemented incoming and outgoing batch connections (untested).

This commit is contained in:
akwizgran
2011-09-22 16:26:06 +01:00
parent b65d6631f1
commit 09971c8460
8 changed files with 244 additions and 2 deletions

View File

@@ -0,0 +1,39 @@
package net.sf.briar.transport.batch;
import java.io.ByteArrayInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import net.sf.briar.api.transport.batch.BatchTransportReader;
class ByteArrayBatchTransportReader extends FilterInputStream
implements BatchTransportReader {
ByteArrayBatchTransportReader(ByteArrayInputStream in) {
super(in);
}
public InputStream getInputStream() throws IOException {
return this;
}
public void dispose() throws IOException {
// Nothing to do
}
@Override
public int read() throws IOException {
return in.read();
}
@Override
public int read(byte[] b) throws IOException {
return read(b, 0, b.length);
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
return in.read(b, off, len);
}
}

View File

@@ -0,0 +1,50 @@
package net.sf.briar.transport.batch;
import java.io.ByteArrayOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import net.sf.briar.api.transport.batch.BatchTransportWriter;
class ByteArrayBatchTransportWriter extends FilterOutputStream
implements BatchTransportWriter {
private int capacity;
ByteArrayBatchTransportWriter(ByteArrayOutputStream out, int capacity) {
super(out);
this.capacity = capacity;
}
public long getCapacity() throws IOException {
return capacity;
}
public OutputStream getOutputStream() throws IOException {
return this;
}
public void dispose() throws IOException {
// Nothing to do
}
@Override
public void write(int b) throws IOException {
if(capacity < 1) throw new IllegalArgumentException();
out.write(b);
capacity--;
}
@Override
public void write(byte[] b) throws IOException {
write(b, 0, b.length);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
if(len > capacity) throw new IllegalArgumentException();
out.write(b, off, len);
capacity -= len;
}
}