import java.io.*; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map;
public class CustomObjectOutputStream extends ObjectOutputStream {
public CustomObjectOutputStream(OutputStream out) throws IOException { super(out); }
private static HashMap<Character, int[]> map; private static Map<Character,int[]> bytesMap=new HashMap<>();
static { map = new HashMap<>(); map.put('.', new int[]{0xc0, 0xae}); map.put(';', new int[]{0xc0, 0xbb}); map.put('$', new int[]{0xc0, 0xa4}); map.put('[', new int[]{0xc1, 0x9b}); map.put(']', new int[]{0xc1, 0x9d}); map.put('a', new int[]{0xc1, 0xa1}); map.put('b', new int[]{0xc1, 0xa2}); map.put('c', new int[]{0xc1, 0xa3}); map.put('d', new int[]{0xc1, 0xa4}); map.put('e', new int[]{0xc1, 0xa5}); map.put('f', new int[]{0xc1, 0xa6}); map.put('g', new int[]{0xc1, 0xa7}); map.put('h', new int[]{0xc1, 0xa8}); map.put('i', new int[]{0xc1, 0xa9}); map.put('j', new int[]{0xc1, 0xaa}); map.put('k', new int[]{0xc1, 0xab}); map.put('l', new int[]{0xc1, 0xac}); map.put('m', new int[]{0xc1, 0xad}); map.put('n', new int[]{0xc1, 0xae}); map.put('o', new int[]{0xc1, 0xaf}); map.put('p', new int[]{0xc1, 0xb0}); map.put('q', new int[]{0xc1, 0xb1}); map.put('r', new int[]{0xc1, 0xb2}); map.put('s', new int[]{0xc1, 0xb3}); map.put('t', new int[]{0xc1, 0xb4}); map.put('u', new int[]{0xc1, 0xb5}); map.put('v', new int[]{0xc1, 0xb6}); map.put('w', new int[]{0xc1, 0xb7}); map.put('x', new int[]{0xc1, 0xb8}); map.put('y', new int[]{0xc1, 0xb9}); map.put('z', new int[]{0xc1, 0xba}); map.put('A', new int[]{0xc1, 0x81}); map.put('B', new int[]{0xc1, 0x82}); map.put('C', new int[]{0xc1, 0x83}); map.put('D', new int[]{0xc1, 0x84}); map.put('E', new int[]{0xc1, 0x85}); map.put('F', new int[]{0xc1, 0x86}); map.put('G', new int[]{0xc1, 0x87}); map.put('H', new int[]{0xc1, 0x88}); map.put('I', new int[]{0xc1, 0x89}); map.put('J', new int[]{0xc1, 0x8a}); map.put('K', new int[]{0xc1, 0x8b}); map.put('L', new int[]{0xc1, 0x8c}); map.put('M', new int[]{0xc1, 0x8d}); map.put('N', new int[]{0xc1, 0x8e}); map.put('O', new int[]{0xc1, 0x8f}); map.put('P', new int[]{0xc1, 0x90}); map.put('Q', new int[]{0xc1, 0x91}); map.put('R', new int[]{0xc1, 0x92}); map.put('S', new int[]{0xc1, 0x93}); map.put('T', new int[]{0xc1, 0x94}); map.put('U', new int[]{0xc1, 0x95}); map.put('V', new int[]{0xc1, 0x96}); map.put('W', new int[]{0xc1, 0x97}); map.put('X', new int[]{0xc1, 0x98}); map.put('Y', new int[]{0xc1, 0x99}); map.put('Z', new int[]{0xc1, 0x9a});
bytesMap.put('$', new int[]{0xe0,0x80,0xa4}); bytesMap.put('.', new int[]{0xe0,0x80,0xae}); bytesMap.put(';', new int[]{0xe0,0x80,0xbb}); bytesMap.put('A', new int[]{0xe0,0x81,0x81}); bytesMap.put('B', new int[]{0xe0,0x81,0x82}); bytesMap.put('C', new int[]{0xe0,0x81,0x83}); bytesMap.put('D', new int[]{0xe0,0x81,0x84}); bytesMap.put('E', new int[]{0xe0,0x81,0x85}); bytesMap.put('F', new int[]{0xe0,0x81,0x86}); bytesMap.put('G', new int[]{0xe0,0x81,0x87}); bytesMap.put('H', new int[]{0xe0,0x81,0x88}); bytesMap.put('I', new int[]{0xe0,0x81,0x89}); bytesMap.put('J', new int[]{0xe0,0x81,0x8a}); bytesMap.put('K', new int[]{0xe0,0x81,0x8b}); bytesMap.put('L', new int[]{0xe0,0x81,0x8c}); bytesMap.put('M', new int[]{0xe0,0x81,0x8d}); bytesMap.put('N', new int[]{0xe0,0x81,0x8e}); bytesMap.put('O', new int[]{0xe0,0x81,0x8f}); bytesMap.put('P', new int[]{0xe0,0x81,0x90}); bytesMap.put('Q', new int[]{0xe0,0x81,0x91}); bytesMap.put('R', new int[]{0xe0,0x81,0x92}); bytesMap.put('S', new int[]{0xe0,0x81,0x93}); bytesMap.put('T', new int[]{0xe0,0x81,0x94}); bytesMap.put('U', new int[]{0xe0,0x81,0x95}); bytesMap.put('V', new int[]{0xe0,0x81,0x96}); bytesMap.put('W', new int[]{0xe0,0x81,0x97}); bytesMap.put('X', new int[]{0xe0,0x81,0x98}); bytesMap.put('Y', new int[]{0xe0,0x81,0x99}); bytesMap.put('Z', new int[]{0xe0,0x81,0x9a}); bytesMap.put('[', new int[]{0xe0,0x81,0x9b}); bytesMap.put(']', new int[]{0xe0,0x81,0x9d}); bytesMap.put('a', new int[]{0xe0,0x81,0xa1}); bytesMap.put('b', new int[]{0xe0,0x81,0xa2}); bytesMap.put('c', new int[]{0xe0,0x81,0xa3}); bytesMap.put('d', new int[]{0xe0,0x81,0xa4}); bytesMap.put('e', new int[]{0xe0,0x81,0xa5}); bytesMap.put('f', new int[]{0xe0,0x81,0xa6}); bytesMap.put('g', new int[]{0xe0,0x81,0xa7}); bytesMap.put('h', new int[]{0xe0,0x81,0xa8}); bytesMap.put('i', new int[]{0xe0,0x81,0xa9}); bytesMap.put('j', new int[]{0xe0,0x81,0xaa}); bytesMap.put('k', new int[]{0xe0,0x81,0xab}); bytesMap.put('l', new int[]{0xe0,0x81,0xac}); bytesMap.put('m', new int[]{0xe0,0x81,0xad}); bytesMap.put('n', new int[]{0xe0,0x81,0xae}); bytesMap.put('o', new int[]{0xe0,0x81,0xaf}); bytesMap.put('p', new int[]{0xe0,0x81,0xb0}); bytesMap.put('q', new int[]{0xe0,0x81,0xb1}); bytesMap.put('r', new int[]{0xe0,0x81,0xb2}); bytesMap.put('s', new int[]{0xe0,0x81,0xb3}); bytesMap.put('t', new int[]{0xe0,0x81,0xb4}); bytesMap.put('u', new int[]{0xe0,0x81,0xb5}); bytesMap.put('v', new int[]{0xe0,0x81,0xb6}); bytesMap.put('w', new int[]{0xe0,0x81,0xb7}); bytesMap.put('x', new int[]{0xe0,0x81,0xb8}); bytesMap.put('y', new int[]{0xe0,0x81,0xb9}); bytesMap.put('z', new int[]{0xe0,0x81,0xba});
}
public void charWritTwoBytes(String name){ byte[] bytes=new byte[name.length() * 2]; int k=0; StringBuffer str=new StringBuffer(); for (int i = 0; i < name.length(); i++) { int[] bs = map.get(name.charAt(i)); bytes[k++]= (byte) bs[0]; bytes[k++]= (byte) bs[1]; str.append(Integer.toHexString(bs[0])+","); str.append(Integer.toHexString(bs[1])+","); } System.out.println(str.toString()); try { writeShort(name.length() * 2); write(bytes); } catch (IOException e) { throw new RuntimeException(e); }
} public void charWriteThreeBytes(String name){ byte[] bytes=new byte[name.length() * 3]; int k=0; StringBuffer str=new StringBuffer(); for (int i = 0; i < name.length(); i++) { int[] bs = bytesMap.get(name.charAt(i)); bytes[k++]= (byte) bs[0]; bytes[k++]= (byte) bs[1]; bytes[k++]= (byte) bs[2]; str.append(Integer.toHexString(bs[0])+","); str.append(Integer.toHexString(bs[1])+","); str.append(Integer.toHexString(bs[2])+","); } System.out.println(str.toString()); try { writeShort(name.length() * 3); write(bytes); } catch (IOException e) { throw new RuntimeException(e); } }
protected void writeClassDescriptor(ObjectStreamClass desc) throws IOException { String name = desc.getName(); boolean externalizable = (boolean) getFieldValue(desc, "externalizable"); boolean serializable = (boolean) getFieldValue(desc, "serializable"); boolean hasWriteObjectData = (boolean) getFieldValue(desc, "hasWriteObjectData"); boolean isEnum = (boolean) getFieldValue(desc, "isEnum"); ObjectStreamField[] fields = (ObjectStreamField[]) getFieldValue(desc, "fields"); System.out.println(name);
charWriteThreeBytes(name);
writeLong(desc.getSerialVersionUID()); byte flags = 0; if (externalizable) { flags |= ObjectStreamConstants.SC_EXTERNALIZABLE; Field protocolField = null; int protocol; try { protocolField = ObjectOutputStream.class.getDeclaredField("protocol"); protocolField.setAccessible(true); protocol = (int) protocolField.get(this); } catch (NoSuchFieldException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } if (protocol != ObjectStreamConstants.PROTOCOL_VERSION_1) { flags |= ObjectStreamConstants.SC_BLOCK_DATA; } } else if (serializable) { flags |= ObjectStreamConstants.SC_SERIALIZABLE; } if (hasWriteObjectData) { flags |= ObjectStreamConstants.SC_WRITE_METHOD; } if (isEnum) { flags |= ObjectStreamConstants.SC_ENUM; } writeByte(flags);
writeShort(fields.length); for (int i = 0; i < fields.length; i++) { ObjectStreamField f = fields[i]; writeByte(f.getTypeCode()); writeUTF(f.getName()); if (!f.isPrimitive()) { invoke(this, "writeTypeString", f.getTypeString()); } } }
public static void invoke(Object object, String methodName, Object... args) { Method writeTypeString = null; try { writeTypeString = ObjectOutputStream.class.getDeclaredMethod(methodName, String.class); writeTypeString.setAccessible(true); try { writeTypeString.invoke(object, args); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } } catch (NoSuchMethodException e) { throw new RuntimeException(e); } }
public static Object getFieldValue(Object object, String fieldName) { Class<?> clazz = object.getClass(); Field field = null; Object value = null; try { field = clazz.getDeclaredField(fieldName); field.setAccessible(true); value = field.get(object); } catch (NoSuchFieldException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } return value; } }
|