use of com.lidroid.xutils.http.client.multipart.content.ContentBody in project xUtils by wyouflf.
the class HttpMultipart method doWriteTo.
private void doWriteTo(final HttpMultipartMode mode, final OutputStream out, MultipartEntity.CallBackInfo callBackInfo, boolean writeContent) throws IOException {
callBackInfo.pos = 0;
ByteArrayBuffer boundary = encode(this.charset, getBoundary());
for (FormBodyPart part : this.parts) {
if (!callBackInfo.doCallBack(true)) {
throw new InterruptedIOException("cancel");
}
writeBytes(TWO_DASHES, out);
callBackInfo.pos += TWO_DASHES.length();
writeBytes(boundary, out);
callBackInfo.pos += boundary.length();
writeBytes(CR_LF, out);
callBackInfo.pos += CR_LF.length();
MinimalFieldHeader header = part.getHeader();
switch(mode) {
case STRICT:
for (MinimalField field : header) {
writeField(field, out);
callBackInfo.pos += encode(MIME.DEFAULT_CHARSET, field.getName() + field.getBody()).length() + FIELD_SEP.length() + CR_LF.length();
}
break;
case BROWSER_COMPATIBLE:
// Only write Content-Disposition
// Use content charset
MinimalField cd = header.getField(MIME.CONTENT_DISPOSITION);
writeField(cd, this.charset, out);
callBackInfo.pos += encode(this.charset, cd.getName() + cd.getBody()).length() + FIELD_SEP.length() + CR_LF.length();
String filename = part.getBody().getFilename();
if (filename != null) {
MinimalField ct = header.getField(MIME.CONTENT_TYPE);
writeField(ct, this.charset, out);
callBackInfo.pos += encode(this.charset, ct.getName() + ct.getBody()).length() + FIELD_SEP.length() + CR_LF.length();
}
break;
default:
break;
}
writeBytes(CR_LF, out);
callBackInfo.pos += CR_LF.length();
if (writeContent) {
ContentBody body = part.getBody();
body.setCallBackInfo(callBackInfo);
body.writeTo(out);
}
writeBytes(CR_LF, out);
callBackInfo.pos += CR_LF.length();
}
writeBytes(TWO_DASHES, out);
callBackInfo.pos += TWO_DASHES.length();
writeBytes(boundary, out);
callBackInfo.pos += boundary.length();
writeBytes(TWO_DASHES, out);
callBackInfo.pos += TWO_DASHES.length();
writeBytes(CR_LF, out);
callBackInfo.pos += CR_LF.length();
callBackInfo.doCallBack(true);
}
use of com.lidroid.xutils.http.client.multipart.content.ContentBody in project xUtils by wyouflf.
the class RequestParams method getEntity.
/**
* Returns an HttpEntity containing all request parameters
*/
public HttpEntity getEntity() {
if (bodyEntity != null) {
return bodyEntity;
}
HttpEntity result = null;
if (fileParams != null && !fileParams.isEmpty()) {
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.STRICT, null, Charset.forName(charset));
if (bodyParams != null && !bodyParams.isEmpty()) {
for (NameValuePair param : bodyParams) {
try {
multipartEntity.addPart(param.getName(), new StringBody(param.getValue()));
} catch (UnsupportedEncodingException e) {
LogUtils.e(e.getMessage(), e);
}
}
}
for (ConcurrentHashMap.Entry<String, ContentBody> entry : fileParams.entrySet()) {
multipartEntity.addPart(entry.getKey(), entry.getValue());
}
result = multipartEntity;
} else if (bodyParams != null && !bodyParams.isEmpty()) {
result = new BodyParamsEntity(bodyParams, charset);
}
return result;
}
use of com.lidroid.xutils.http.client.multipart.content.ContentBody in project xUtils by wyouflf.
the class HttpMultipart method getTotalLength.
/**
* Determines the total length of the multipart content (content length of
* individual parts plus that of extra elements required to delimit the parts
* from one another). If any of the @{link BodyPart}s contained in this object
* is of a streaming entity of unknown length the total length is also unknown.
* <p/>
* This method buffers only a small amount of data in order to determine the
* total length of the entire entity. The content of individual parts is not
* buffered.
*
* @return total length of the multipart entity if known, <code>-1</code>
* otherwise.
*/
public long getTotalLength() {
long contentLen = 0;
for (FormBodyPart part : this.parts) {
ContentBody body = part.getBody();
long len = body.getContentLength();
if (len >= 0) {
contentLen += len;
} else {
return -1;
}
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
doWriteTo(this.mode, out, false);
byte[] extra = out.toByteArray();
return contentLen + extra.length;
} catch (Throwable ex) {
// Should never happen
return -1;
}
}
Aggregations