@ InterfaceAudience . Private protected static Map < String , Object > installAttachmentBodies ( Map < String , Object > attachments , Database database ) throws CouchbaseLiteException { Map < String , Object > updatedAttachments = new HashMap < String , Object > ( ) ; for ( String name : attachments . keySet ( ) ) { Object value = attachments . get ( name ) ; if ( value instanceof Attachment ) { Attachment attachment = ( Attachment ) value ; Map < String , Object > metadataMutable = new HashMap < String , Object > ( ) ; metadataMutable . putAll ( attachment . getMetadata ( ) ) ; InputStream body = attachment . getBodyIfNew ( ) ; if ( body != null ) { // Copy attachment body into the database's blob store: BlobStoreWriter writer ; try { writer = blobStoreWriterForBody ( body , database ) ; } catch ( Exception e ) { throw new CouchbaseLiteException ( e . getMessage ( ) , Status . ATTACHMENT_ERROR ) ; } metadataMutable . put ( "length" , writer . getLength ( ) ) ; metadataMutable . put ( "digest" , writer . mD5DigestString ( ) ) ; metadataMutable . put ( "follows" , true ) ; database . rememberAttachmentWriter ( writer ) ; } updatedAttachments . put ( name , metadataMutable ) ; } else if ( value instanceof AttachmentInternal ) { throw new IllegalArgumentException ( "AttachmentInternal objects not expected here. Could indicate a bug" ) ; } else if ( value != null ) { updatedAttachments . put ( name , value ) ; } } return updatedAttachments ; }
Goes through an _attachments dictionary and replaces any values that are Attachment objects with proper JSON metadata dicts . It registers the attachment bodies with the blob store and sets the metadata getDigest and follows properties accordingly .