From ef61eaa8040cf8d4dca1bf1cfaeeb872ccf77df2 Mon Sep 17 00:00:00 2001
From: akwizgran <akwizgran@users.sourceforge.net>
Date: Fri, 21 Oct 2011 18:42:27 +0100
Subject: [PATCH] Separated MessageHeader interface from Message interface.

---
 api/net/sf/briar/api/protocol/Message.java    | 35 ++++++-------------
 .../sf/briar/api/protocol/MessageHeader.java  | 25 +++++++++++++
 .../sf/briar/db/DatabaseComponentImpl.java    |  4 +--
 components/net/sf/briar/db/JdbcDatabase.java  | 13 ++++---
 .../net/sf/briar/protocol/MessageImpl.java    | 11 ++++--
 .../net/sf/briar/ProtocolIntegrationTest.java | 10 +++---
 test/net/sf/briar/db/TestMessage.java         | 11 ++++--
 .../briar/protocol/ProtocolReadWriteTest.java |  2 +-
 .../briar/protocol/writers/ConstantsTest.java |  2 +-
 9 files changed, 69 insertions(+), 44 deletions(-)
 create mode 100644 api/net/sf/briar/api/protocol/MessageHeader.java

diff --git a/api/net/sf/briar/api/protocol/Message.java b/api/net/sf/briar/api/protocol/Message.java
index 6b056700aa..0f145f01e5 100644
--- a/api/net/sf/briar/api/protocol/Message.java
+++ b/api/net/sf/briar/api/protocol/Message.java
@@ -1,6 +1,8 @@
 package net.sf.briar.api.protocol;
 
-public interface Message {
+import java.io.InputStream;
+
+public interface Message extends MessageHeader {
 
 	/**
 	 * The maximum length of a message body in bytes. To allow for future
@@ -19,30 +21,15 @@ public interface Message {
 	/** The length of the random salt in bytes. */
 	static final int SALT_LENGTH = 8;
 
-	/** Returns the message's unique identifier. */
-	MessageId getId();
+	/** Returns the length of the message in bytes. */
+	int getLength();
+
+	/** Returns the serialised representation of the entire message. */
+	byte[] getSerialisedBytes();
 
 	/**
-	 * Returns the message's parent, or MessageId.NONE if this is the first
-	 * message in a thread.
+	 * Returns a stream for reading the serialised representation of the entire
+	 * message.
 	 */
-	MessageId getParent();
-
-	/** Returns the group to which the message belongs. */
-	GroupId getGroup();
-
-	/** Returns the message's author. */
-	AuthorId getAuthor();
-
-	/** Returns the message's subject line. */
-	String getSubject();
-
-	/** Returns the timestamp created by the message's author. */
-	long getTimestamp();
-
-	/** Returns the size of the message in bytes. */
-	int getSize();
-
-	/** Returns the serialised representation of the message. */
-	byte[] getBytes();
+	InputStream getSerialisedStream();
 }
\ No newline at end of file
diff --git a/api/net/sf/briar/api/protocol/MessageHeader.java b/api/net/sf/briar/api/protocol/MessageHeader.java
new file mode 100644
index 0000000000..31ffc973cd
--- /dev/null
+++ b/api/net/sf/briar/api/protocol/MessageHeader.java
@@ -0,0 +1,25 @@
+package net.sf.briar.api.protocol;
+
+public interface MessageHeader {
+
+	/** Returns the message's unique identifier. */
+	MessageId getId();
+
+	/**
+	 * Returns the message's parent, or MessageId.NONE if this is the first
+	 * message in a thread.
+	 */
+	MessageId getParent();
+
+	/** Returns the group to which the message belongs. */
+	GroupId getGroup();
+
+	/** Returns the message's author. */
+	AuthorId getAuthor();
+
+	/** Returns the message's subject line. */
+	String getSubject();
+
+	/** Returns the timestamp created by the message's author. */
+	long getTimestamp();
+}
diff --git a/components/net/sf/briar/db/DatabaseComponentImpl.java b/components/net/sf/briar/db/DatabaseComponentImpl.java
index c89f788915..356338b1d9 100644
--- a/components/net/sf/briar/db/DatabaseComponentImpl.java
+++ b/components/net/sf/briar/db/DatabaseComponentImpl.java
@@ -236,7 +236,7 @@ DatabaseCleaner.Callback {
 			if(sendability > 0) updateAncestorSendability(txn, id, true);
 			// Count the bytes stored
 			synchronized(spaceLock) {
-				bytesStoredSinceLastCheck += m.getSize();
+				bytesStoredSinceLastCheck += m.getLength();
 			}
 		}
 		return stored;
@@ -344,7 +344,7 @@ DatabaseCleaner.Callback {
 		else db.setStatus(txn, c, id, Status.NEW);
 		// Count the bytes stored
 		synchronized(spaceLock) {
-			bytesStoredSinceLastCheck += m.getSize();
+			bytesStoredSinceLastCheck += m.getLength();
 		}
 		return true;
 	}
diff --git a/components/net/sf/briar/db/JdbcDatabase.java b/components/net/sf/briar/db/JdbcDatabase.java
index a6d6adc66d..c69b16d4ca 100644
--- a/components/net/sf/briar/db/JdbcDatabase.java
+++ b/components/net/sf/briar/db/JdbcDatabase.java
@@ -1,6 +1,5 @@
 package net.sf.briar.db;
 
-import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.IOException;
 import java.sql.Connection;
@@ -549,9 +548,9 @@ abstract class JdbcDatabase implements Database<Connection> {
 			if(m.getAuthor() == null) ps.setNull(4, Types.BINARY);
 			else ps.setBytes(4, m.getAuthor().getBytes());
 			ps.setLong(5, m.getTimestamp());
-			ps.setInt(6, m.getSize());
-			byte[] raw = m.getBytes();
-			ps.setBinaryStream(7, new ByteArrayInputStream(raw), raw.length);
+			int length = m.getLength();
+			ps.setInt(6, length);
+			ps.setBinaryStream(7, m.getSerialisedStream(), length);
 			int affected = ps.executeUpdate();
 			if(affected != 1) throw new DbStateException();
 			ps.close();
@@ -635,9 +634,9 @@ abstract class JdbcDatabase implements Database<Connection> {
 			if(m.getParent() == null) ps.setNull(2, Types.BINARY);
 			else ps.setBytes(2, m.getParent().getBytes());
 			ps.setLong(3, m.getTimestamp());
-			ps.setInt(4, m.getSize());
-			byte[] raw = m.getBytes();
-			ps.setBinaryStream(5, new ByteArrayInputStream(raw), raw.length);
+			int length = m.getLength();
+			ps.setInt(4, length);
+			ps.setBinaryStream(5, m.getSerialisedStream(), length);
 			ps.setInt(6, c.getInt());
 			int affected = ps.executeUpdate();
 			if(affected != 1) throw new DbStateException();
diff --git a/components/net/sf/briar/protocol/MessageImpl.java b/components/net/sf/briar/protocol/MessageImpl.java
index e8e54e1eb4..33b06eab06 100644
--- a/components/net/sf/briar/protocol/MessageImpl.java
+++ b/components/net/sf/briar/protocol/MessageImpl.java
@@ -1,5 +1,8 @@
 package net.sf.briar.protocol;
 
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+
 import net.sf.briar.api.protocol.AuthorId;
 import net.sf.briar.api.protocol.GroupId;
 import net.sf.briar.api.protocol.Message;
@@ -50,14 +53,18 @@ class MessageImpl implements Message {
 		return timestamp;
 	}
 
-	public int getSize() {
+	public int getLength() {
 		return raw.length;
 	}
 
-	public byte[] getBytes() {
+	public byte[] getSerialisedBytes() {
 		return raw;
 	}
 
+	public InputStream getSerialisedStream() {
+		return new ByteArrayInputStream(raw);
+	}
+
 	@Override
 	public boolean equals(Object o) {
 		return o instanceof Message && id.equals(((Message) o).getId());
diff --git a/test/net/sf/briar/ProtocolIntegrationTest.java b/test/net/sf/briar/ProtocolIntegrationTest.java
index e57d649f3c..a33aef927b 100644
--- a/test/net/sf/briar/ProtocolIntegrationTest.java
+++ b/test/net/sf/briar/ProtocolIntegrationTest.java
@@ -142,10 +142,10 @@ public class ProtocolIntegrationTest extends TestCase {
 		a.finish();
 
 		BatchWriter b = protocolWriterFactory.createBatchWriter(out1);
-		assertTrue(b.writeMessage(message.getBytes()));
-		assertTrue(b.writeMessage(message1.getBytes()));
-		assertTrue(b.writeMessage(message2.getBytes()));
-		assertTrue(b.writeMessage(message3.getBytes()));
+		assertTrue(b.writeMessage(message.getSerialisedBytes()));
+		assertTrue(b.writeMessage(message1.getSerialisedBytes()));
+		assertTrue(b.writeMessage(message2.getSerialisedBytes()));
+		assertTrue(b.writeMessage(message3.getSerialisedBytes()));
 		b.finish();
 
 		OfferWriter o = protocolWriterFactory.createOfferWriter(out1);
@@ -255,6 +255,6 @@ public class ProtocolIntegrationTest extends TestCase {
 		assertEquals(m1.getGroup(), m2.getGroup());
 		assertEquals(m1.getAuthor(), m2.getAuthor());
 		assertEquals(m1.getTimestamp(), m2.getTimestamp());
-		assertArrayEquals(m1.getBytes(), m2.getBytes());
+		assertArrayEquals(m1.getSerialisedBytes(), m2.getSerialisedBytes());
 	}
 }
diff --git a/test/net/sf/briar/db/TestMessage.java b/test/net/sf/briar/db/TestMessage.java
index c118dd7157..3084699472 100644
--- a/test/net/sf/briar/db/TestMessage.java
+++ b/test/net/sf/briar/db/TestMessage.java
@@ -1,5 +1,8 @@
 package net.sf.briar.db;
 
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+
 import net.sf.briar.api.protocol.AuthorId;
 import net.sf.briar.api.protocol.GroupId;
 import net.sf.briar.api.protocol.Message;
@@ -49,14 +52,18 @@ class TestMessage implements Message {
 		return timestamp;
 	}
 
-	public int getSize() {
+	public int getLength() {
 		return raw.length;
 	}
 
-	public byte[] getBytes() {
+	public byte[] getSerialisedBytes() {
 		return raw;
 	}
 
+	public InputStream getSerialisedStream() {
+		return new ByteArrayInputStream(raw);
+	}
+
 	@Override
 	public boolean equals(Object o) {
 		return o instanceof Message && id.equals(((Message)o).getId());
diff --git a/test/net/sf/briar/protocol/ProtocolReadWriteTest.java b/test/net/sf/briar/protocol/ProtocolReadWriteTest.java
index 43598bc483..b749649c67 100644
--- a/test/net/sf/briar/protocol/ProtocolReadWriteTest.java
+++ b/test/net/sf/briar/protocol/ProtocolReadWriteTest.java
@@ -87,7 +87,7 @@ public class ProtocolReadWriteTest extends TestCase {
 		a.finish();
 
 		BatchWriter b = writerFactory.createBatchWriter(out);
-		b.writeMessage(message.getBytes());
+		b.writeMessage(message.getSerialisedBytes());
 		b.finish();
 
 		OfferWriter o = writerFactory.createOfferWriter(out);
diff --git a/test/net/sf/briar/protocol/writers/ConstantsTest.java b/test/net/sf/briar/protocol/writers/ConstantsTest.java
index 694cc5a3a8..4c966682e7 100644
--- a/test/net/sf/briar/protocol/writers/ConstantsTest.java
+++ b/test/net/sf/briar/protocol/writers/ConstantsTest.java
@@ -113,7 +113,7 @@ public class ConstantsTest extends TestCase {
 				ProtocolConstants.MAX_PACKET_LENGTH);
 		BatchWriter b = new BatchWriterImpl(out, serial, writerFactory,
 				crypto.getMessageDigest());
-		assertTrue(b.writeMessage(message.getBytes()));
+		assertTrue(b.writeMessage(message.getSerialisedBytes()));
 		b.finish();
 		// Check the size of the serialised batch
 		assertTrue(out.size() > UniqueId.LENGTH + Group.MAX_NAME_LENGTH +
-- 
GitLab