當前位置: 首頁>>代碼示例>>Java>>正文


Java ByteArrayBuffer.length方法代碼示例

本文整理匯總了Java中org.apache.http.util.ByteArrayBuffer.length方法的典型用法代碼示例。如果您正苦於以下問題:Java ByteArrayBuffer.length方法的具體用法?Java ByteArrayBuffer.length怎麽用?Java ByteArrayBuffer.length使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.http.util.ByteArrayBuffer的用法示例。


在下文中一共展示了ByteArrayBuffer.length方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: toByteArray

import org.apache.http.util.ByteArrayBuffer; //導入方法依賴的package包/類
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();
}
 
開發者ID:DigitalPebble,項目名稱:storm-crawler,代碼行數:37,代碼來源:HttpProtocol.java

示例2: doWriteTo

import org.apache.http.util.ByteArrayBuffer; //導入方法依賴的package包/類
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("stop");
        }
        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);
}
 
開發者ID:SavorGit,項目名稱:Hotspot-master-devp,代碼行數:70,代碼來源:HttpMultipart.java

示例3: doWriteTo

import org.apache.http.util.ByteArrayBuffer; //導入方法依賴的package包/類
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);
}
 
開發者ID:BigAppOS,項目名稱:BigApp_Discuz_Android,代碼行數:70,代碼來源:HttpMultipart.java

示例4: toByteArray

import org.apache.http.util.ByteArrayBuffer; //導入方法依賴的package包/類
private final byte[] toByteArray(final ResponseBody responseBody,
        MutableBoolean trimmed) throws IOException {

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

    final InputStream instream = responseBody.byteStream();
    if (instream == null) {
        return null;
    }
    if (responseBody.contentLength() > Integer.MAX_VALUE) {
        throw new IOException(
                "Cannot buffer entire body for content length: "
                        + responseBody.contentLength());
    }
    int reportedLength = (int) responseBody.contentLength();
    // 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;
    }
    long endDueFor = -1;
    if (completionTimeout != -1) {
        endDueFor = System.currentTimeMillis() + (completionTimeout * 1000);
    }
    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);
        // check whether we hit the completion timeout
        if (endDueFor != -1 && endDueFor <= System.currentTimeMillis()) {
            trimmed.setValue(true);
            break;
        }
    }
    return buffer.toByteArray();
}
 
開發者ID:DigitalPebble,項目名稱:storm-crawler,代碼行數:49,代碼來源:HttpProtocol.java


注:本文中的org.apache.http.util.ByteArrayBuffer.length方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。