@@ -55,33 +55,38 @@ public HttpUrlSource(HttpUrlSource source) {
5555 }
5656
5757 @ Override
58- public synchronized int length () throws ProxyCacheException {
58+ public synchronized long length () throws ProxyCacheException {
5959 if (sourceInfo .length == Integer .MIN_VALUE ) {
6060 fetchContentInfo ();
6161 }
6262 return sourceInfo .length ;
6363 }
6464
6565 @ Override
66- public void open (int offset ) throws ProxyCacheException {
66+ public void open (long offset ) throws ProxyCacheException {
6767 try {
6868 connection = openConnection (offset , -1 );
6969 String mime = connection .getContentType ();
7070 inputStream = new BufferedInputStream (connection .getInputStream (), DEFAULT_BUFFER_SIZE );
71- int length = readSourceAvailableBytes (connection , offset , connection .getResponseCode ());
71+ long length = readSourceAvailableBytes (connection , offset , connection .getResponseCode ());
7272 this .sourceInfo = new SourceInfo (sourceInfo .url , length , mime );
7373 this .sourceInfoStorage .put (sourceInfo .url , sourceInfo );
7474 } catch (IOException e ) {
7575 throw new ProxyCacheException ("Error opening connection for " + sourceInfo .url + " with offset " + offset , e );
7676 }
7777 }
7878
79- private int readSourceAvailableBytes (HttpURLConnection connection , int offset , int responseCode ) throws IOException {
80- int contentLength = connection . getContentLength ();
79+ private long readSourceAvailableBytes (HttpURLConnection connection , long offset , int responseCode ) throws IOException {
80+ long contentLength = getContentLength (connection );
8181 return responseCode == HTTP_OK ? contentLength
8282 : responseCode == HTTP_PARTIAL ? contentLength + offset : sourceInfo .length ;
8383 }
8484
85+ private long getContentLength (HttpURLConnection connection ) {
86+ String contentLengthValue = connection .getHeaderField ("Content-Length" );
87+ return contentLengthValue == null ? -1 : Long .parseLong (contentLengthValue );
88+ }
89+
8590 @ Override
8691 public void close () throws ProxyCacheException {
8792 if (connection != null ) {
@@ -116,7 +121,7 @@ private void fetchContentInfo() throws ProxyCacheException {
116121 InputStream inputStream = null ;
117122 try {
118123 urlConnection = openConnection (0 , 10000 );
119- int length = urlConnection . getContentLength ();
124+ long length = getContentLength (urlConnection );
120125 String mime = urlConnection .getContentType ();
121126 inputStream = urlConnection .getInputStream ();
122127 this .sourceInfo = new SourceInfo (sourceInfo .url , length , mime );
@@ -132,7 +137,7 @@ private void fetchContentInfo() throws ProxyCacheException {
132137 }
133138 }
134139
135- private HttpURLConnection openConnection (int offset , int timeout ) throws IOException , ProxyCacheException {
140+ private HttpURLConnection openConnection (long offset , int timeout ) throws IOException , ProxyCacheException {
136141 HttpURLConnection connection ;
137142 boolean redirected ;
138143 int redirectCount = 0 ;
0 commit comments