public class MultipartAttachmentWriter
extends java.lang.Object
A class for writing MIME Multipart/Related (RFC 2387) streams to CouchDB. Used to send attachments inline with document revisions without incurring the overhead of base64 encoding.
Use as follows:
DocumentRevision myDocument;
List<Attachment> myAttachments;
CouchClient myClient;
...
MultipartAttachmentWriter mpw = new MultipartAttachmentWriter();
mpw.setRequestBody(myDocument);
for (Attachment a : myAttachments) {
mpw.addAttachment(a);
}
// don't forget to call close when we're done adding attachments
mpw.close();
// do a PUT to couch
myClient.putMultipart(mpw);
The stream consists of a first MIME body which is the JSON document itself, which needs to have
the _attachments
object correctly populated. This is currently done by
RevisionHistoryHelper.addAttachments(java.util.List, java.util.Map, com.cloudant.sync.replication.PushAttachmentsInline, int)
Attachments which are included in subsequent MIME bodies should have follows
set to
true
in the _attachments
object.
CouchClient.putMultipart(MultipartAttachmentWriter)
,
RevisionHistoryHelper.addAttachments(java.util.List, java.util.Map, com.cloudant.sync.replication.PushAttachmentsInline, int)
,
Creating Multiple Attachments,
RFC 2387Constructor and Description |
---|
MultipartAttachmentWriter()
Construct a
MultipartAttachmentWriter with a default boundary |
MultipartAttachmentWriter(java.lang.String boundary)
Construct a
MultipartAttachmentWriter with a specific boundary |
Modifier and Type | Method and Description |
---|---|
void |
addAttachment(Attachment attachment,
long length)
Add an attachment to be streamed as a subsequent MIME body.
|
java.lang.String |
getBoundary() |
long |
getContentLength() |
java.lang.String |
getId() |
java.io.InputStream |
makeInputStream() |
com.cloudant.http.HttpConnection.InputStreamGenerator |
makeInputStreamGenerator() |
void |
setBody(java.util.Map<java.lang.String,java.lang.Object> body)
Set the JSON body (the first MIME body) for the writer.
|
java.lang.String |
toString() |
public MultipartAttachmentWriter()
MultipartAttachmentWriter
with a default boundary
makeBoundary()
public MultipartAttachmentWriter(java.lang.String boundary)
MultipartAttachmentWriter
with a specific boundary
boundary
- The boundary sequence used to delimit each part. Must not occur anywhere in
the data of any part.public void setBody(java.util.Map<java.lang.String,java.lang.Object> body)
body
- The DocumentRevision to be serialised as JSONpublic void addAttachment(Attachment attachment, long length) throws java.io.IOException
attachment
- The attachment to be streamedlength
- Size in bytes of attachment, as it will be transmitted over the network
(that is, after any encoding)java.io.IOException
- if there was an error getting the input stream from the Attachment
public java.lang.String getBoundary()
public long getContentLength()
content-length
header when performing an HTTP PUT.public java.lang.String getId()
public java.lang.String toString()
toString
in class java.lang.Object
public com.cloudant.http.HttpConnection.InputStreamGenerator makeInputStreamGenerator()
public java.io.InputStream makeInputStream()