java.net.HttpURLConnection.getContentLength()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(9.6k)|赞(0)|评价(0)|浏览(314)

本文整理了Java中java.net.HttpURLConnection.getContentLength()方法的一些代码示例,展示了HttpURLConnection.getContentLength()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpURLConnection.getContentLength()方法的具体详情如下:
包路径:java.net.HttpURLConnection
类名称:HttpURLConnection
方法名:getContentLength

HttpURLConnection.getContentLength介绍

暂无

代码示例

代码示例来源:origin: square/okhttp

@Override public int getContentLength() {
 return delegate.getContentLength();
}

代码示例来源:origin: prestodb/presto

@Override public int getContentLength() {
 return delegate.getContentLength();
}

代码示例来源:origin: bumptech/glide

private InputStream getStreamForSuccessfulRequest(HttpURLConnection urlConnection)
  throws IOException {
 if (TextUtils.isEmpty(urlConnection.getContentEncoding())) {
  int contentLength = urlConnection.getContentLength();
  stream = ContentLengthInputStream.obtain(urlConnection.getInputStream(), contentLength);
 } else {
  if (Log.isLoggable(TAG, Log.DEBUG)) {
   Log.d(TAG, "Got non empty content encoding: " + urlConnection.getContentEncoding());
  }
  stream = urlConnection.getInputStream();
 }
 return stream;
}

代码示例来源:origin: RipMeApp/ripme

/**
 * @param url
 *      Target URL
 * @return 
 *      Returns connection length
 */
private int getTotalBytes(URL url) throws IOException {
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  conn.setRequestMethod("HEAD");
  conn.setRequestProperty("accept",  "*/*");
  conn.setRequestProperty("Referer", this.url.toExternalForm()); // Sic
  conn.setRequestProperty("User-agent", AbstractRipper.USER_AGENT);
  return conn.getContentLength();
}

代码示例来源:origin: google/agera

@NonNull
private byte[] getByteArray(@NonNull final HttpURLConnection connection) throws IOException {
 final int contentLength = connection.getContentLength();
 if (contentLength == 0) {
  return EMPTY_BODY;
 }
 final InputStream inputStream = getInputStream(connection);
 try {
  final int capacity = contentLength < 0 ? CONTENT_BUFFER_SIZE : contentLength;
  final ByteArrayOutputStream out = new ByteArrayOutputStream();
  final byte[] buffer = new byte[capacity];
  while (true) {
   int r = inputStream.read(buffer);
   if (r == -1) {
    break;
   }
   out.write(buffer, 0, r);
  }
  return out.toByteArray();
 } finally {
  inputStream.close();
 }
}

代码示例来源:origin: aa112901/remusic

/**
   * 得到Content大小
   *
   * @param response
   * @return
   */
  private int getContentLength(HttpURLConnection response) {
    int contentLength = 0;
    String range = request.getHeaderField(Constants.RANGE);
    if (range != null) {
      contentLength = Integer.valueOf(range.substring(range.indexOf("-") + 1, range.indexOf("/"))) + 1;
    } else {
      contentLength = request.getContentLength();
    }
    if (contentLength != 0) {
      cacheDao.insertOrUpdate(fileUtils.getFileName(), contentLength);
    }
    return contentLength;
  }
}

代码示例来源:origin: libgdx/libgdx

@Override
public byte[] getResult () {
  InputStream input = getInputStream();
  // If the response does not contain any content, input will be null.
  if (input == null) {
    return StreamUtils.EMPTY_BYTES;
  }
  try {
    return StreamUtils.copyStreamToByteArray(input, connection.getContentLength());
  } catch (IOException e) {
    return StreamUtils.EMPTY_BYTES;
  } finally {
    StreamUtils.closeQuietly(input);
  }
}

代码示例来源:origin: libgdx/libgdx

@Override
public byte[] getResult () {
  InputStream input = getInputStream();
  // If the response does not contain any content, input will be null.
  if (input == null) {
    return StreamUtils.EMPTY_BYTES;
  }
  try {
    return StreamUtils.copyStreamToByteArray(input, connection.getContentLength());
  } catch (IOException e) {
    return StreamUtils.EMPTY_BYTES;
  } finally {
    StreamUtils.closeQuietly(input);
  }
}

代码示例来源:origin: libgdx/libgdx

@Override
public String getResultAsString () {
  InputStream input = getInputStream();
  // If the response does not contain any content, input will be null.
  if (input == null) {
    return "";
  }
  try {
    return StreamUtils.copyStreamToString(input, connection.getContentLength());
  } catch (IOException e) {
    return "";
  } finally {
    StreamUtils.closeQuietly(input);
  }
}

代码示例来源:origin: libgdx/libgdx

@Override
public String getResultAsString () {
  InputStream input = getInputStream();
  // If the response does not contain any content, input will be null.
  if (input == null) {
    return "";
  }
  try {
    return StreamUtils.copyStreamToString(input, connection.getContentLength());
  } catch (IOException e) {
    return "";
  } finally {
    StreamUtils.closeQuietly(input);
  }
}

代码示例来源:origin: nostra13/Android-Universal-Image-Loader

return new ContentLengthInputStream(new BufferedInputStream(imageStream, BUFFER_SIZE), conn.getContentLength());

代码示例来源:origin: chentao0707/SimplifyReader

/**
 * Initializes an {@link org.apache.http.HttpEntity} from the given {@link java.net.HttpURLConnection}.
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
  BasicHttpEntity entity = new BasicHttpEntity();
  InputStream inputStream;
  try {
    inputStream = connection.getInputStream();
  } catch (IOException ioe) {
    inputStream = connection.getErrorStream();
  }
  entity.setContent(inputStream);
  entity.setContentLength(connection.getContentLength());
  entity.setContentEncoding(connection.getContentEncoding());
  entity.setContentType(connection.getContentType());
  return entity;
}

代码示例来源:origin: mcxiaoke/android-volley

/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
  BasicHttpEntity entity = new BasicHttpEntity();
  InputStream inputStream;
  try {
    inputStream = connection.getInputStream();
  } catch (IOException ioe) {
    inputStream = connection.getErrorStream();
  }
  entity.setContent(inputStream);
  entity.setContentLength(connection.getContentLength());
  entity.setContentEncoding(connection.getContentEncoding());
  entity.setContentType(connection.getContentType());
  return entity;
}

代码示例来源:origin: graphhopper/graphhopper

public void downloadAndUnzip(String url, String toFolder, final ProgressListener progressListener) throws IOException {
  HttpURLConnection conn = createConnection(url);
  final int length = conn.getContentLength();
  InputStream iStream = fetch(conn, false);
  new Unzipper().unzip(iStream, new File(toFolder), new ProgressListener() {
    @Override
    public void update(long sumBytes) {
      progressListener.update((int) (100 * sumBytes / length));
    }
  });
}

代码示例来源:origin: google/agera

@Test
public void shouldReturnErrorStreamForFailingInputStream() throws Throwable {
 final ByteArrayInputStream inputStream = new ByteArrayInputStream(RESPONSE_BODY);
 when(mockHttpURLConnection.getContentLength()).thenReturn(-1);
 //noinspection unchecked
 when(mockHttpURLConnection.getInputStream()).thenThrow(IOException.class);
 when(mockHttpURLConnection.getErrorStream()).thenReturn(inputStream);
 assertThat(httpFunction().apply(HTTP_GET_REQUEST).get().getBody(), is(RESPONSE_BODY));
 verify(mockHttpURLConnection).disconnect();
}

代码示例来源:origin: google/agera

@Test
public void shouldNotPassOnNullResponseHeader() throws Throwable {
 final ByteArrayInputStream inputStream = new ByteArrayInputStream(RESPONSE_BODY);
 when(mockHttpURLConnection.getInputStream()).thenReturn(inputStream);
 when(mockHttpURLConnection.getContentLength()).thenReturn(RESPONSE_BODY.length);
 final Map<String, List<String>> headerFields = new HashMap<>();
 headerFields.put(null, singletonList("value"));
 headerFields.put("naMe2", singletonList("value2"));
 when(mockHttpURLConnection.getHeaderFields()).thenReturn(headerFields);
 final HttpResponse httpResponse = httpFunction().apply(HTTP_GET_REQUEST).get();
 assertThat(httpResponse.header.size(), is(1));
 assertThat(httpResponse.header, hasEntry("name2", "value2"));
 verify(mockHttpURLConnection).disconnect();
}

代码示例来源:origin: google/agera

@Test
public void shouldGetEmptyBodyFromGetResponseOfZeroLength() throws Throwable {
 final InputStream inputStream = mock(InputStream.class);
 when(mockHttpURLConnection.getInputStream()).thenReturn(inputStream);
 when(mockHttpURLConnection.getContentLength()).thenReturn(0);
 assertThat(httpFunction().apply(HTTP_GET_REQUEST).get().getBody(), is(EMPTY_BODY));
 verify(mockHttpURLConnection).disconnect();
 verifyZeroInteractions(inputStream);
}

代码示例来源:origin: google/agera

@Test
public void shouldGetByteArrayFromGetResponseOfUnknownLength() throws Throwable {
 final ByteArrayInputStream inputStream = new ByteArrayInputStream(RESPONSE_BODY);
 when(mockHttpURLConnection.getInputStream()).thenReturn(inputStream);
 when(mockHttpURLConnection.getContentLength()).thenReturn(-1);
 assertThat(httpFunction().apply(HTTP_GET_REQUEST).get().getBody(), is(RESPONSE_BODY));
 verify(mockHttpURLConnection).disconnect();
}

代码示例来源:origin: google/agera

@Test
public void shouldGetByteArrayFromGetResponse() throws Throwable {
 final ByteArrayInputStream inputStream = new ByteArrayInputStream(RESPONSE_BODY);
 when(mockHttpURLConnection.getInputStream()).thenReturn(inputStream);
 when(mockHttpURLConnection.getContentLength()).thenReturn(RESPONSE_BODY.length);
 assertThat(httpFunction().apply(HTTP_GET_REQUEST).get().getBody(), is(RESPONSE_BODY));
 verify(mockHttpURLConnection).disconnect();
}

代码示例来源:origin: google/agera

@Test
public void shouldPassOnResponseHeadersAsLowerCase() throws Throwable {
 final ByteArrayInputStream inputStream = new ByteArrayInputStream(RESPONSE_BODY);
 when(mockHttpURLConnection.getInputStream()).thenReturn(inputStream);
 when(mockHttpURLConnection.getContentLength()).thenReturn(RESPONSE_BODY.length);
 final Map<String, List<String>> headerFields = new HashMap<>();
 headerFields.put("NAmE", singletonList("value"));
 headerFields.put("naMe2", singletonList("value2"));
 when(mockHttpURLConnection.getHeaderFields()).thenReturn(headerFields);
 final HttpResponse httpResponse = httpFunction().apply(HTTP_GET_REQUEST).get();
 assertThat(httpResponse.header.size(), is(2));
 assertThat(httpResponse.header, hasEntry("name", "value"));
 assertThat(httpResponse.header, hasEntry("name2", "value2"));
 verify(mockHttpURLConnection).disconnect();
}

相关文章

HttpURLConnection类方法