One of my current tasks with the Serval Project is the redevelopment of the Serval Maps application, which was the focus of my honours thesis last year.
As part of the redevelopment I’ve implemented a new data interchange format that takes advantage of the Rhizome capabilities of the Serval Mesh software. I’ve decided to use Google Protocol Buffers as the basis for my binary file format.
To write the messages to the file it is necessary to use the writeDelimitedTo method of my Message class to write the messages to the file. By using this method each message is delimited in the file.
The code looks something like this:
try {
FileOutputStream mOutput = new FileOutputStream(mOutputPath + mFileName, true);
mMessageBuilder.build().writeDelimitedTo(mOutput);
mOutput.close();
} catch (FileNotFoundException e) {
Log.e(TAG, "unable to create the output file", e);
return;
} catch (IOException e) {
Log.e(TAG, "unable to write to the output file", e);
return;
}
Reading back the messages from the file requires the use of the mergeDelimitedFrom method to read the messages out of the file and process them. The code looks like this:
try {
while(mMessageBuilder.mergeDelimitedFrom(mInput) == true) {
// process each message in turn
}
} catch (IOException e) {
Log.e(TAG, "unable to read from the input file", e);
return;
} finally {
try {
mInput.close();
} catch (IOException e) {
Log.e(TAG, "unable to close input file", e);
}
}
It took me some time this afternoon to work this out, including a large number of Google searches. I hope this post helps someone else who is working through similar issues.
The photo “Binary” was uploaded to Flickr by Fernando de Sousa and used under the terms of a Creative Commons License.




