home *** CD-ROM | disk | FTP | other *** search
- package netscape.netcast;
-
- import java.io.File;
- import java.io.IOException;
- import java.util.Enumeration;
- import marimba.castanet.client.CastanetCache;
- import marimba.castanet.client.CastanetFile;
- import marimba.castanet.util.Checksum;
- import marimba.io.RAFOutputStream;
-
- public class FileCache implements CastanetCache {
- static final char[] HEX_CHARS = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
- FileCacheFile waiters;
- FileCacheFile readers;
- File dir;
-
- public FileCache(File dir) {
- this.dir = dir;
- if (!dir.exists()) {
- dir.mkdirs();
- }
-
- }
-
- FileCacheFile getPlainFile(Checksum cs, long length) {
- String str = cs.toString();
- StringBuffer buf = new StringBuffer(str.length() + 3);
- buf.append(HEX_CHARS[(int)(cs.getChecksum1() & 15L)]);
- buf.append(HEX_CHARS[(int)(cs.getChecksum2() & 15L)]);
- buf.append(File.separatorChar);
- buf.append(str);
- return new FileCacheFile(this, this.dir, buf.toString(), length);
- }
-
- public synchronized RAFOutputStream create(Checksum cs, long length) {
- FileCacheFile file = this.getPlainFile(cs, length);
-
- try {
- return new FileCacheOutputStream(file);
- } catch (IOException var7) {
- File parent = new File(((File)file).getParent());
- if (!parent.exists() && !parent.mkdirs()) {
- return null;
- } else {
- try {
- return new FileCacheOutputStream(file);
- } catch (IOException var6) {
- return null;
- }
- }
- }
- }
-
- public synchronized CastanetFile getPending(Checksum cs, long length) {
- return this.getPlainFile(cs, length);
- }
-
- public synchronized CastanetFile get(Checksum cs) {
- FileCacheFile file = this.getPlainFile(cs, -2L);
- return ((File)file).exists() ? file : null;
- }
-
- public synchronized boolean delete(Checksum cs) {
- return this.getPlainFile(cs, -2L).delete();
- }
-
- public Enumeration enumerate() {
- return new FileCacheEnumeration(this, this.dir);
- }
-
- public int estimateFileCount() {
- String[] dirs = new String[]{"00", "11", "22", "33", "44", "55", "66", "77", "88", "99", "AA", "BB", "CC", "DD", "EE", "FF"};
- int size = 0;
-
- for(int i = 0; i < dirs.length; ++i) {
- String[] list = (new File(this.dir, dirs[i])).list();
- if (list != null) {
- size += list.length;
- }
- }
-
- size = size * 256 / dirs.length;
- return size;
- }
-
- public synchronized void notifyError(Checksum cs) {
- for(FileCacheFile f = this.waiters; f != null; f = f.next) {
- if (cs.equals(f.getChecksum())) {
- f.notifyError();
- }
- }
-
- for(FileCacheFile f = this.readers; f != null; f = f.next) {
- if (cs.equals(f.getChecksum())) {
- f.notifyError();
- }
- }
-
- this.notifyAll();
- }
-
- synchronized void notifyData(Checksum cs) {
- for(FileCacheFile f = this.readers; f != null; f = f.next) {
- if (cs.equals(f.getChecksum())) {
- f.notifyData();
- }
- }
-
- }
-
- synchronized void notifyComplete(Checksum cs) {
- this.notifyData(cs);
-
- for(FileCacheFile f = this.waiters; f != null; f = f.next) {
- if (cs.equals(f.getChecksum())) {
- f.notifyComplete();
- }
- }
-
- }
-
- synchronized void addWaiter(FileCacheFile f) {
- f.next = this.waiters;
- this.waiters = f;
- }
-
- synchronized void removeWaiter(FileCacheFile f) {
- if (this.waiters == f) {
- this.waiters = f.next;
- } else {
- for(FileCacheFile p = this.waiters; p.next != null; p = p.next) {
- if (p.next == f) {
- p.next = f.next;
- return;
- }
- }
-
- }
- }
-
- synchronized void addReader(FileCacheFile f) {
- f.next = this.readers;
- this.readers = f;
- }
-
- synchronized void removeReader(FileCacheFile f) {
- if (this.readers == f) {
- this.readers = f.next;
- } else {
- for(FileCacheFile p = this.readers; p.next != null; p = p.next) {
- if (p.next == f) {
- p.next = f.next;
- return;
- }
- }
-
- }
- }
-
- public synchronized void close() {
- for(FileCacheFile f = this.readers; f != null; f = f.next) {
- f.notifyError();
- }
-
- for(FileCacheFile f = this.waiters; f != null; f = f.next) {
- f.notifyError();
- }
-
- }
-
- public void sync() {
- }
- }
-