allow to use shared proxy with shared cache for multiple clients · leelizk/AndroidVideoCache@5115b96 · GitHub
Skip to content

Commit 5115b96

Browse files
committed
allow to use shared proxy with shared cache for multiple clients
1 parent 7f22a66 commit 5115b96

27 files changed

Lines changed: 902 additions & 540 deletions

library/build.gradle

Lines changed: 1 addition & 1 deletion

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
import java.io.ByteArrayInputStream;
44
import java.util.Arrays;
55

6-
import static com.danikula.videocache.Preconditions.checkArgument;
7-
import static com.danikula.videocache.Preconditions.checkNotNull;
8-
96
/**
107
* Simple memory based {@link Cache} implementation.
118
*
@@ -24,7 +21,6 @@ public ByteArrayCache(byte[] data) {
2421
this.data = Preconditions.checkNotNull(data);
2522
}
2623

27-
2824
@Override
2925
public int read(byte[] buffer, long offset, int length) throws ProxyCacheException {
3026
if (offset >= data.length) {
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package com.danikula.videocache;
22

3+
import java.io.File;
4+
35
/**
4-
* @author Egor Makovsky (yahor.makouski@gmail.com).
6+
* Listener for cache availability.
7+
*
8+
* @author Egor Makovsky (yahor.makouski@gmail.com)
9+
* @author Alexey Danilov (danikula@gmail.com).
510
*/
611
public interface CacheListener {
7-
void onError(ProxyCacheException e);
812

9-
void onCacheDataAvailable(int cachePercentage);
13+
void onCacheAvailable(File cacheFile, String url, int percentsAvailable);
1014
}

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,16 @@ public class FileCache implements Cache {
1515

1616
private static final String TEMP_POSTFIX = ".download";
1717

18+
public File file;
1819
private RandomAccessFile dataFile;
19-
private File file;
2020

2121
public FileCache(File file) throws ProxyCacheException {
2222
try {
2323
checkNotNull(file);
24-
boolean partialFile = isTempFile(file);
25-
boolean completed = file.exists() && !partialFile;
26-
if (completed) {
27-
this.dataFile = new RandomAccessFile(file, "r");
28-
this.file = file;
29-
} else {
30-
ProxyCacheUtils.createDirectory(file.getParentFile());
31-
this.file = partialFile ? file : new File(file.getParentFile(), file.getName() + TEMP_POSTFIX);
32-
this.dataFile = new RandomAccessFile(this.file, "rw");
33-
}
24+
ProxyCacheUtils.createDirectory(file.getParentFile());
25+
boolean completed = file.exists();
26+
this.file = completed ? file : new File(file.getParentFile(), file.getName() + TEMP_POSTFIX);
27+
this.dataFile = new RandomAccessFile(this.file, completed ? "r" : "rw");
3428
} catch (IOException e) {
3529
throw new ProxyCacheException("Error using file " + file + " as disc cache", e);
3630
}
@@ -41,7 +35,7 @@ public synchronized int available() throws ProxyCacheException {
4135
try {
4236
return (int) dataFile.length();
4337
} catch (IOException e) {
44-
throw new ProxyCacheException("Error reading length of file " + dataFile, e);
38+
throw new ProxyCacheException("Error reading length of file " + file, e);
4539
}
4640
}
4741

@@ -117,4 +111,5 @@ public File getFile() {
117111
private boolean isTempFile(File file) {
118112
return file.getName().endsWith(TEMP_POSTFIX);
119113
}
114+
120115
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.danikula.videocache;
2+
3+
import java.io.File;
4+
5+
/**
6+
* Generator for files to be used for caching.
7+
*
8+
* @author Alexey Danilov (danikula@gmail.com).
9+
*/
10+
public interface FileNameGenerator {
11+
12+
File generate(String url);
13+
14+
}
Lines changed: 71 additions & 0 deletions

0 commit comments

Comments
 (0)