Briar uses the Bramble Sync Protocol [(BSP)](https://code.briarproject.org/briar/briar-spec/blob/master/protocols/BSP.md) to share data between two Briar users. These messages are encrypted using the Bramble Transport Protocol (BTP)[https://code.briarproject.org/briar/briar-spec/blob/master/protocols/BTP.md]. The encryption results in a *stream* of encrypted BSP messages, which we want to store as a file on the mailbox for later retrieval. While it would be possible to just try and send these streams all at once, we chose to chunk a single stream into equally sized parts which are sent individually and later recovered at the receiving end. This allows briar to continue an interrupted up-/download of a stream.
# Usage
*Note: Mailbox chunking is designed to be used with the Mailbox Protocol. See [here](https://code.briarproject.org/goapunk/briar-repeater/wikis/Mailbox-Protocol) for a reference.*
Chunked files are shared between the mailbox application and Briar (Mailbox owner or one of their contacts). Either Briar or the Mailbox can be the recipient or the sender but the procedure is identical for both:
1) The *sender* checks whether a file needs to be sent to the *recipient*.
2) If a file is available, the *sender* parses the file ( A list where each entry is a chunk of the stream).
3) The *sender*[offers](https://code.briarproject.org/goapunk/briar-repeater/wikis/Mailbox-Protocol#offer) the *recipient* the file. The *recipient* responds with a list of chunks missing from it's local file (Which is all if the file has not been previously received)
4) The *sender*[sends](https://code.briarproject.org/goapunk/briar-repeater/wikis/Mailbox-Protocol#chunk) all missing chunks (indicated by the offer's response) and awaits the responses, from the *receiver* for all chunks sent.
5) Once all chunks have been sent successfully (all responses for all CHUNK requests received), the *sender* finalizes the procedure by sending a *FIN_CHUNK* request and deletes the file
6) When the recipient receives the *FIN_CHUNK* request, they delete the file.