From 3b32aee6be0f512de9718676b19f8341bafe7beb Mon Sep 17 00:00:00 2001
From: akwizgran <akwizgran@users.sourceforge.net>
Date: Fri, 19 Aug 2011 20:10:14 +0200
Subject: [PATCH] Slightly modified ConnectionWriterImpl to write a full-size
 frame as soon as possible, rather than waiting for the next write.

---
 .../briar/transport/ConnectionWriterImpl.java |  8 +++++--
 .../transport/ConnectionWriterImplTest.java   | 24 +++++++++++++------
 2 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/components/net/sf/briar/transport/ConnectionWriterImpl.java b/components/net/sf/briar/transport/ConnectionWriterImpl.java
index ee7ef1b41a..651034904c 100644
--- a/components/net/sf/briar/transport/ConnectionWriterImpl.java
+++ b/components/net/sf/briar/transport/ConnectionWriterImpl.java
@@ -13,6 +13,10 @@ import javax.crypto.Mac;
 import net.sf.briar.api.transport.ConnectionWriter;
 import net.sf.briar.util.ByteUtils;
 
+/**
+ * A ConnectionWriter that buffers its input and writes a frame whenever there
+ * is a full-size frame to write or the flush() method is called.
+ */
 class ConnectionWriterImpl extends FilterOutputStream
 implements ConnectionWriter {
 
@@ -45,8 +49,8 @@ implements ConnectionWriter {
 
 	@Override
 	public void write(int b) throws IOException {
-		if(buf.size() == maxPayloadLength) writeFrame();
 		buf.write(b);
+		if(buf.size() == maxPayloadLength) writeFrame();
 	}
 
 	@Override
@@ -57,7 +61,7 @@ implements ConnectionWriter {
 	@Override
 	public void write(byte[] b, int off, int len) throws IOException {
 		int available = maxPayloadLength - buf.size();
-		while(available < len) {
+		while(available <= len) {
 			buf.write(b, off, available);
 			writeFrame();
 			off += available;
diff --git a/test/net/sf/briar/transport/ConnectionWriterImplTest.java b/test/net/sf/briar/transport/ConnectionWriterImplTest.java
index 4640ab10f6..bf11444b40 100644
--- a/test/net/sf/briar/transport/ConnectionWriterImplTest.java
+++ b/test/net/sf/briar/transport/ConnectionWriterImplTest.java
@@ -45,21 +45,31 @@ public class ConnectionWriterImplTest extends TransportTest {
 	}
 
 	@Test
-	public void testFrameIsWrittenAtMaxLength() throws Exception {
+	public void testWriteByteToMaxLengthWritesFrame() throws Exception {
 		ByteArrayOutputStream out = new ByteArrayOutputStream();
 		ConnectionEncrypter e = new NullConnectionEncrypter(out);
 		ConnectionWriter w = new ConnectionWriterImpl(e, mac);
 		OutputStream out1 = w.getOutputStream();
-		// The first maxPayloadLength bytes should be buffered
-		for(int i = 0; i < maxPayloadLength; i++) out1.write(0);
+		// The first maxPayloadLength - 1 bytes should be buffered
+		for(int i = 0; i < maxPayloadLength - 1; i++) out1.write(0);
 		assertEquals(0, out.size());
 		// The next byte should trigger the writing of a frame
 		out1.write(0);
 		assertEquals(MAX_FRAME_LENGTH, out.size());
-		// Flushing the stream should write a single-byte frame
-		out1.flush();
-		assertEquals(MAX_FRAME_LENGTH + headerLength + 1 + macLength,
-				out.size());
+	}
+
+	@Test
+	public void testWriteArrayToMaxLengthWritesFrame() throws Exception {
+		ByteArrayOutputStream out = new ByteArrayOutputStream();
+		ConnectionEncrypter e = new NullConnectionEncrypter(out);
+		ConnectionWriter w = new ConnectionWriterImpl(e, mac);
+		OutputStream out1 = w.getOutputStream();
+		// The first maxPayloadLength - 1 bytes should be buffered
+		out1.write(new byte[maxPayloadLength - 1]);
+		assertEquals(0, out.size());
+		// The next maxPayloadLength + 1 bytes should trigger two frames
+		out1.write(new byte[maxPayloadLength + 1]);
+		assertEquals(MAX_FRAME_LENGTH * 2, out.size());
 	}
 
 	@Test
-- 
GitLab