简单的做了持久化list的操作,方便网络传输数据,待时日可扩展为持久化队列系统或nosql数据库。为自主开发的的nosql作准备,以此为记:
package com.test.list;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
*
* 持久化list对象
*
* @author LiuZiHeng
* @version
* @date 2010-8-16
*/
public class ListToBytes {
public byte[] int2Bytes(int val) {
byte[] bval = new byte[4];
bval[0] = (byte)(val >> 24);
bval[1] = (byte)((val << 8) >> 24);
bval[2] = (byte)((val << 16)>>24);
bval[3] = (byte)((val << 24) >> 24);
return bval;
}
public void saveList(OutputStream out, List<A> list) throws IOException {
if(list == null || list.size() == 0) {
return;
}
for(int i = 0; i < list.size(); i++) {
A a = list.get(i);
byte[] objb = ObjectToBytes(a);
out.write(int2Bytes(objb.length));
out.write(objb);
}
}
/**
* Object to byte[]
*
* @param obj
* @return
*/
public byte[] ObjectToBytes(Object obj) {
ObjectOutput out = null;
try {
ByteArrayOutputStream byteout = new ByteArrayOutputStream();
out = new ObjectOutputStream(byteout);
out.writeObject(obj);
byte[] buf = byteout.toByteArray();
return buf;
} catch (IOException e) {
return null;
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}
}
/**
* byte[] to long
*
* @param b
* @return
*/
public Object bytesToObject(byte[] b) {
if (b.length > 0) {
ObjectInput in = null;
try {
ByteArrayInputStream byteIn = new ByteArrayInputStream(b);
in = new ObjectInputStream(byteIn);
Object obj = in.readObject();
if(obj != null) {
return obj;
}
} catch (IOException e) {
} catch (ClassNotFoundException e) {
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
}
return null;
} else {
return null;
}
}
public List getListFromSave(String filePath) {
List<Object> l2 = new ArrayList<Object>();
RandomAccessFile ram =null;
try {
ram = new RandomAccessFile(filePath, "r");
while(ram.getFilePointer() != ram.length()) {
int len = ram.readInt();
byte[] buf = new byte[len];
ram.readFully(buf);
Object tmpa = bytesToObject(buf);
l2.add(tmpa);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(ram != null) {
try {
ram.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return l2;
}
public static void main(String[] args) {
ListToBytes ltb = new ListToBytes();
List<A> list = new ArrayList<A>();
A a = new A("a", 1);
A a2 = new A("b", 2);
A a3 = new A("c", 3);
list.add(a);
list.add(a2);
list.add(a3);
System.out.println("list before mod:");
for(A ab : list) {
System.out.println(ab.getA() + "->" + ab.getB());
}
FileOutputStream out = null;
try {
out = new FileOutputStream("./savelist/list.f");
ltb.saveList(out, list);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("\nmod list:");
list.get(0).setA("a11");
for(A ab : list) {
System.out.println(ab.getA() + "->" + ab.getB());
}
System.out.println("\nload it from save:");
List l2 = ltb.getListFromSave("./savelist/list.f");
for(Object a22 : l2) {
System.out.println(((A)a22).getA() + "=" + ((A)a22).getB());
}
}
}
class A implements Serializable {
private static final long serialVersionUID = -2278862909904751331L;
private String a;
private int b;
public A(String a, int b) {
this.a = a;
this.b = b;
}
public void setA(String a) {
this.a = a;
}
public String getA() {
return a;
}
public void setB(int b) {
this.b = b;
}
public int getB() {
return b;
}
}