本文整理汇总了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;
}
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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);
}
}
}
}
示例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();
}
}
示例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);
}
};
}
示例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;
}
示例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;
}
示例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;
}
示例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();
}
}
示例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();
}
}
示例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;
}
示例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;
}