From dafb4a45b59a2f0a802c102679552e02d4b31d18 Mon Sep 17 00:00:00 2001 From: bontric <benjohnwie@gmail.com> Date: Thu, 20 Sep 2018 18:30:48 +0200 Subject: [PATCH] Add readTag function to AbstractMailboxSession --- .../mailbox/AbstractMailboxSession.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/AbstractMailboxSession.java b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/AbstractMailboxSession.java index 410fa78c2..b1adf5248 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/mailbox/AbstractMailboxSession.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/mailbox/AbstractMailboxSession.java @@ -247,4 +247,35 @@ public abstract class AbstractMailboxSession implements Runnable { return os.toByteArray(); } + + /** + * Reads a Tag from the given InputStream and returns the matching StreamContext + * if available + * + * @param in InputStream to read the tag from + * @return StreamContext matching the tag read from the InputStream + * @throws IOException + * @throws DbException + */ + @Nullable + protected StreamContext readTag(InputStream in) + throws IOException, DbException { + + byte[] tag = new byte[TAG_LENGTH]; + // read tag from input stream + int offset = 0; + while (offset < tag.length) { + int read = in.read(tag, offset, tag.length - offset); + if (read == -1) throw new EOFException(); + offset += read; + } + + StreamContext ctx = + keyManager.getStreamContext(MailboxConstants.ID, tag); + + if (ctx == null) + throw new IOException( + "Received stream with unrecognisable tag"); + return ctx; + } } -- GitLab