From 7e1130b3fdfad4cd32a9c89828171359a00d225e Mon Sep 17 00:00:00 2001
From: bontric <benjohnwie@gmail.com>
Date: Thu, 20 Sep 2018 18:26:31 +0200
Subject: [PATCH] Use nullable value instead of explicit "hasContactId" for
 STORE requests

---
 .../mailbox/protocol/MailboxRequestStore.java | 29 +++++++++++++++----
 1 file changed, 23 insertions(+), 6 deletions(-)

diff --git a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/protocol/MailboxRequestStore.java b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/protocol/MailboxRequestStore.java
index 83cc85a86..362119f3d 100644
--- a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/protocol/MailboxRequestStore.java
+++ b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/protocol/MailboxRequestStore.java
@@ -4,6 +4,8 @@ import org.briarproject.bramble.api.FormatException;
 import org.briarproject.bramble.api.contact.ContactId;
 import org.briarproject.bramble.api.data.BdfList;
 
+import javax.annotation.Nullable;
+
 import static org.briarproject.bramble.mailbox.protocol.MailboxMessage.TYPE.STORE;
 
 public class MailboxRequestStore extends MailboxRequest {
@@ -17,11 +19,20 @@ public class MailboxRequestStore extends MailboxRequest {
 		this.encryptedSyncStream = encryptedSyncMessage;
 	}
 
+	public MailboxRequestStore(byte[] encryptedSyncMessage) {
+		super(STORE);
+		this.encryptedSyncStream = encryptedSyncMessage;
+		contactId = null;
+	}
+
 	public MailboxRequestStore(BdfList msg) throws FormatException {
 		super(msg);
 	}
 
 	public BdfList getRequestBody() {
+		if (contactId == null)
+			return BdfList.of(null, encryptedSyncStream);
+
 		return BdfList.of(contactId.getInt(), encryptedSyncStream);
 	}
 
@@ -32,20 +43,26 @@ public class MailboxRequestStore extends MailboxRequest {
 
 	@Override
 	public void parseBody(BdfList msg) throws FormatException {
-		Long cId = msg.getLong(0);
-
-		if (cId > Integer.MAX_VALUE || cId < Integer.MIN_VALUE)
-			throw new FormatException();
+		Long cId = msg.getOptionalLong(0);
+		if (cId != null) {
+			if (cId > Integer.MAX_VALUE || cId < Integer.MIN_VALUE)
+				throw new FormatException();
+			contactId = new ContactId(cId.intValue());
+		}
 
-		this.contactId = new ContactId(cId.intValue());
-		this.encryptedSyncStream = msg.getRaw(1);
+		encryptedSyncStream = msg.getRaw(1);
 	}
 
 	public byte[] getEncryptedSyncStream() {
 		return encryptedSyncStream;
 	}
 
+	@Nullable
 	public ContactId getContactId() {
 		return contactId;
 	}
+
+	public boolean hasContactId() {
+		return contactId != null;
+	}
 }
-- 
GitLab