org.apache.http.HttpEntity#getContentLength ( )源码实例Demo

下面列出了org.apache.http.HttpEntity#getContentLength ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@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;
}
 
源代码2 项目: metron   文件: RestFunctions.java
/**
 * Parses the Http response into a Map and checks for content length.
 * @param restConfig
 * @param httpUriRequest
 * @param httpEntity
 * @return
 * @throws IOException
 */
protected static Optional<Object> parseResponse(RestConfig restConfig, HttpUriRequest httpUriRequest, HttpEntity httpEntity) throws IOException {
  Optional<Object> parsedResponse = Optional.empty();
  if (httpEntity != null) {
    int actualContentLength = 0;
    String json = EntityUtils.toString(httpEntity);
    if (json != null && !json.isEmpty()) {
      actualContentLength = json.length();
      parsedResponse = Optional.of(JSONUtils.INSTANCE.load(json, JSONUtils.MAP_SUPPLIER));
    }
    if (restConfig.verifyContentLength() && actualContentLength != httpEntity.getContentLength()) {
      throw new IOException(String.format("Stellar REST request to %s returned incorrect or missing content length. " +
                      "Content length in the response was %d but the actual body content length was %d.",
              httpUriRequest.getURI().toString(),
              httpEntity.getContentLength(),
              actualContentLength));
    }
  }
  return parsedResponse;
}
 
源代码3 项目: arcusplatform   文件: HttpService.java
public static File download(URI uri, Credentials auth, File dst, ProgressMonitor<Long> monitor) throws IOException {
   CloseableHttpResponse rsp = get(uri, auth);
   try {
      if (rsp.getStatusLine().getStatusCode() != 200) {
         throw new IOException("http request failed: " + rsp.getStatusLine().getStatusCode());
      }

      HttpEntity entity = rsp.getEntity();
      long length = entity.getContentLength();

      try (InputStream is = entity.getContent();
           OutputStream os = new BufferedOutputStream(new FileOutputStream(dst))) {
         copy(is, os, length, monitor);
      }
      EntityUtils.consume(entity);
   } finally {
      rsp.close();
   }
   return dst;
}
 
public Object handleEntity(HttpEntity entity, EntityCallBack callback,String charset)throws IOException {
	if (entity == null)
		return null;
	
	ByteArrayOutputStream outStream = new ByteArrayOutputStream();
	byte[] buffer = new byte[1024];
	
	long count = entity.getContentLength();
	long curCount = 0;
	int len = -1;
	InputStream is = entity.getContent();
	while ((len = is.read(buffer)) != -1) {
		outStream.write(buffer, 0, len);
		curCount += len;
		if(callback!=null)
			callback.callBack(count, curCount,false);
	}
	if(callback!=null)
		callback.callBack(count, curCount,true);
	byte[] data = outStream.toByteArray();
	outStream.close();
	is.close();
	return new String(data,charset);
}
 
源代码5 项目: storm-crawler   文件: HttpProtocol.java
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();
}
 
源代码6 项目: pinpoint   文件: HttpClient4EntityExtractor.java
/**
 * copy: EntityUtils Get the entity content as a String, using the provided default character set if none is found in the entity. If defaultCharset is null, the default "ISO-8859-1" is used.
 *
 * @param entity         must not be null
 * @param defaultCharset character set to be applied if none found in the entity
 * @return the entity content as a String. May be null if {@link HttpEntity#getContent()} is null.
 * @throws ParseException           if header elements cannot be parsed
 * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE
 * @throws IOException              if an error occurs reading the input stream
 */
@SuppressWarnings("deprecation")
private static String entityUtilsToString(final HttpEntity entity, final String defaultCharset, final int maxLength) throws Exception {
    if (entity == null) {
        throw new IllegalArgumentException("HTTP entity must not be null");
    }
    if (entity.getContentLength() > Integer.MAX_VALUE) {
        return "HTTP entity is too large to be buffered in memory length:" + entity.getContentLength();
    }
    if (entity.getContentType().getValue().startsWith("multipart/form-data")) {
        return "content type is multipart/form-data. content length:" + entity.getContentLength();
    }

    String charset = getContentCharSet(entity);
    if (charset == null) {
        charset = defaultCharset;
    }
    if (charset == null) {
        charset = HTTP.DEFAULT_CONTENT_CHARSET;
    }

    final FixedByteArrayOutputStream outStream = new FixedByteArrayOutputStream(maxLength);
    entity.writeTo(outStream);
    final String entityValue = outStream.toString(charset);
    if (entity.getContentLength() > maxLength) {
        final StringBuilder sb = new StringBuilder();
        sb.append(entityValue);
        sb.append("HTTP entity large length: ");
        sb.append(entity.getContentLength());
        sb.append(" )");
        return sb.toString();
    }

    return entityValue;
}
 
源代码7 项目: esigate   文件: HttpResponseUtils.java
/**
 * Copied from org.apache.http.entity.InputStreamEntity.writeTo(OutputStream) method but flushes the buffer after
 * each read in order to allow streaming and web sockets.
 * 
 * @param httpEntity
 *            The entity to copy to the OutputStream
 * @param outstream
 *            The OutputStream
 * @throws IOException
 *             If a problem occurs
 */
public static void writeTo(final HttpEntity httpEntity, final OutputStream outstream) throws IOException {
    Args.notNull(outstream, "Output stream");
    try (InputStream instream = httpEntity.getContent()) {
        final byte[] buffer = new byte[OUTPUT_BUFFER_SIZE];
        int l;
        if (httpEntity.getContentLength() < 0) {
            // consume until EOF
            while ((l = instream.read(buffer)) != -1) {
                outstream.write(buffer, 0, l);
                outstream.flush();
                LOG.debug("Flushed {} bytes of data");
            }
        } else {
            // consume no more than length
            long remaining = httpEntity.getContentLength();
            while (remaining > 0) {
                l = instream.read(buffer, 0, (int) Math.min(OUTPUT_BUFFER_SIZE, remaining));
                if (l == -1) {
                    break;
                }
                outstream.write(buffer, 0, l);
                outstream.flush();
                LOG.debug("Flushed {} bytes of data");
                remaining -= l;
            }
        }
    }
}
 
源代码8 项目: CrossBow   文件: BasicNetwork.java
/** 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();
    }
}
 
源代码9 项目: SimplifyReader   文件: BasicNetwork.java
/** 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();
    }
}
 
源代码10 项目: volley-jackson-extension   文件: JacksonNetwork.java
/**
 * Copied from {@link com.android.volley.toolbox.BasicNetwork}
 *
 * 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();
	}
}
 
源代码11 项目: docker-java-api   文件: ReadLogString.java
/**
 * Check that content length less then Integer.MAX_VALUE.
 * Try to get content length from entity
 * If length less then zero return 4096
 *
 * @param entity HttpEntity for get capacity.
 * @return Capacity.
 */
private int getCapacity(final HttpEntity entity) {
    Args.check(entity.getContentLength() <= Integer.MAX_VALUE,
            "HTTP entity too large to be buffered in memory");
    int capacity = (int) entity.getContentLength();
    if (capacity < 0) {
        capacity = 4096;
    }
    return capacity;
}
 
源代码12 项目: RestVolley   文件: RVNetwork.java
/** 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 项目: device-database   文件: BasicNetwork.java
/** 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();
    }
}
 
/**
 * 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, count = 0;
                    // 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));
                        sendProgressMessage(count, (int) contentLength);
                    }
                } 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;
}
 
源代码15 项目: pearl   文件: BasicNetwork.java
/** 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();
    }
}
 
源代码16 项目: Quelea   文件: PlanningCenterOnlineParser.java
public String downloadFile(Media media, Attachment attachment, String fileName, ProgressBar progressBar, LocalDateTime lastUpdated) {
    try {
        QueleaProperties props = QueleaProperties.get();
        String fullFileName = FilenameUtils.concat(props.getDownloadPath(), fileName);
        File file = new File(fullFileName);
        if (file.exists()) {
            long lastModified = file.lastModified();
            if (lastUpdated == null || lastUpdated.atZone(ZoneOffset.UTC).toInstant().toEpochMilli() <= lastModified) {
                LOGGER.log(Level.INFO, "{0} exists, using existing file", file.getAbsolutePath());
                return file.getAbsolutePath();
            }

            // file is going to get overridden as it failed the timestamp check
            if (!file.delete()) {
                // deletion of exiting file failed! just use the existing file then
                LOGGER.log(Level.INFO, "Couldn''t delete existing file: {0}", file.getAbsolutePath());
                return file.getAbsolutePath();
            }
        }

        String partFullFileName = fullFileName + ".part";
        File partFile = new File(partFullFileName);

        AttachmentActivity attachmentActivity = planningCenterClient.services().media(media.getId()).attachment(attachment.getId()).api().open().execute().body().get();
        HttpResponse response = httpClient.execute(new HttpGet(attachmentActivity.getAttachmentUrl()));
        HttpEntity entity = response.getEntity();
        if (entity != null) {

            long contentLength = entity.getContentLength();

            InputStream is = entity.getContent();
            try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(partFile))) {
                Long totalBytesRead = 0L;

                byte buffer[] = new byte[1024 * 1024];
                int count;
                while ((count = is.read(buffer)) != -1) {
                    bos.write(buffer, 0, count);

                    totalBytesRead += count;
                    progressBar.setProgress((double) totalBytesRead / (double) contentLength);
                }
            }

            EntityUtils.consume(entity);
        }

        boolean success = partFile.renameTo(file);
        if (success && lastUpdated != null) {
            file.setLastModified(lastUpdated.atZone(ZoneOffset.UTC).toInstant().toEpochMilli()); // set file timestamp to same as on PCO
        }
        return file.getAbsolutePath();
    } catch (Exception e) {
        LOGGER.log(Level.WARNING, "Error", e);
    }

    return "";
}
 
源代码17 项目: ongdb-lab-apoc   文件: Dictionary.java
/**
 * 从远程服务器上下载自定义词条
 */
private static List<String> getRemoteWordsUnprivileged(String location) {

    List<String> buffer = new ArrayList<String>();
    RequestConfig rc = RequestConfig.custom().setConnectionRequestTimeout(10 * 1000).setConnectTimeout(10 * 1000)
            .setSocketTimeout(60 * 1000).build();
    CloseableHttpClient httpclient = HttpClients.createDefault();
    CloseableHttpResponse response;
    BufferedReader in;
    HttpGet get = new HttpGet(location);
    get.setConfig(rc);
    try {
        response = httpclient.execute(get);
        if (response.getStatusLine().getStatusCode() == 200) {

            String charset = "UTF-8";
            // 获取编码,默认为utf-8
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                Header contentType = entity.getContentType();
                if (contentType != null && contentType.getValue() != null) {
                    String typeValue = contentType.getValue();
                    if (typeValue != null && typeValue.contains("charset=")) {
                        charset = typeValue.substring(typeValue.lastIndexOf("=") + 1);
                    }
                }

                if (entity.getContentLength() > 0) {
                    in = new BufferedReader(new InputStreamReader(entity.getContent(), charset));
                    String line;
                    while ((line = in.readLine()) != null) {
                        buffer.add(line);
                    }
                    in.close();
                    response.close();
                    return buffer;
                }
            }
        }
        response.close();
    } catch (IllegalStateException | IOException e) {
        logger.error("getRemoteWords " + location + " error", e);
    }
    return buffer;
}
 
源代码18 项目: PYX-Reloaded   文件: GoogleApacheHttpResponse.java
@Override
public long getContentLength() {
    HttpEntity entity = response.getEntity();
    return entity == null ? -1 : entity.getContentLength();
}
 
/**
 * Creates a new {@link DecompressingEntity}.
 *
 * @param wrapped the non-null {@link org.apache.http.HttpEntity} to be wrapped
 */
public DecompressingEntity(final HttpEntity wrapped) {
    super(wrapped);
    this.uncompressedLength = wrapped.getContentLength();
}
 
源代码20 项目: Crawer   文件: HttpConnnectionManager.java
/**
 * 
 * 从response返回的实体中读取页面代码
 * 
 * @param httpEntity
 *            Http实体
 * 
 * @return 页面代码
 * 
 * @throws ParseException
 * 
 * @throws IOException
 */

private static String readHtmlContentFromEntity(HttpEntity httpEntity)
		throws ParseException, IOException {

	String html = "";

	Header header = httpEntity.getContentEncoding();

	if (httpEntity.getContentLength() < 2147483647L) { // EntityUtils无法处理ContentLength超过2147483647L的Entity

		if (header != null && "gzip".equals(header.getValue())) {

			html = EntityUtils.toString(new GzipDecompressingEntity(
					httpEntity));

		} else {

			html = EntityUtils.toString(httpEntity);

		}

	} else {

		InputStream in = httpEntity.getContent();

		if (header != null && "gzip".equals(header.getValue())) {

			html = unZip(in, ContentType.getOrDefault(httpEntity)
					.getCharset().toString());

		} else {

			html = readInStreamToString(in,
					ContentType.getOrDefault(httpEntity).getCharset()
							.toString());

		}

		if (in != null) {

			in.close();

		}

	}

	return html;

}