Java Code Examples for org.apache.http.HttpEntity#getContentLength()
The following examples show how to use
org.apache.http.HttpEntity#getContentLength() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileAsyncHttpResponseHandler.java From Android-Basics-Codes with Artistic License 2.0 | 6 votes |
@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; }
Example 2
Source File: RestFunctions.java From metron with Apache License 2.0 | 6 votes |
/** * 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; }
Example 3
Source File: HttpService.java From arcusplatform with Apache License 2.0 | 6 votes |
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; }
Example 4
Source File: StringEntityHandler.java From Android-Basics-Codes with Artistic License 2.0 | 6 votes |
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); }
Example 5
Source File: HttpProtocol.java From storm-crawler with Apache License 2.0 | 5 votes |
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(); }
Example 6
Source File: HttpClient4EntityExtractor.java From pinpoint with Apache License 2.0 | 5 votes |
/** * 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; }
Example 7
Source File: HttpResponseUtils.java From esigate with Apache License 2.0 | 5 votes |
/** * 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; } } } }
Example 8
Source File: BasicNetwork.java From CrossBow with Apache License 2.0 | 5 votes |
/** 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(); } }
Example 9
Source File: BasicNetwork.java From SimplifyReader with Apache License 2.0 | 5 votes |
/** 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(); } }
Example 10
Source File: JacksonNetwork.java From volley-jackson-extension with Apache License 2.0 | 5 votes |
/** * 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(); } }
Example 11
Source File: BasicNetwork.java From pearl with Apache License 2.0 | 5 votes |
/** 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(); } }
Example 12
Source File: DataAsyncHttpResponseHandler.java From Android-Basics-Codes with Artistic License 2.0 | 5 votes |
/** * 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; }
Example 13
Source File: ReadLogString.java From docker-java-api with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * 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; }
Example 14
Source File: BasicNetwork.java From device-database with Apache License 2.0 | 5 votes |
/** 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(); } }
Example 15
Source File: RVNetwork.java From RestVolley with Apache License 2.0 | 5 votes |
/** 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(); } }
Example 16
Source File: GoogleApacheHttpResponse.java From PYX-Reloaded with Apache License 2.0 | 4 votes |
@Override public long getContentLength() { HttpEntity entity = response.getEntity(); return entity == null ? -1 : entity.getContentLength(); }
Example 17
Source File: Dictionary.java From ongdb-lab-apoc with Apache License 2.0 | 4 votes |
/** * 从远程服务器上下载自定义词条 */ 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; }
Example 18
Source File: PlanningCenterOnlineParser.java From Quelea with GNU General Public License v3.0 | 4 votes |
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 ""; }
Example 19
Source File: HttpConnnectionManager.java From Crawer with MIT License | 2 votes |
/** * * 从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; }
Example 20
Source File: DecompressingEntity.java From android-open-project-demo with Apache License 2.0 | 2 votes |
/** * 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(); }