Java Code Examples for org.apache.http.HttpEntity#getContentLength()

The following examples show how to use org.apache.http.HttpEntity#getContentLength() . These examples are extracted from open source projects. 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
@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 Project: metron   File: RestFunctions.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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 Project: arcusplatform   File: HttpService.java    License: Apache License 2.0 6 votes vote down vote up
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
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 Project: storm-crawler   File: HttpProtocol.java    License: Apache License 2.0 5 votes vote down vote up
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
/**
 * 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 Project: esigate   File: HttpResponseUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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 Project: CrossBow   File: BasicNetwork.java    License: Apache License 2.0 5 votes vote down vote up
/** 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 Project: SimplifyReader   File: BasicNetwork.java    License: Apache License 2.0 5 votes vote down vote up
/** 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
/**
 * 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
/**
 * 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 12
Source Project: RestVolley   File: RVNetwork.java    License: Apache License 2.0 5 votes vote down vote up
/** 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 13
Source Project: device-database   File: BasicNetwork.java    License: Apache License 2.0 5 votes vote down vote up
/** 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 14
/**
 * 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 15
Source Project: pearl   File: BasicNetwork.java    License: Apache License 2.0 5 votes vote down vote up
/** 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
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 17
Source Project: ongdb-lab-apoc   File: Dictionary.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * 从远程服务器上下载自定义词条
 */
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
@Override
public long getContentLength() {
    HttpEntity entity = response.getEntity();
    return entity == null ? -1 : entity.getContentLength();
}
 
Example 19
/**
 * 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();
}
 
Example 20
Source Project: Crawer   File: HttpConnnectionManager.java    License: MIT License 2 votes vote down vote up
/**
 * 
 * 从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;

}