Java Code Examples for org.apache.http.util.ByteArrayBuffer#length()

The following examples show how to use org.apache.http.util.ByteArrayBuffer#length() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpProtocol.java    From storm-crawler with Apache License 2.0 5 votes vote down vote up
private static final byte[] toByteArray(final HttpEntity entity,
        int maxContent, MutableBoolean trimmed) throws IOException {

    if (entity == null)
        return new byte[] {};

    final InputStream instream = entity.getContent();
    if (instream == null) {
        return null;
    }
    Args.check(entity.getContentLength() <= Integer.MAX_VALUE,
            "HTTP entity too large to be buffered in memory");
    int reportedLength = (int) entity.getContentLength();
    // set default size for buffer: 100 KB
    int bufferInitSize = 102400;
    if (reportedLength != -1) {
        bufferInitSize = reportedLength;
    }
    // avoid init of too large a buffer when we will trim anyway
    if (maxContent != -1 && bufferInitSize > maxContent) {
        bufferInitSize = maxContent;
    }
    final ByteArrayBuffer buffer = new ByteArrayBuffer(bufferInitSize);
    final byte[] tmp = new byte[4096];
    int lengthRead;
    while ((lengthRead = instream.read(tmp)) != -1) {
        // check whether we need to trim
        if (maxContent != -1 && buffer.length() + lengthRead > maxContent) {
            buffer.append(tmp, 0, maxContent - buffer.length());
            trimmed.setValue(true);
            break;
        }
        buffer.append(tmp, 0, lengthRead);
    }
    return buffer.toByteArray();
}
 
Example 2
Source File: HttpMultipart.java    From BigApp_Discuz_Android with Apache License 2.0 4 votes vote down vote up
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);
}
 
Example 3
Source File: HttpMultipart.java    From android-open-project-demo with Apache License 2.0 4 votes vote down vote up
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);
}