当前位置: 首页>>代码示例>>Java>>正文


Java HttpEntity.getContentLength方法代码示例

本文整理汇总了Java中org.apache.http.HttpEntity.getContentLength方法的典型用法代码示例。如果您正苦于以下问题:Java HttpEntity.getContentLength方法的具体用法?Java HttpEntity.getContentLength怎么用?Java HttpEntity.getContentLength使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.http.HttpEntity的用法示例。


在下文中一共展示了HttpEntity.getContentLength方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: BufferedHttpEntity

import org.apache.http.HttpEntity; //导入方法依赖的package包/类
/**
 * Creates a new buffered entity wrapper.
 *
 * @param entity   the entity to wrap, not null
 * @throws IllegalArgumentException if wrapped is null
 */
public BufferedHttpEntity(final HttpEntity entity) throws IOException {
    super(entity);
    if (!entity.isRepeatable() || entity.getContentLength() < 0) {
        this.buffer = EntityUtils.toByteArray(entity);
    } else {
        this.buffer = null;
    }
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:15,代码来源:BufferedHttpEntity.java

示例2: callUrl

import org.apache.http.HttpEntity; //导入方法依赖的package包/类
/**
 * 
 * @param url
 * @return
 * @throws IOException
 */
public static String callUrl(String url) throws IOException {
    HttpGet httpGet = new HttpGet(url);
    RequestConfig defaultRequestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000).setConnectionRequestTimeout(
            10000).build();
    try (CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build()) {
        try (CloseableHttpResponse response = httpClient.execute(httpGet); StringWriter writer = new StringWriter()) {
            switch (response.getStatusLine().getStatusCode()) {
                case 200:
                    HttpEntity httpEntity = response.getEntity();
                    httpEntity.getContentLength();
                    IOUtils.copy(httpEntity.getContent(), writer);
                    return writer.toString();
                default:
                    logger.error("Could not open URL '{}': {}", url, response.getStatusLine().getReasonPhrase());
            }
        }
    } finally {
        httpGet.releaseConnection();
    }

    return null;
}
 
开发者ID:intranda,项目名称:goobi-viewer-indexer,代码行数:29,代码来源:Utils.java

示例3: getResponseData

import org.apache.http.HttpEntity; //导入方法依赖的package包/类
@Override
protected byte[] getResponseData(HttpEntity entity) throws IOException {
    if (entity != null) {
        InputStream instream = entity.getContent();
        long contentLength = entity.getContentLength();
        FileOutputStream buffer = new FileOutputStream(getTargetFile(), this.append);
        if (instream != null) {
            try {
                byte[] tmp = new byte[BUFFER_SIZE];
                int l, count = 0;
                // do not send messages if request has been cancelled
                while ((l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) {
                    count += l;
                    buffer.write(tmp, 0, l);
                    sendProgressMessage(count, (int) contentLength);
                }
            } finally {
                AsyncHttpClient.silentCloseInputStream(instream);
                buffer.flush();
                AsyncHttpClient.silentCloseOutputStream(buffer);
            }
        }
    }
    return null;
}
 
开发者ID:yiwent,项目名称:Mobike,代码行数:26,代码来源:FileAsyncHttpResponseHandler.java

示例4: getResponseData

import org.apache.http.HttpEntity; //导入方法依赖的package包/类
@Override
byte[] getResponseData(HttpEntity entity) throws IOException {
    if (entity != null) {
        InputStream instream = entity.getContent();
        long contentLength = entity.getContentLength();
        FileOutputStream buffer = new FileOutputStream(getTargetFile());
        if (instream != null) {
            try {
                byte[] tmp = new byte[BUFFER_SIZE];
                int l, count = 0;
                // do not send messages if request has been cancelled
                while ((l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) {
                    count += l;
                    buffer.write(tmp, 0, l);
                    sendProgressMessage(count, (int) contentLength);
                }
            } finally {
                instream.close();
                buffer.flush();
                buffer.close();
            }
        }
    }
    return null;
}
 
开发者ID:LanguidSheep,项目名称:sealtalk-android-master,代码行数:26,代码来源:FileAsyncHttpResponseHandler.java

示例5: getResponseData

import org.apache.http.HttpEntity; //导入方法依赖的package包/类
@Override
protected byte[] getResponseData(HttpEntity entity) throws IOException {
    if (entity != null) {
        InputStream instream = entity.getContent();
        long contentLength = entity.getContentLength() + current;
        FileOutputStream buffer = new FileOutputStream(getTargetFile(), append);
        if (instream != null) {
            try {
                byte[] tmp = new byte[BUFFER_SIZE];
                int l;
                while (current < contentLength && (l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) {
                    current += l;
                    buffer.write(tmp, 0, l);
                    sendProgressMessage((int) current, (int) contentLength);
                }
            } finally {
                instream.close();
                buffer.flush();
                buffer.close();
            }
        }
    }
    return null;
}
 
开发者ID:yiwent,项目名称:Mobike,代码行数:25,代码来源:RangeFileAsyncHttpResponseHandler.java

示例6: process

import org.apache.http.HttpEntity; //导入方法依赖的package包/类
public void process(final HttpRequest request, final HttpContext context)
        throws HttpException, IOException {
    if (request == null) {
        throw new IllegalArgumentException("HTTP request may not be null");
    }
    if (request instanceof HttpEntityEnclosingRequest) {
        HttpEntity entity = ((HttpEntityEnclosingRequest)request).getEntity();
        // Do not send the expect header if request body is known to be empty
        if (entity != null && entity.getContentLength() != 0) {
            ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
            if (HttpProtocolParams.useExpectContinue(request.getParams())
                    && !ver.lessEquals(HttpVersion.HTTP_1_0)) {
                request.addHeader(HTTP.EXPECT_DIRECTIVE, HTTP.EXPECT_CONTINUE);
            }
        }
    }
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:18,代码来源:RequestExpectContinue.java

示例7: entityToBytes

import org.apache.http.HttpEntity; //导入方法依赖的package包/类
/** Reads the contents of HttpEntity into a byte[]. */
private byte[] entityToBytes(HttpEntity entity) throws IOException, ServerError {
    PoolingByteArrayOutputStream bytes =
            new PoolingByteArrayOutputStream(mPool, (int) entity.getContentLength());
    byte[] buffer = null;
    try {
        InputStream in = entity.getContent();
        if (in == null) {
            throw new ServerError();
        }
        buffer = mPool.getBuf(1024);
        int count;
        while ((count = in.read(buffer)) != -1) {
            bytes.write(buffer, 0, count);
        }
        return bytes.toByteArray();
    } finally {
        try {
            // Close the InputStream and release the resources by "consuming the content".
            entity.consumeContent();
        } catch (IOException e) {
            // This can happen if there was an exception above that left the entity in
            // an invalid state.
            VolleyLog.v("Error occured when calling consumingContent");
        }
        mPool.returnBuf(buffer);
        bytes.close();
    }
}
 
开发者ID:wangzhaosheng,项目名称:publicProject,代码行数:30,代码来源:BasicNetwork.java

示例8: toFeignBody

import org.apache.http.HttpEntity; //导入方法依赖的package包/类
Response.Body toFeignBody(HttpResponse httpResponse) throws IOException {
  final HttpEntity entity = httpResponse.getEntity();
  if (entity == null) {
    return null;
  }
  return new Response.Body() {

    @Override
    public Integer length() {
      return entity.getContentLength() >= 0 && entity.getContentLength() <= Integer.MAX_VALUE ?
              (int) entity.getContentLength() : null;
    }

    @Override
    public boolean isRepeatable() {
      return entity.isRepeatable();
    }

    @Override
    public InputStream asInputStream() throws IOException {
      return entity.getContent();
    }

    @Override
    public Reader asReader() throws IOException {
      return new InputStreamReader(asInputStream(), UTF_8);
    }

    @Override
    public void close() throws IOException {
      EntityUtils.consume(entity);
    }
  };
}
 
开发者ID:wenwu315,项目名称:XXXX,代码行数:35,代码来源:ApacheHttpClient.java

示例9: getResponseData

import org.apache.http.HttpEntity; //导入方法依赖的package包/类
/**
 * Returns byte array of response HttpEntity contents
 *
 * @param entity can be null
 * @return response entity body or null
 * @throws IOException if reading entity or creating byte array failed
 */
@Override
byte[] getResponseData(HttpEntity entity) throws IOException {

    byte[] responseBody = null;
    if (entity != null) {
        InputStream instream = entity.getContent();
        if (instream != null) {
            long contentLength = entity.getContentLength();
            if (contentLength > Integer.MAX_VALUE) {
                throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
            }
            if (contentLength < 0) {
                contentLength = BUFFER_SIZE;
            }
            try {
                ByteArrayBuffer buffer = new ByteArrayBuffer((int) contentLength);
                try {
                    byte[] tmp = new byte[BUFFER_SIZE];
                    int l;
                    // do not send messages if request has been cancelled
                    while ((l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) {
                        buffer.append(tmp, 0, l);
                        sendProgressDataMessage(copyOfRange(tmp, 0, l));
                    }
                } finally {
                    AsyncHttpClient.silentCloseInputStream(instream);
                }
                responseBody = buffer.toByteArray();
            } catch (OutOfMemoryError e) {
                System.gc();
                throw new IOException("File too large to fit into available memory");
            }
        }
    }
    return responseBody;
}
 
开发者ID:yiwent,项目名称:Mobike,代码行数:44,代码来源:DataAsyncHttpResponseHandler.java

示例10: getResponseData

import org.apache.http.HttpEntity; //导入方法依赖的package包/类
byte[] getResponseData(HttpEntity entity) throws IOException {
    byte[] responseBody = null;
    if (entity != null) {
        InputStream instream = entity.getContent();
        if (instream != null) {
            long contentLength = entity.getContentLength();
            if (contentLength > Integer.MAX_VALUE) {
                throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
            }
            if (contentLength < 0) {
                contentLength = BUFFER_SIZE;
            }
            try {
                ByteArrayBuffer buffer = new ByteArrayBuffer((int) contentLength);
                try {
                    byte[] tmp = new byte[BUFFER_SIZE];
                    int l, count = 0;
                    // do not send messages if request has been cancelled
                    while ((l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) {
                        count += l;
                        buffer.append(tmp, 0, l);
                        sendProgressMessage(count, (int) contentLength);
                    }
                } finally {
                    instream.close();
                }
                responseBody = buffer.toByteArray();
            } catch (OutOfMemoryError e) {
                System.gc();
                throw new IOException("File too large to fit into available memory");
            }
        }
    }
    return responseBody;
}
 
开发者ID:zqHero,项目名称:rongyunDemo,代码行数:36,代码来源:AsyncHttpResponseHandler.java

示例11: getResponseData

import org.apache.http.HttpEntity; //导入方法依赖的package包/类
/**
 * Returns byte array of response HttpEntity contents
 *
 * @param entity can be null
 * @return response entity body or null
 * @throws java.io.IOException if reading entity or creating byte array failed
 */
@Override
byte[] getResponseData(HttpEntity entity) throws IOException {

    byte[] responseBody = null;
    if (entity != null) {
        InputStream instream = entity.getContent();
        if (instream != null) {
            long contentLength = entity.getContentLength();
            if (contentLength > Integer.MAX_VALUE) {
                throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
            }
            if (contentLength < 0) {
                contentLength = BUFFER_SIZE;
            }
            try {
                ByteArrayBuffer buffer = new ByteArrayBuffer((int) contentLength);
                try {
                    byte[] tmp = new byte[BUFFER_SIZE];
                    int l;
                    // do not send messages if request has been cancelled
                    while ((l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) {
                        buffer.append(tmp, 0, l);
                        sendProgressDataMessage(copyOfRange(tmp, 0, l));
                    }
                } finally {
                    AsyncHttpClient.silentCloseInputStream(instream);
                }
                responseBody = buffer.toByteArray();
            } catch (OutOfMemoryError e) {
                System.gc();
                throw new IOException("File too large to fit into available memory");
            }
        }
    }
    return responseBody;
}
 
开发者ID:benniaobuguai,项目名称:android-project-gallery,代码行数:44,代码来源:DataAsyncHttpResponseHandler.java

示例12: entityToBytes

import org.apache.http.HttpEntity; //导入方法依赖的package包/类
/**
 * Reads the contents of HttpEntity into a byte[].
 */
private byte[] entityToBytes(HttpEntity entity) throws IOException, ServerError {
  PoolingByteArrayOutputStream bytes =
      new PoolingByteArrayOutputStream(mPool, (int) entity.getContentLength());
  byte[] buffer = null;
  try {
    InputStream in = entity.getContent();
    if (in == null) {
      throw new ServerError();
    }
    buffer = mPool.getBuf(1024);
    int count;
    while ((count = in.read(buffer)) != -1) {
      bytes.write(buffer, 0, count);
    }
    return bytes.toByteArray();
  } finally {
    try {
      // Close the InputStream and release the resources by "consuming the content".
      entity.consumeContent();
    } catch (IOException e) {
      // This can happen if there was an exception above that left the entity in
      // an invalid state.
      VolleyLog.v("Error occured when calling consumingContent");
    }
    mPool.returnBuf(buffer);
    bytes.close();
  }
}
 
开发者ID:nordfalk,项目名称:EsperantoRadio,代码行数:32,代码来源:DrBasicNetwork.java

示例13: toByteArray

import org.apache.http.HttpEntity; //导入方法依赖的package包/类
/**
 * Read the contents of an entity and return it as a byte array.
 *
 * @param entity
 * @return byte array containing the entity content. May be null if
 *   {@link HttpEntity#getContent()} is null.
 * @throws IOException if an error occurs reading the input stream
 * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE
 */
public static byte[] toByteArray(final HttpEntity entity) throws IOException {
    if (entity == null) {
        throw new IllegalArgumentException("HTTP entity may not be null");
    }
    InputStream instream = entity.getContent();
    if (instream == null) {
        return null;
    }
    try {
        if (entity.getContentLength() > Integer.MAX_VALUE) {
            throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
        }
        int i = (int)entity.getContentLength();
        if (i < 0) {
            i = 4096;
        }
        ByteArrayBuffer buffer = new ByteArrayBuffer(i);
        byte[] tmp = new byte[4096];
        int l;
        while((l = instream.read(tmp)) != -1) {
            buffer.append(tmp, 0, l);
        }
        return buffer.toByteArray();
    } finally {
        instream.close();
    }
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:37,代码来源:EntityUtils.java

示例14: getResponseData

import org.apache.http.HttpEntity; //导入方法依赖的package包/类
protected byte[] getResponseData(HttpEntity entity) throws IOException {
    if (entity != null) {
        InputStream instream = entity.getContent();
        long contentLength = entity.getContentLength() + this.current;
        FileOutputStream buffer = new FileOutputStream(getTargetFile(), this.append);
        if (instream != null) {
            try {
                byte[] tmp = new byte[4096];
                while (this.current < contentLength) {
                    int l = instream.read(tmp);
                    if (l == -1 || Thread.currentThread().isInterrupted()) {
                        break;
                    }
                    this.current += (long) l;
                    buffer.write(tmp, 0, l);
                    sendProgressMessage(this.current, contentLength);
                }
                instream.close();
                buffer.flush();
                buffer.close();
            } catch (Throwable th) {
                instream.close();
                buffer.flush();
                buffer.close();
            }
        }
    }
    return null;
}
 
开发者ID:JackChan1999,项目名称:boohee_v5.6,代码行数:30,代码来源:RangeFileAsyncHttpResponseHandler.java

示例15: getResponseData

import org.apache.http.HttpEntity; //导入方法依赖的package包/类
byte[] getResponseData(HttpEntity entity) throws IOException {
    byte[] responseBody = null;
    if (entity != null) {
        InputStream instream = entity.getContent();
        if (instream != null) {
            long contentLength = entity.getContentLength();
            if (contentLength > 2147483647L) {
                throw new IllegalArgumentException("HTTP entity too large to be buffered in " +
                        "memory");
            }
            try {
                ByteArrayBuffer buffer = new ByteArrayBuffer(contentLength <= 0 ? 4096 :
                        (int) contentLength);
                byte[] tmp = new byte[4096];
                long count = 0;
                while (true) {
                    int l = instream.read(tmp);
                    if (l == -1 || Thread.currentThread().isInterrupted()) {
                        break;
                    }
                    long j;
                    count += (long) l;
                    buffer.append(tmp, 0, l);
                    if (contentLength <= 0) {
                        j = 1;
                    } else {
                        j = contentLength;
                    }
                    sendProgressMessage(count, j);
                }
                AsyncHttpClient.silentCloseInputStream(instream);
                AsyncHttpClient.endEntityViaReflection(entity);
                responseBody = buffer.toByteArray();
            } catch (OutOfMemoryError e) {
                System.gc();
                throw new IOException("File too large to fit into available memory");
            } catch (Throwable th) {
                AsyncHttpClient.silentCloseInputStream(instream);
                AsyncHttpClient.endEntityViaReflection(entity);
            }
        }
    }
    return responseBody;
}
 
开发者ID:JackChan1999,项目名称:boohee_v5.6,代码行数:45,代码来源:AsyncHttpResponseHandler.java


注:本文中的org.apache.http.HttpEntity.getContentLength方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。