home *** CD-ROM | disk | FTP | other *** search
- package netscape.netcast;
-
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.Enumeration;
- import marimba.castanet.client.CastanetFile;
- import marimba.castanet.util.Checksum;
- import marimba.io.FastInputStream;
- import marimba.io.FastOutputStream;
- import marimba.io.RAFOutputStream;
- import marimba.util.Timer;
- import marimba.util.TimerClient;
- import netscape.security.PrivilegeManager;
- import netscape.security.Target;
-
- public final class MappedFileCache extends FileCache implements TimerClient {
- static final int FILES_PER_DIRECTORY = 128;
- static final int WRITE_DELAY = 3000;
- static final int ENTRY_SIZE = 20;
- File mapFile;
- // $FF: renamed from: in marimba.io.FastInputStream
- FastInputStream field_0;
- FastOutputStream out;
- byte[] data;
- int len;
- int count;
- int nextFile;
- int currentDirectory;
- boolean dirty;
-
- public MappedFileCache(File dir) {
- super(dir);
- this.mapFile = new File(dir, "cache.map");
- this.load();
- }
-
- synchronized void load() {
- PrivilegeManager privMgr = PrivilegeManager.getPrivilegeManager();
- Target target = null;
- if (privMgr != null) {
- target = Target.findTarget("MarimbaAppContextTarget");
- if (target != null) {
- privMgr.enablePrivilege(target);
- }
- }
-
- this.nextFile = -1;
- if (this.mapFile.exists()) {
- this.len = (int)this.mapFile.length();
- this.data = new byte[this.len + 1000];
-
- try {
- FileInputStream fin = new FileInputStream(this.mapFile);
-
- try {
- fin.read(this.data, 0, this.len);
- } finally {
- fin.close();
- }
-
- this.len -= 4;
- if (this.len % 20 != 0) {
- System.out.println("Database length screw up!");
- }
-
- this.count = this.len / 20;
- this.currentDirectory = this.count / 128;
- this.field_0 = new FastInputStream(this.data);
- this.out = new FastOutputStream(this.data);
- this.field_0.seek((long)this.len);
- this.nextFile = this.field_0.readInt();
- } catch (IOException var9) {
- }
- }
-
- if (this.nextFile == -1) {
- this.len = 0;
- this.count = 0;
- this.currentDirectory = 0;
- this.nextFile = 1;
- this.data = new byte[1000];
- this.field_0 = new FastInputStream(this.data);
- this.out = new FastOutputStream(this.data);
- }
-
- }
-
- synchronized boolean save() {
- if (this.dirty) {
- PrivilegeManager privMgr = PrivilegeManager.getPrivilegeManager();
- Target target = null;
- if (privMgr != null) {
- target = Target.findTarget("MarimbaAppContextTarget");
- if (target != null) {
- privMgr.enablePrivilege(target);
- }
- }
-
- try {
- this.out.seek((long)this.len);
- this.out.writeInt(this.nextFile);
- FileOutputStream out = new FileOutputStream(this.mapFile);
- long time = System.currentTimeMillis();
-
- try {
- out.write(this.data, 0, this.len + 4);
- this.dirty = false;
- } finally {
- out.close();
- }
-
- time = System.currentTimeMillis() - time;
- } catch (IOException var11) {
- return false;
- }
- }
-
- return true;
- }
-
- void modify() {
- this.dirty = true;
- Timer.master.add(this, System.currentTimeMillis() + 3000L, (Object)null);
- }
-
- public long tick(long tm, Object arg) {
- if (!this.save()) {
- System.out.println("[Couldn't save cache map!]");
- }
-
- return -1L;
- }
-
- Checksum getChecksum(int slot) {
- try {
- this.field_0.seek((long)(slot * 20));
- return new Checksum(this.field_0);
- } catch (IOException e) {
- ((Throwable)e).printStackTrace();
- return null;
- }
- }
-
- int getName(int slot) {
- try {
- this.field_0.seek((long)(slot * 20 + 16));
- return this.field_0.readInt();
- } catch (IOException var2) {
- return -1;
- }
- }
-
- int compare(long cs1, long cs2, int slot) {
- int off = slot * 20;
- byte[] data = this.data;
-
- for(int i = 56; i >= 0; i -= 8) {
- long diff = (cs1 >> i & 255L) - (long)(data[off++] & 255);
- if (diff < 0L) {
- return -1;
- }
-
- if (diff > 0L) {
- return 1;
- }
- }
-
- for(int i = 56; i >= 0; i -= 8) {
- long diff = (cs2 >> i & 255L) - (long)(data[off++] & 255);
- if (diff < 0L) {
- return -1;
- }
-
- if (diff > 0L) {
- return 1;
- }
- }
-
- return 0;
- }
-
- int lookup(Checksum cs) {
- int min = 0;
- int max = this.count;
- long cs1 = cs.getChecksum1();
- long cs2 = cs.getChecksum2();
-
- while(min < max) {
- int mid = (min + max) / 2;
- int cmp = this.compare(cs1, cs2, mid);
- switch (cmp) {
- case -1:
- max = mid;
- break;
- case 0:
- default:
- return this.getName(mid);
- case 1:
- min = mid + 1;
- }
- }
-
- return -1;
- }
-
- String makeName(int fno) {
- return Integer.toString(fno / 128, 36) + File.separatorChar + Integer.toString(fno, 36);
- }
-
- int nextFile() {
- this.currentDirectory = this.nextFile / 128;
- return this.nextFile++;
- }
-
- void insert(Checksum cs) {
- this.insert(cs, this.nextFile());
- }
-
- void insert(Checksum cs, int name) {
- PrivilegeManager privMgr = PrivilegeManager.getPrivilegeManager();
- Target target = null;
- if (privMgr != null) {
- target = Target.findTarget("MarimbaAppContextTarget");
- if (target != null) {
- privMgr.enablePrivilege(target);
- }
- }
-
- int min = 0;
- int max = this.count;
- long cs1 = cs.getChecksum1();
- long cs2 = cs.getChecksum2();
-
- while(min < max) {
- int mid = (min + max) / 2;
- int cmp = this.compare(cs1, cs2, mid);
- switch (cmp) {
- case -1:
- max = mid;
- break;
- case 0:
- default:
- System.out.println("Cache error: already in cache");
- break;
- case 1:
- min = mid + 1;
- }
- }
-
- if (this.len + 20 >= this.data.length - 4) {
- byte[] d = new byte[this.data.length * 3 / 2];
- System.arraycopy(this.data, 0, d, 0, this.data.length);
- this.data = d;
- this.field_0 = new FastInputStream(this.data);
- this.out = new FastOutputStream(this.data);
- }
-
- int pos = min * 20;
- if (min < this.count) {
- System.arraycopy(this.data, pos, this.data, pos + 20, this.len - pos);
- }
-
- try {
- this.out.seek((long)pos);
- cs.writeChecksum(this.out);
- this.out.writeInt(name);
- ++this.count;
- this.len += 20;
- } catch (IOException e) {
- ((Throwable)e).printStackTrace();
- }
- }
-
- int remove(Checksum cs) {
- int min = 0;
- int max = this.count;
- long cs1 = cs.getChecksum1();
- long cs2 = cs.getChecksum2();
-
- while(min < max) {
- int mid = (min + max) / 2;
- int cmp = this.compare(cs1, cs2, mid);
- switch (cmp) {
- case -1:
- max = mid;
- break;
- case 0:
- default:
- int name = this.getName(mid);
- --this.count;
- this.len -= 20;
- System.arraycopy(this.data, (mid + 1) * 20, this.data, mid * 20, (this.count - mid) * 20);
- this.modify();
- return name;
- case 1:
- min = mid + 1;
- }
- }
-
- return -1;
- }
-
- public synchronized CastanetFile get(Checksum cs) {
- PrivilegeManager privMgr = PrivilegeManager.getPrivilegeManager();
- Target target = null;
- if (privMgr != null) {
- target = Target.findTarget("MarimbaAppContextTarget");
- if (target != null) {
- privMgr.enablePrivilege(target);
- }
- }
-
- int name = this.lookup(cs);
- if (name != -1) {
- FileCacheFile file = new MappedFileCacheFile(this, super.dir, this.makeName(name), -2L, cs);
- return ((File)file).exists() ? file : null;
- } else {
- return null;
- }
- }
-
- public synchronized CastanetFile getPending(Checksum cs, long length) {
- PrivilegeManager privMgr = PrivilegeManager.getPrivilegeManager();
- Target target = null;
- if (privMgr != null) {
- target = Target.findTarget("MarimbaAppContextTarget");
- if (target != null) {
- privMgr.enablePrivilege(target);
- }
- }
-
- int name = this.lookup(cs);
- if (name == -1) {
- name = this.nextFile();
- this.insert(cs, name);
- }
-
- return new MappedFileCacheFile(this, super.dir, this.makeName(name), length, cs);
- }
-
- public synchronized boolean delete(Checksum cs) {
- PrivilegeManager privMgr = PrivilegeManager.getPrivilegeManager();
- Target target = null;
- if (privMgr != null) {
- target = Target.findTarget("MarimbaAppContextTarget");
- if (target != null) {
- privMgr.enablePrivilege(target);
- }
- }
-
- int name = this.lookup(cs);
- if (name != -1) {
- File file = new File(super.dir, this.makeName(name));
- if (file.delete()) {
- this.remove(cs);
- int dirno = name / 128;
- if (dirno < this.currentDirectory) {
- (new File(file.getParent())).delete();
- }
-
- return true;
- }
- }
-
- return false;
- }
-
- void deleted(MappedFileCacheFile file) {
- this.remove(((FileCacheFile)file).getChecksum());
- }
-
- public synchronized RAFOutputStream create(Checksum cs, long length) {
- PrivilegeManager privMgr = PrivilegeManager.getPrivilegeManager();
- Target target = null;
- if (privMgr != null) {
- target = Target.findTarget("MarimbaAppContextTarget");
- if (target != null) {
- privMgr.enablePrivilege(target);
- }
- }
-
- int name = this.lookup(cs);
- boolean newFile = name == -1;
- if (newFile) {
- name = this.nextFile();
- }
-
- FileCacheFile file = new MappedFileCacheFile(this, super.dir, this.makeName(name), length, cs);
-
- RAFOutputStream raf;
- try {
- raf = new FileCacheOutputStream(file);
- } catch (IOException var12) {
- File parent = new File(((File)file).getParent());
- if (!parent.exists() && !parent.mkdirs()) {
- return null;
- }
-
- try {
- raf = new FileCacheOutputStream(file);
- } catch (IOException var11) {
- return null;
- }
- }
-
- if (newFile) {
- this.insert(cs, name);
- }
-
- return raf;
- }
-
- public Enumeration enumerate() {
- return new MappedFileCacheEnumeration(this);
- }
-
- public int estimateFileCount() {
- return this.count;
- }
-
- public synchronized void close() {
- if (this.dirty && !this.save()) {
- System.out.println("Couldn't close cache!");
- }
-
- }
-
- public synchronized void notifyComplete(Checksum cs) {
- this.modify();
- }
-
- public void sync() {
- this.save();
- }
- }
-