Search in sources :

Example 1 with ContentBody

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);
}
Also used : InterruptedIOException(java.io.InterruptedIOException) ContentBody(com.lidroid.xutils.http.client.multipart.content.ContentBody) ByteArrayBuffer(org.apache.http.util.ByteArrayBuffer)

Example 2 with ContentBody

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;
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpEntity(org.apache.http.HttpEntity) ContentBody(com.lidroid.xutils.http.client.multipart.content.ContentBody) MultipartEntity(com.lidroid.xutils.http.client.multipart.MultipartEntity) StringBody(com.lidroid.xutils.http.client.multipart.content.StringBody) BodyParamsEntity(com.lidroid.xutils.http.client.entity.BodyParamsEntity) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 3 with ContentBody

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;
    }
}
Also used : ContentBody(com.lidroid.xutils.http.client.multipart.content.ContentBody) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

ContentBody (com.lidroid.xutils.http.client.multipart.content.ContentBody)3 BodyParamsEntity (com.lidroid.xutils.http.client.entity.BodyParamsEntity)1 MultipartEntity (com.lidroid.xutils.http.client.multipart.MultipartEntity)1 StringBody (com.lidroid.xutils.http.client.multipart.content.StringBody)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InterruptedIOException (java.io.InterruptedIOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 HttpEntity (org.apache.http.HttpEntity)1 NameValuePair (org.apache.http.NameValuePair)1 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)1 ByteArrayBuffer (org.apache.http.util.ByteArrayBuffer)1