home *** CD-ROM | disk | FTP | other *** search
/ Australian PC Authority 1999 May / may1999.iso / INTERNET / COMMUNIC / NETCAST.Z / marimb10.jar / netscape / netcast / FileCache.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-02-25  |  4.9 KB  |  174 lines

  1. package netscape.netcast;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.Enumeration;
  6. import marimba.castanet.client.CastanetCache;
  7. import marimba.castanet.client.CastanetFile;
  8. import marimba.castanet.util.Checksum;
  9. import marimba.io.RAFOutputStream;
  10.  
  11. public class FileCache implements CastanetCache {
  12.    static final char[] HEX_CHARS = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
  13.    FileCacheFile waiters;
  14.    FileCacheFile readers;
  15.    File dir;
  16.  
  17.    public FileCache(File dir) {
  18.       this.dir = dir;
  19.       if (!dir.exists()) {
  20.          dir.mkdirs();
  21.       }
  22.  
  23.    }
  24.  
  25.    FileCacheFile getPlainFile(Checksum cs, long length) {
  26.       String str = cs.toString();
  27.       StringBuffer buf = new StringBuffer(str.length() + 3);
  28.       buf.append(HEX_CHARS[(int)(cs.getChecksum1() & 15L)]);
  29.       buf.append(HEX_CHARS[(int)(cs.getChecksum2() & 15L)]);
  30.       buf.append(File.separatorChar);
  31.       buf.append(str);
  32.       return new FileCacheFile(this, this.dir, buf.toString(), length);
  33.    }
  34.  
  35.    public synchronized RAFOutputStream create(Checksum cs, long length) {
  36.       FileCacheFile file = this.getPlainFile(cs, length);
  37.  
  38.       try {
  39.          return new FileCacheOutputStream(file);
  40.       } catch (IOException var7) {
  41.          File parent = new File(((File)file).getParent());
  42.          if (!parent.exists() && !parent.mkdirs()) {
  43.             return null;
  44.          } else {
  45.             try {
  46.                return new FileCacheOutputStream(file);
  47.             } catch (IOException var6) {
  48.                return null;
  49.             }
  50.          }
  51.       }
  52.    }
  53.  
  54.    public synchronized CastanetFile getPending(Checksum cs, long length) {
  55.       return this.getPlainFile(cs, length);
  56.    }
  57.  
  58.    public synchronized CastanetFile get(Checksum cs) {
  59.       FileCacheFile file = this.getPlainFile(cs, -2L);
  60.       return ((File)file).exists() ? file : null;
  61.    }
  62.  
  63.    public synchronized boolean delete(Checksum cs) {
  64.       return this.getPlainFile(cs, -2L).delete();
  65.    }
  66.  
  67.    public Enumeration enumerate() {
  68.       return new FileCacheEnumeration(this, this.dir);
  69.    }
  70.  
  71.    public int estimateFileCount() {
  72.       String[] dirs = new String[]{"00", "11", "22", "33", "44", "55", "66", "77", "88", "99", "AA", "BB", "CC", "DD", "EE", "FF"};
  73.       int size = 0;
  74.  
  75.       for(int i = 0; i < dirs.length; ++i) {
  76.          String[] list = (new File(this.dir, dirs[i])).list();
  77.          if (list != null) {
  78.             size += list.length;
  79.          }
  80.       }
  81.  
  82.       size = size * 256 / dirs.length;
  83.       return size;
  84.    }
  85.  
  86.    public synchronized void notifyError(Checksum cs) {
  87.       for(FileCacheFile f = this.waiters; f != null; f = f.next) {
  88.          if (cs.equals(f.getChecksum())) {
  89.             f.notifyError();
  90.          }
  91.       }
  92.  
  93.       for(FileCacheFile f = this.readers; f != null; f = f.next) {
  94.          if (cs.equals(f.getChecksum())) {
  95.             f.notifyError();
  96.          }
  97.       }
  98.  
  99.       this.notifyAll();
  100.    }
  101.  
  102.    synchronized void notifyData(Checksum cs) {
  103.       for(FileCacheFile f = this.readers; f != null; f = f.next) {
  104.          if (cs.equals(f.getChecksum())) {
  105.             f.notifyData();
  106.          }
  107.       }
  108.  
  109.    }
  110.  
  111.    synchronized void notifyComplete(Checksum cs) {
  112.       this.notifyData(cs);
  113.  
  114.       for(FileCacheFile f = this.waiters; f != null; f = f.next) {
  115.          if (cs.equals(f.getChecksum())) {
  116.             f.notifyComplete();
  117.          }
  118.       }
  119.  
  120.    }
  121.  
  122.    synchronized void addWaiter(FileCacheFile f) {
  123.       f.next = this.waiters;
  124.       this.waiters = f;
  125.    }
  126.  
  127.    synchronized void removeWaiter(FileCacheFile f) {
  128.       if (this.waiters == f) {
  129.          this.waiters = f.next;
  130.       } else {
  131.          for(FileCacheFile p = this.waiters; p.next != null; p = p.next) {
  132.             if (p.next == f) {
  133.                p.next = f.next;
  134.                return;
  135.             }
  136.          }
  137.  
  138.       }
  139.    }
  140.  
  141.    synchronized void addReader(FileCacheFile f) {
  142.       f.next = this.readers;
  143.       this.readers = f;
  144.    }
  145.  
  146.    synchronized void removeReader(FileCacheFile f) {
  147.       if (this.readers == f) {
  148.          this.readers = f.next;
  149.       } else {
  150.          for(FileCacheFile p = this.readers; p.next != null; p = p.next) {
  151.             if (p.next == f) {
  152.                p.next = f.next;
  153.                return;
  154.             }
  155.          }
  156.  
  157.       }
  158.    }
  159.  
  160.    public synchronized void close() {
  161.       for(FileCacheFile f = this.readers; f != null; f = f.next) {
  162.          f.notifyError();
  163.       }
  164.  
  165.       for(FileCacheFile f = this.waiters; f != null; f = f.next) {
  166.          f.notifyError();
  167.       }
  168.  
  169.    }
  170.  
  171.    public void sync() {
  172.    }
  173. }
  174.