📄 cookiemanager.java
字号:
/*
* CookieManager.java
*
* Created on 2006年4月29日, 上午10:08
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package org.gggeye.easymf.net;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Vector;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordFilter;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import org.gggeye.easymf.util.StringTool;
/**
* This class is used to manage cookie on the mobile device. not implemented
* @author mingjava
* @version 0.1 05/06/2006
*/
public class CookieManager {
private RecordStore rs = null;
public static final String COOKIE_STORE = "httpmecookie";
private static class CookieFilter implements RecordFilter{
private String path = "/";
CookieFilter(String path){
this.path = path;
}
public boolean matches(byte[] data){
ByteArrayInputStream bais = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bais);
try{
Cookie cookie = Cookie.deserialize(dis);
return path.startsWith(cookie.getPath());
}catch(IOException ex){
ex.printStackTrace();
return false;
}
}
}
/** Creates a new instance of CookieManager */
public CookieManager() {
try{
rs = RecordStore.openRecordStore(COOKIE_STORE,true);
}catch(RecordStoreException ex){
ex.printStackTrace();
}
}
public void release(){
try{
rs.closeRecordStore();
}catch(RecordStoreException ex){
ex.printStackTrace();
}
}
public void addCookie(Cookie cookie){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try{
cookie.serialize(dos);
byte[] data = baos.toByteArray();
int id = isExist(cookie);
if(id == -1){
rs.addRecord(data,0,data.length);
}else{
rs.setRecord(id,data,0,data.length);
}
}catch(IOException ex){
ex.printStackTrace();
}catch(RecordStoreException ex){
ex.printStackTrace();
}finally{
try{
dos.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
private int isExist(Cookie cookie){
try{
RecordEnumeration re = rs.enumerateRecords(null,null,false);
int size = re.numRecords();
if(size == 0)
return -1;
else{
while(re.hasNextElement()){
int id = re.nextRecordId();
byte[] data = rs.getRecord(id);
ByteArrayInputStream bais = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bais);
try {
Cookie c = Cookie.deserialize(dis);
if(c.getName().equals(cookie.getName())&&c.getPath().equals(cookie.getPath()))
return id;
} catch(IOException ex){
ex.printStackTrace();
} finally {
try{
dis.close();
}catch(IOException ex){
}
}
}
}
}catch(RecordStoreException ex){
ex.printStackTrace();
}
return -1;
}
public Cookie[] getCookie(String path){
try{
RecordEnumeration re = rs.enumerateRecords(new CookieFilter(path),null,false);
int size = re.numRecords();
if(size == 0){
return null;
}else{
Vector v = new Vector();
int i = 0;
while(re.hasNextElement()){
int id = re.nextRecordId();
byte[] data = rs.getRecord(id);
ByteArrayInputStream bais = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bais);
try{
Cookie cookie = Cookie.deserialize(dis);
if(cookie.isExpired()){
rs.deleteRecord(id);
}else{
v.addElement(cookie);
}
}catch(IOException ex){
ex.printStackTrace();
}finally{
try{
dis.close();
}catch(IOException e){
//do nothing
}
}
}
int vsize = v.size();
if(vsize == 0)
return null;
else{
Cookie[] cookies = new Cookie[vsize];
v.copyInto(cookies);
v.removeAllElements();
v= null;
return cookies;
}
}
}catch(RecordStoreException ex){
ex.printStackTrace();
return null;
}
}
public void deleteCookie(Cookie cookie){
int id = isExist(cookie);
if(id != -1){
try{
rs.deleteRecord(id);
}catch(RecordStoreException ex){
ex.printStackTrace();
}
}
}
public static Cookie parseCookie(String s,String uri){
Cookie cookie = new Cookie();
System.out.println(s);
StringTool su = new StringTool(s,";");
while(su.hasMoreTokens()){
String str = su.nextToken().trim();
int i = str.indexOf("=");
if(i == -1){
//secure do nothing
continue;
}else{
String name = str.substring(0,i);
String value = str.substring(i+1,str.length());
if("path".equalsIgnoreCase(name)){
cookie.setPath(value);
}else if("expires".equalsIgnoreCase(name)){
cookie.setExpire(StringTool.getData(value));
}else if("domain".equalsIgnoreCase(name)){
//do nothing
}else{
cookie.setName(name);
cookie.setValue(value);
}
}
if(cookie.getPath().equals(""))
cookie.setPath(uri);
}
System.out.println(cookie.toString());
return cookie;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -