Fixes #164 by porting from iOS

@march44 can you review and merge?

https://github.com/couchbase/couchbase-lite-java-core/issues/164