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 410fa78c2ffd3a218e87e29d54aac7acfbc49d47..b1adf524833688d882cb69e0402692328829aa25 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; + } }