Skip to content

Commit 2253b16

Browse files
committed
#122: support for 2Gb+ files
1 parent 6cd1855 commit 2253b16

12 files changed

Lines changed: 30 additions & 25 deletions

File tree

library/src/main/java/com/danikula/videocache/ByteArrayCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public int read(byte[] buffer, long offset, int length) throws ProxyCacheExcepti
3333
}
3434

3535
@Override
36-
public int available() throws ProxyCacheException {
36+
public long available() throws ProxyCacheException {
3737
return data.length;
3838
}
3939

library/src/main/java/com/danikula/videocache/ByteArraySource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ public int read(byte[] buffer) throws ProxyCacheException {
2222
}
2323

2424
@Override
25-
public int length() throws ProxyCacheException {
25+
public long length() throws ProxyCacheException {
2626
return data.length;
2727
}
2828

2929
@Override
30-
public void open(int offset) throws ProxyCacheException {
30+
public void open(long offset) throws ProxyCacheException {
3131
arrayInputStream = new ByteArrayInputStream(data);
3232
arrayInputStream.skip(offset);
3333
}

library/src/main/java/com/danikula/videocache/Cache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
public interface Cache {
99

10-
int available() throws ProxyCacheException;
10+
long available() throws ProxyCacheException;
1111

1212
int read(byte[] buffer, long offset, int length) throws ProxyCacheException;
1313

library/src/main/java/com/danikula/videocache/HttpProxyCache.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,17 @@ public void processRequest(GetRequest request, Socket socket) throws IOException
4949
}
5050

5151
private boolean isUseCache(GetRequest request) throws ProxyCacheException {
52-
int sourceLength = source.length();
52+
long sourceLength = source.length();
5353
boolean sourceLengthKnown = sourceLength > 0;
54-
int cacheAvailable = cache.available();
54+
long cacheAvailable = cache.available();
5555
// do not use cache for partial requests which too far from available cache. It seems user seek video.
5656
return !sourceLengthKnown || !request.partial || request.rangeOffset <= cacheAvailable + sourceLength * NO_CACHE_BARRIER;
5757
}
5858

5959
private String newResponseHeaders(GetRequest request) throws IOException, ProxyCacheException {
6060
String mime = source.getMime();
6161
boolean mimeKnown = !TextUtils.isEmpty(mime);
62-
int length = cache.isCompleted() ? cache.available() : source.length();
62+
long length = cache.isCompleted() ? cache.available() : source.length();
6363
boolean lengthKnown = length >= 0;
6464
long contentLength = request.partial ? length - request.rangeOffset : length;
6565
boolean addRange = lengthKnown && request.partial;

library/src/main/java/com/danikula/videocache/HttpUrlSource.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

library/src/main/java/com/danikula/videocache/ProxyCache.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ private void notifyNewCacheDataAvailable(long cacheAvailable, long sourceAvailab
103103

104104
protected void onCacheAvailable(long cacheAvailable, long sourceLength) {
105105
boolean zeroLengthSource = sourceLength == 0;
106-
int percents = zeroLengthSource ? 100 : (int) (cacheAvailable * 100 / sourceLength);
106+
int percents = zeroLengthSource ? 100 : (int) (cacheAvailable / sourceLength * 100);
107107
boolean percentsChanged = percents != percentsAvailable;
108108
boolean sourceLengthKnown = sourceLength >= 0;
109109
if (sourceLengthKnown && percentsChanged) {
@@ -116,8 +116,8 @@ protected void onCachePercentsAvailableChanged(int percentsAvailable) {
116116
}
117117

118118
private void readSource() {
119-
int sourceAvailable = -1;
120-
int offset = 0;
119+
long sourceAvailable = -1;
120+
long offset = 0;
121121
try {
122122
offset = cache.available();
123123
source.open(offset);

library/src/main/java/com/danikula/videocache/Source.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ public interface Source {
1313
* @param offset offset in bytes for source.
1414
* @throws ProxyCacheException if error occur while opening source.
1515
*/
16-
void open(int offset) throws ProxyCacheException;
16+
void open(long offset) throws ProxyCacheException;
1717

1818
/**
1919
* Returns length bytes or <b>negative value</b> if length is unknown.
2020
*
2121
* @return bytes length
2222
* @throws ProxyCacheException if error occur while fetching source data.
2323
*/
24-
int length() throws ProxyCacheException;
24+
long length() throws ProxyCacheException;
2525

2626
/**
2727
* Read data to byte buffer from source with current offset.

library/src/main/java/com/danikula/videocache/SourceInfo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
public class SourceInfo {
99

1010
public final String url;
11-
public final int length;
11+
public final long length;
1212
public final String mime;
1313

14-
public SourceInfo(String url, int length, String mime) {
14+
public SourceInfo(String url, long length, String mime) {
1515
this.url = url;
1616
this.length = length;
1717
this.mime = mime;

library/src/main/java/com/danikula/videocache/file/FileCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public FileCache(File file, DiskUsage diskUsage) throws ProxyCacheException {
4141
}
4242

4343
@Override
44-
public synchronized int available() throws ProxyCacheException {
44+
public synchronized long available() throws ProxyCacheException {
4545
try {
4646
return (int) dataFile.length();
4747
} catch (IOException e) {

library/src/main/java/com/danikula/videocache/sourcestorage/DatabaseSourceInfoStorage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void release() {
8383
private SourceInfo convert(Cursor cursor) {
8484
return new SourceInfo(
8585
cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_URL)),
86-
cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_LENGTH)),
86+
cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_LENGTH)),
8787
cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_MIME))
8888
);
8989
}

0 commit comments

Comments
 (0)