将php序列化到数据库的对象饭序列化成java对象.docx
《将php序列化到数据库的对象饭序列化成java对象.docx》由会员分享,可在线阅读,更多相关《将php序列化到数据库的对象饭序列化成java对象.docx(34页珍藏版)》请在冰豆网上搜索。
将php序列化到数据库的对象饭序列化成java对象
将php序列化到数据库的对象饭序列化成java对象
php的对象序列化到数据库中,形如a:
3:
{i:
0;i:
1241;i:
1;i:
4;i:
2;i:
16;}但是java需要取出这样的对象,转换成java的对象/*PHPSerializer.java
*
*Author:
MaBingyao
*Copyright:
CoolCode.CN
*Version:
2.1
*LastModified:
2006-08-09
*Thislibraryisfree.Youcanredistributeitand/ormodifyit.
*
*/
packageorg.phprpc.util;
importjava.io.*;
importjava.util.*;
importjava.lang.reflect.*;
classUnSerializeResult{
publicObjectvalue;
publicinthv;
publicUnSerializeResult(){}
publicUnSerializeResult(Objectvalue,inthv){
this.value=value;
this.hv=hv;
}
}
publicclassPHPSerializer{
privatestaticPackage[]__packages=Package.getPackages();
privatestaticfinalbyte__Quote=34;
privatestaticfinalbyte__0=48;
privatestaticfinalbyte__1=49;
privatestaticfinalbyte__Colon=58;
privatestaticfinalbyte__Semicolon=59;
privatestaticfinalbyte__C=67;
privatestaticfinalbyte__N=78;
privatestaticfinalbyte__O=79;
privatestaticfinalbyte__R=82;
privatestaticfinalbyte__U=85;
privatestaticfinalbyte__Slash=92;
privatestaticfinalbyte__a=97;
privatestaticfinalbyte__b=98;
privatestaticfinalbyte__d=100;
privatestaticfinalbyte__i=105;
privatestaticfinalbyte__r=114;
privatestaticfinalbyte__s=115;
privatestaticfinalbyte__LeftB=123;
privatestaticfinalbyte__RightB=125;
privatestaticfinalString__NAN=newString("NAN");
privatestaticfinalString__INF=newString("INF");
privatestaticfinalString__NINF=newString("-INF");
privatePHPSerializer(){}
publicstaticbyte[]serialize(Objectobj){
returnserialize(obj,"UTF-8");
}
publicstaticbyte[]serialize(Objectobj,Stringcharset){
HashMapht=newHashMap();
inthv=1;
ByteArrayOutputStreamstream=newByteArrayOutputStream();
hv=serialize(stream,obj,ht,hv,charset);
byte[]result=stream.toByteArray();
try{
stream.close();
}catch(Exceptione){}
returnresult;
}
publicstaticintserialize(ByteArrayOutputStreamstream,Objectobj,HashMapht,inthv,Stringcharset){
if(obj==null){
hv++;
writeNull(stream);
}else{
if(objinstanceofBoolean){
hv++;
writeBoolean(stream,((Boolean)obj).booleanValue()?
__1:
__0);
}elseif((objinstanceofByte)||(objinstanceofShort)
||(objinstanceofInteger)){
hv++;
writeInteger(stream,getBytes(obj));
}elseif(objinstanceofLong){
hv++;
writeDouble(stream,getBytes(obj));
}elseif(objinstanceofFloat){
hv++;
Floatf=(Float)obj;
if(f.isNaN()){
writeDouble(stream,getBytes(__NAN));
}elseif(f.isInfinite()){
if(f.floatValue()>0){
writeDouble(stream,getBytes(__INF));
}else{
writeDouble(stream,getBytes(__NINF));
}
}else{
writeDouble(stream,getBytes(f));
}
}elseif(objinstanceofDouble){
hv++;
Doubled=(Double)obj;
if(d.isNaN()){
writeDouble(stream,getBytes(__NAN));
}elseif(d.isInfinite()){
if(d.doubleValue()>0){
writeDouble(stream,getBytes(__INF));
}else{
writeDouble(stream,getBytes(__NINF));
}
}else{
writeDouble(stream,getBytes(d));
}
}elseif((objinstanceofCharacter)||(objinstanceofString)){
hv++;
writeString(stream,getBytes(obj,charset));
}elseif(obj.getClass().isArray()){
if(ht.containsKey(newInteger(obj.hashCode()))){
writePointRef(stream,getBytes(ht.get(newInteger(obj.hashCode()))));
}else{
ht.put(newInteger(obj.hashCode()),newInteger(hv++));
hv=writeArray(stream,obj,ht,hv,charset);
}
}elseif(objinstanceofArrayList){
if(ht.containsKey(newInteger(obj.hashCode()))){
writePointRef(stream,getBytes(ht.get(newInteger(obj.hashCode()))));
}else{
ht.put(newInteger(obj.hashCode()),newInteger(hv++));
hv=writeArrayList(stream,(ArrayList)obj,ht,hv,charset);
}
}elseif(objinstanceofHashMap){
if(ht.containsKey(newInteger(obj.hashCode()))){
writePointRef(stream,getBytes(ht.get(newInteger(obj.hashCode()))));
}else{
ht.put(newInteger(obj.hashCode()),newInteger(hv++));
hv=writeHashMap(stream,(HashMap)obj,ht,hv,charset);
}
}else{
if(ht.containsKey(newInteger(obj.hashCode()))){
hv++;
writeRef(stream,getBytes(ht.get(newInteger(obj.hashCode()))));
}else{
ht.put(newInteger(obj.hashCode()),newInteger(hv++));
hv=writeObject(stream,obj,ht,hv,charset);
}
}
}
returnhv;
}
privatestaticvoidwriteNull(ByteArrayOutputStreamstream){
stream.write(__N);
stream.write(__Semicolon);
}
privatestaticvoidwriteRef(ByteArrayOutputStreamstream,byte[]r){
stream.write(__r);
stream.write(__Colon);
stream.write(r,0,r.length);
stream.write(__Semicolon);
}
privatestaticvoidwritePointRef(ByteArrayOutputStreamstream,byte[]p){
stream.write(__R);
stream.write(__Colon);
stream.write(p,0,p.length);
stream.write(__Semicolon);
}
privatestaticvoidwriteBoolean(ByteArrayOutputStreamstream,byteb){
stream.write(__b);
stream.write(__Colon);
stream.write(b);
stream.write(__Semicolon);
}
privatestaticvoidwriteInteger(ByteArrayOutputStreamstream,byte[]i){
stream.write(__i);
stream.write(__Colon);
stream.write(i,0,i.length);
stream.write(__Semicolon);
}
privatestaticvoidwriteDouble(ByteArrayOutputStreamstream,byte[]d){
stream.write(__d);
stream.write(__Colon);
stream.write(d,0,d.length);
stream.write(__Semicolon);
}
privatestaticvoidwriteString(ByteArrayOutputStreamstream,byte[]s){
byte[]slen=getBytes(newInteger(s.length));
stream.write(__s);
stream.write(__Colon);
stream.write(slen,0,slen.length);
stream.write(__Colon);
stream.write(__Quote);
stream.write(s,0,s.length);
stream.write(__Quote);
stream.write(__Semicolon);
}
privatestaticintwriteArray(ByteArrayOutputStreamstream,Objecta,HashMapht,inthv,Stringcharset){
intlen=Array.getLength(a);
byte[]alen=getBytes(newInteger(len));
stream.write(__a);
stream.write(__Colon);
stream.write(alen,0,alen.length);
stream.write(__Colon);
stream.write(__LeftB);
for(inti=0;iwriteInteger(stream,getBytes(newInteger(i)));
hv=serialize(stream,Array.get(a,i),ht,hv,charset);
}
stream.write(__RightB);
returnhv;
}
privatestaticintwriteArrayList(ByteArrayOutputStreamstream,ArrayLista,HashMapht,inthv,Stringcharset){
intlen=a.size();
byte[]alen=getBytes(newInteger(len));
stream.write(__a);
stream.write(__Colon);
stream.write(alen,0,alen.length);
stream.write(__Colon);
stream.write(__LeftB);
for(inti=0;iwriteInteger(stream,getBytes(newInteger(i)));
hv=serialize(stream,a.get(i),ht,hv,charset);
}
stream.write(__RightB);
returnhv;
}
privatestaticintwriteHashMap(ByteArrayOutputStreamstream,HashMaph,HashMapht,inthv,Stringcharset){
intlen=h.size();
byte[]hlen=getBytes(newInteger(len));
stream.write(__a);
stream.write(__Colon);
stream.write(hlen,0,hlen.length);
stream.write(__Colon);
stream.write(__LeftB);
for(Iteratorkeys=h.keySet().iterator();keys.hasNext();){
Objectkey=keys.next();
if((keyinstanceofByte)||(keyinstanceofShort)
||(keyinstanceofInteger)){
writeInteger(stream,getBytes(key));
}elseif(keyinstanceofBoolean){
writeInteger(stream,newbyte[]{((Boolean)key).booleanValue()?
__1:
__0});
}else{
writeString(stream,getBytes(key,charset));
}
hv=serialize(stream,h.get(key),ht,hv,charset);
}
stream.write(__RightB);
returnhv;
}
privatestaticintwriteObject(ByteArrayOutputStreamstream,Objectobj,HashMapht,inthv,Stringcharset){
Classcls=obj.getClass();
if(objinstanceofjava.io.Serializable){
byte[]className=getBytes(getClassName(cls),charset);
byte[]classNameLen=getBytes(newInteger(className.length));
if(objinstanceoforg.phprpc.util.Serializable){
byte[]cs=((org.phprpc.util.Serializable)obj).serialize();
byte[]cslen=getBytes(newInteger(cs.length));
stream.write(__C);
stream.write(__Colon);
stream.write(classNameLen,0,classNameLen.length);
stream.write(__Colon);
stream.write(__Quote);
stream.write(className,0,className.length);
stream.write(__Quote);
stream.write(__Colon);
stream.write(cslen,0,cslen.length);
stream.write(__Colon);
stream.write(__LeftB);
stream.write(cs,0,cs.length);
stream.write(__RightB);
}else{
Method__sleep;
try{
__sleep=cls.getMethod("__sleep",newClass[0]);
}catch(Exceptione){
__sleep=null;
}
intfl=0;
Field[]f;
if(__sleep!
=null){
String[]fieldNames;
try{
__sleep.setAccessible(true);
fieldNames=(String[])__sleep.invoke(obj,newObject[0]);
}catch(Exceptione){
fieldNames=null;
}
f=getFields(obj,fieldNames);
}else{
f=getFields(obj);
}
AccessibleObject.setAccessible(f,true);
byte[]flen=getBytes(newInteger(f.length));
stream.write(__O);
stream.write(__Colon);
stream.write(classNameLen,0,classNameLen.length);
stream.write(__Colon);
stream.write(__Quote);
stream.write(className,0,className.length);
stream.write(__Quote);
stream.write(__Colon);
stream.write(flen,0,flen.length);
stream.write(__Colon);
stream.write(__LeftB);
for(inti=0,len=f.length;iintmod=f[i].getModifiers();
if(Modifier.isPublic(mod)){
writeString(stream,getBytes(f[i].getName(),charset));
}elseif(Modifier.isProtected(mod)){
writeString(stream,
getBytes("*"+f[i].getName(),charset));
}else{
writeString(stream,
getBytes(
""+getClassNa