📄 cache.java
字号:
package com.gameislive.browser;
//import javax.microedition.rms.RecordStore;
import java.util.Vector;
/**
* 某些图片若曾经访问且在Cache缓冲池中有记录,则不再从服务器中加载<br>
* 直接从Cache中加载可在一定程度提高浏览速度<br>
* 所有Cache内容在浏览器退出时都不保存到RMS中
*
* @author pan
*
*/
public class Cache {
Vector vector;
int limit;
int total;
/**
* Cache缓存池数据被保存在RMS中,每创建Cache缓存池时,需要从RMS中加载原来保存的数据
* @param i
*/
public Cache(int i) {
vector = new Vector(1);
limit = i;
reset();
// try {
// RecordStore recordstore = RecordStore.openRecordStore("browser.Cache", false);
// if (recordstore != null) {
// int j = recordstore.getNumRecords();
// for (int k = 1; k <= j; k += 3) {
// String s = new String(recordstore.getRecord(k));
// String t = new String(recordstore.getRecord(k + 1));
// byte abyte0[] = recordstore.getRecord(k + 2);
// vector.addElement(new CookiesObject(s,t,abyte0));
// }
//
// recordstore.closeRecordStore();
// }
// }
// catch (Exception e) {
// e.printStackTrace();
// }
}
public void reset() {
vector.removeAllElements();
total = 0;
}
/**
* 添加一条记录到缓存池,如果缓存池不足以容纳该记录,则不添加
* 如果缓存池足够容纳该记录,但剩余空间不足,则删除池中第一条记录
* 如果缓存池剩余空间足够大,则直接在池后边添加
* @param s
* @param abyte0
*/
public void setCache(String s,String contentType, byte abyte0[]) {
if (abyte0.length >= limit) {
return;
}
if (getCache(s) != null) {
return;
}
do {
if (total + abyte0.length < limit) {
break;
}
CacheObject cacheobj = (CacheObject) vector.elementAt(0);
total -= cacheobj.buffer.length;
vector.removeElementAt(0);
if (vector.size() == 0) {
total = 0;
}
} while (true);
total += abyte0.length;
if(contentType==null) contentType="";
vector.addElement(new CacheObject(s,contentType, abyte0));
}
/**
* 从缓存池中获得指定名字的记录数据
* @param s
* @return
*/
public byte[] getCache(String s) {
int i = vector.size();
for (int j = 0; j < i; j++) {
CacheObject cacheobj = (CacheObject) vector.elementAt(j);
if (s.equals(cacheobj.name)) {
return cacheobj.buffer;
}
}
return null;
}
/**
* 从缓存池中获得指定名字的CacheObj
* @param s
* @return
*/
public CacheObject getCookiesObject(String s) {
int i = vector.size();
for (int j = 0; j < i; j++) {
CacheObject cacheobj = (CacheObject) vector.elementAt(j);
if (s.equals(cacheobj.name)) {
return cacheobj;
}
}
return null;
}
// /**
// * 浏览器关闭时,需要把数据从缓存池写入RMS,以便下次登陆时继续使用
// *
// */
// public void saveCache() {
// try {
// RecordStore.deleteRecordStore("browser.Cache");
// }
// catch (Exception e) {
// e.printStackTrace();
// }
// int i = vector.size();
// if (i == 0) {
// return;
// }
// try {
// RecordStore recordstore = RecordStore.openRecordStore("browser.Cache", true);
// for (int j = 0; j < i; j++) {
// CookiesObject cacheobj = (CookiesObject) vector.elementAt(j);
// recordstore.addRecord(cacheobj.name.getBytes(), 0, cacheobj.name.length());
// recordstore.addRecord(cacheobj.contentType.getBytes(), 0, cacheobj.contentType.length());
// recordstore.addRecord(cacheobj.buffer, 0, cacheobj.buffer.length);
// }
// recordstore.closeRecordStore();
// }
// catch (Exception exception1) {
// }
// }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -