当前位置: 首页>>代码示例>>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;未经允许,请勿转载。