버그라고 보긴 힘들 것 같은데요.
레퍼런스에 이런 말이 있습니다.
By default, this implementation of HttpURLConnection
requests that servers use gzip compression. Since getContentLength()
returns the number of bytes transmitted, you cannot use that method to predict how many bytes can be read from getInputStream()
. Instead, read that stream until it is exhausted: when read()
returns -1. Gzip compression can be disabled by setting the acceptable encodings in the request header:
즉, HttpUrlConnection은 request할 때 용량을 줄이기 위해 gzip compression을 요청한다는 뜻입니다. 그럼 실제 무압축상태에서 1MB인 content라도 response header의 content-length필드에는 gzip 압축된 크기가 나올겁니다. 원하는 내용이 아니죠. 정확한 length를 알려면 압축을 풀어보는 수밖에 없습니다. 그래서 끝까지 다 읽는 과정이 들어간 것이 아닐까요?
해결방법은 바로 나와 있는 것처럼 무압축으로 request하면 될 것도 같네요. 헤더의 필드가 실제 사이즈니까 다 읽을 필요 없겠죠.
urlConnection.setRequestProperty("Accept-Encoding", "identity");
네트웍 패킷 한 번 따보세요. compression이 gzip인지, response header의 Content-Length 필드가 얼마로 오는지도요.
================================
다시 읽어보니, 레퍼런스에서 말하는 내용은 getContentLength()가 틀린 값이 올 수도 있으니 믿지말라는 얘기였군요.;;;
흠흠.. 삭제하려다가 삭제 버튼을 못찾아서 그냥 놔둡니다. ㅎ