📄 phpserializer.java
字号:
/* PHPSerializer.java
*
* Author: Ma Bingyao <andot@ujn.edu.cn>
* Copyright: CoolCode.CN
* Version: 2.1
* LastModified: 2006-08-09
* This library is free. You can redistribute it and/or modify it.
* http://www.coolcode.cn/?p=202
*/
package org.phprpc.util;
import java.io.*;
import java.util.*;
import java.lang.reflect.*;
class UnSerializeResult {
public Object value;
public int hv;
public UnSerializeResult() {}
public UnSerializeResult(Object value, int hv) {
this.value = value;
this.hv = hv;
}
}
public class PHPSerializer {
private static Package[] __packages = Package.getPackages();
private static final byte __Quote = 34;
private static final byte __0 = 48;
private static final byte __1 = 49;
private static final byte __Colon = 58;
private static final byte __Semicolon = 59;
private static final byte __C = 67;
private static final byte __N = 78;
private static final byte __O = 79;
private static final byte __R = 82;
private static final byte __U = 85;
private static final byte __Slash = 92;
private static final byte __a = 97;
private static final byte __b = 98;
private static final byte __d = 100;
private static final byte __i = 105;
private static final byte __r = 114;
private static final byte __s = 115;
private static final byte __LeftB = 123;
private static final byte __RightB = 125;
private static final String __NAN = new String("NAN");
private static final String __INF = new String("INF");
private static final String __NINF = new String("-INF");
private PHPSerializer() {}
public static byte[] serialize(Object obj) {
return serialize(obj, "UTF-8");
}
public static byte[] serialize(Object obj, String charset) {
HashMap ht = new HashMap();
int hv = 1;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
hv = serialize(stream, obj, ht, hv, charset);
byte[] result = stream.toByteArray();
try {
stream.close();
} catch (Exception e) {}
return result;
}
public static int serialize(ByteArrayOutputStream stream, Object obj, HashMap ht, int hv, String charset) {
if (obj == null) {
hv++;
writeNull(stream);
} else {
if (obj instanceof Boolean) {
hv++;
writeBoolean(stream, ((Boolean) obj).booleanValue() ? __1 : __0);
} else if ((obj instanceof Byte) || (obj instanceof Short)
|| (obj instanceof Integer)) {
hv++;
writeInteger(stream, getBytes(obj));
} else if (obj instanceof Long) {
hv++;
writeDouble(stream, getBytes(obj));
} else if (obj instanceof Float) {
hv++;
Float f = (Float) obj;
if (f.isNaN()) {
writeDouble(stream, getBytes(__NAN));
} else if (f.isInfinite()) {
if (f.floatValue() > 0) {
writeDouble(stream, getBytes(__INF));
} else {
writeDouble(stream, getBytes(__NINF));
}
} else {
writeDouble(stream, getBytes(f));
}
} else if (obj instanceof Double) {
hv++;
Double d = (Double) obj;
if (d.isNaN()) {
writeDouble(stream, getBytes(__NAN));
} else if (d.isInfinite()) {
if (d.doubleValue() > 0) {
writeDouble(stream, getBytes(__INF));
} else {
writeDouble(stream, getBytes(__NINF));
}
} else {
writeDouble(stream, getBytes(d));
}
} else if ((obj instanceof Character) || (obj instanceof String)) {
hv++;
writeString(stream, getBytes(obj, charset));
} else if (obj.getClass().isArray()) {
if (ht.containsKey(new Integer(obj.hashCode()))) {
writePointRef(stream, getBytes(ht.get(new Integer(obj.hashCode()))));
} else {
ht.put(new Integer(obj.hashCode()), new Integer(hv++));
hv = writeArray(stream, obj, ht, hv, charset);
}
} else if (obj instanceof ArrayList) {
if (ht.containsKey(new Integer(obj.hashCode()))) {
writePointRef(stream, getBytes(ht.get(new Integer(obj.hashCode()))));
} else {
ht.put(new Integer(obj.hashCode()), new Integer(hv++));
hv = writeArrayList(stream, (ArrayList) obj, ht, hv, charset);
}
} else if (obj instanceof HashMap) {
if (ht.containsKey(new Integer(obj.hashCode()))) {
writePointRef(stream, getBytes(ht.get(new Integer(obj.hashCode()))));
} else {
ht.put(new Integer(obj.hashCode()), new Integer(hv++));
hv = writeHashMap(stream, (HashMap) obj, ht, hv, charset);
}
} else {
if (ht.containsKey(new Integer(obj.hashCode()))) {
hv++;
writeRef(stream, getBytes(ht.get(new Integer(obj.hashCode()))));
} else {
ht.put(new Integer(obj.hashCode()), new Integer(hv++));
hv = writeObject(stream, obj, ht, hv, charset);
}
}
}
return hv;
}
private static void writeNull(ByteArrayOutputStream stream) {
stream.write(__N);
stream.write(__Semicolon);
}
private static void writeRef(ByteArrayOutputStream stream, byte[] r) {
stream.write(__r);
stream.write(__Colon);
stream.write(r, 0, r.length);
stream.write(__Semicolon);
}
private static void writePointRef(ByteArrayOutputStream stream, byte[] p) {
stream.write(__R);
stream.write(__Colon);
stream.write(p, 0, p.length);
stream.write(__Semicolon);
}
private static void writeBoolean(ByteArrayOutputStream stream, byte b) {
stream.write(__b);
stream.write(__Colon);
stream.write(b);
stream.write(__Semicolon);
}
private static void writeInteger(ByteArrayOutputStream stream, byte[] i) {
stream.write(__i);
stream.write(__Colon);
stream.write(i, 0, i.length);
stream.write(__Semicolon);
}
private static void writeDouble(ByteArrayOutputStream stream, byte[] d) {
stream.write(__d);
stream.write(__Colon);
stream.write(d, 0, d.length);
stream.write(__Semicolon);
}
private static void writeString(ByteArrayOutputStream stream, byte[] s) {
byte[] slen = getBytes(new Integer(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);
}
private static int writeArray(ByteArrayOutputStream stream, Object a, HashMap ht, int hv, String charset) {
int len = Array.getLength(a);
byte[] alen = getBytes(new Integer(len));
stream.write(__a);
stream.write(__Colon);
stream.write(alen, 0, alen.length);
stream.write(__Colon);
stream.write(__LeftB);
for (int i = 0; i < len; i++) {
writeInteger(stream, getBytes(new Integer(i)));
hv = serialize(stream, Array.get(a, i), ht, hv, charset);
}
stream.write(__RightB);
return hv;
}
private static int writeArrayList(ByteArrayOutputStream stream, ArrayList a, HashMap ht, int hv, String charset) {
int len = a.size();
byte[] alen = getBytes(new Integer(len));
stream.write(__a);
stream.write(__Colon);
stream.write(alen, 0, alen.length);
stream.write(__Colon);
stream.write(__LeftB);
for (int i = 0; i < len; i++) {
writeInteger(stream, getBytes(new Integer(i)));
hv = serialize(stream, a.get(i), ht, hv, charset);
}
stream.write(__RightB);
return hv;
}
private static int writeHashMap(ByteArrayOutputStream stream, HashMap h, HashMap ht, int hv, String charset) {
int len = h.size();
byte[] hlen = getBytes(new Integer(len));
stream.write(__a);
stream.write(__Colon);
stream.write(hlen, 0, hlen.length);
stream.write(__Colon);
stream.write(__LeftB);
for (Iterator keys = h.keySet().iterator(); keys.hasNext();) {
Object key = keys.next();
if ((key instanceof Byte) || (key instanceof Short)
|| (key instanceof Integer)) {
writeInteger(stream, getBytes(key));
} else if (key instanceof Boolean) {
writeInteger(stream, new byte[] { ((Boolean) key).booleanValue() ? __1 : __0 });
} else {
writeString(stream, getBytes(key, charset));
}
hv = serialize(stream, h.get(key), ht, hv, charset);
}
stream.write(__RightB);
return hv;
}
private static int writeObject(ByteArrayOutputStream stream, Object obj, HashMap ht, int hv, String charset) {
Class cls = obj.getClass();
if (obj instanceof java.io.Serializable) {
byte[] className = getBytes(getClassName(cls), charset);
byte[] classNameLen = getBytes(new Integer(className.length));
if (obj instanceof org.phprpc.util.Serializable) {
byte[] cs = ((org.phprpc.util.Serializable) obj).serialize();
byte[] cslen = getBytes(new Integer(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", new Class[0]);
} catch (Exception e) {
__sleep = null;
}
int fl = 0;
Field[] f;
if (__sleep != null) {
String[] fieldNames;
try {
__sleep.setAccessible(true);
fieldNames = (String[]) __sleep.invoke(obj, new Object[0]);
} catch (Exception e) {
fieldNames = null;
}
f = getFields(obj, fieldNames);
} else {
f = getFields(obj);
}
AccessibleObject.setAccessible(f, true);
byte[] flen = getBytes(new Integer(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 (int i = 0, len = f.length; i < len; i++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -