|
| 1 | +package com.threedr3am.bug.feature; |
| 2 | + |
| 3 | +import com.caucho.hessian.io.AbstractHessianOutput; |
| 4 | +import com.caucho.hessian.io.HessianOutput; |
| 5 | +import com.caucho.hessian.io.HessianProtocolException; |
| 6 | +import com.caucho.hessian.io.Serializer; |
| 7 | +import com.caucho.hessian.io.SerializerFactory; |
| 8 | +import com.caucho.hessian.io.UnsafeSerializer; |
| 9 | +import com.caucho.hessian.io.WriteReplaceSerializer; |
| 10 | +import java.io.ByteArrayOutputStream; |
| 11 | +import java.io.IOException; |
| 12 | +import java.io.Serializable; |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.Map; |
| 15 | + |
| 16 | +/** |
| 17 | + * |
| 18 | + * Hessian序列化数据特征 |
| 19 | + * |
| 20 | + * 4d 74 00 ... 7a |
| 21 | + * |
| 22 | + * @author threedr3am |
| 23 | + */ |
| 24 | +public class HessianSerialization implements Serializable { |
| 25 | + |
| 26 | + public static void main(String[] args) throws IOException { |
| 27 | + printAndMatch(object1()); |
| 28 | + printAndMatch(object1_()); |
| 29 | + printAndMatch(object2()); |
| 30 | + printAndMatch(object2_()); |
| 31 | + printAndMatch(object3()); |
| 32 | + printAndMatch(object3_()); |
| 33 | + printAndMatch(object4()); |
| 34 | + printAndMatch(object4_()); |
| 35 | + printAndMatch(object5()); |
| 36 | + printAndMatch(object5_()); |
| 37 | + printAndMatch(object6()); |
| 38 | + printAndMatch(object6_()); |
| 39 | + } |
| 40 | + |
| 41 | + private static void printAndMatch(byte[] bytes) { |
| 42 | + StringBuilder stringBuilder = new StringBuilder(); |
| 43 | + for (int i = 0; i < bytes.length; i++) { |
| 44 | + stringBuilder.append(String.format("\\x%x ", bytes[i])); |
| 45 | + } |
| 46 | + System.out.println(stringBuilder.toString()); |
| 47 | + System.out.println(stringBuilder.toString().replaceAll(" ", "").contains("\\x4d\\x74\\x0")); |
| 48 | + } |
| 49 | + |
| 50 | + private static byte[] object1() throws IOException { |
| 51 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 52 | + AbstractHessianOutput out = new HessianOutput(bos); |
| 53 | + NoWriteReplaceSerializerFactory sf = new NoWriteReplaceSerializerFactory(); |
| 54 | + sf.setAllowNonSerializable(true); |
| 55 | + out.setSerializerFactory(sf); |
| 56 | + out.writeString("test"); |
| 57 | + out.close(); |
| 58 | + return bos.toByteArray(); |
| 59 | + } |
| 60 | + |
| 61 | + private static byte[] object1_() throws IOException { |
| 62 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 63 | + AbstractHessianOutput out = new HessianOutput(bos); |
| 64 | + out.writeString("test"); |
| 65 | + out.close(); |
| 66 | + return bos.toByteArray(); |
| 67 | + } |
| 68 | + |
| 69 | + private static byte[] object1_2() throws IOException { |
| 70 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 71 | + AbstractHessianOutput out = new HessianOutput(bos); |
| 72 | + out.writeString("threedr3am"); |
| 73 | + out.close(); |
| 74 | + return bos.toByteArray(); |
| 75 | + } |
| 76 | + |
| 77 | + private static byte[] object2() throws IOException { |
| 78 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 79 | + AbstractHessianOutput out = new HessianOutput(bos); |
| 80 | + NoWriteReplaceSerializerFactory sf = new NoWriteReplaceSerializerFactory(); |
| 81 | + sf.setAllowNonSerializable(true); |
| 82 | + out.setSerializerFactory(sf); |
| 83 | + out.writeObject(new A()); |
| 84 | + out.close(); |
| 85 | + return bos.toByteArray(); |
| 86 | + } |
| 87 | + |
| 88 | + private static byte[] object2_() throws IOException { |
| 89 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 90 | + AbstractHessianOutput out = new HessianOutput(bos); |
| 91 | + out.writeObject(new A()); |
| 92 | + out.close(); |
| 93 | + return bos.toByteArray(); |
| 94 | + } |
| 95 | + |
| 96 | + private static byte[] object3() throws IOException { |
| 97 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 98 | + AbstractHessianOutput out = new HessianOutput(bos); |
| 99 | + NoWriteReplaceSerializerFactory sf = new NoWriteReplaceSerializerFactory(); |
| 100 | + sf.setAllowNonSerializable(true); |
| 101 | + out.setSerializerFactory(sf); |
| 102 | + out.writeObject(new B(new A())); |
| 103 | + out.close(); |
| 104 | + return bos.toByteArray(); |
| 105 | + } |
| 106 | + |
| 107 | + private static byte[] object3_() throws IOException { |
| 108 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 109 | + AbstractHessianOutput out = new HessianOutput(bos); |
| 110 | + out.writeObject(new B(new A())); |
| 111 | + out.close(); |
| 112 | + return bos.toByteArray(); |
| 113 | + } |
| 114 | + |
| 115 | + private static byte[] object4() throws IOException { |
| 116 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 117 | + AbstractHessianOutput out = new HessianOutput(bos); |
| 118 | + NoWriteReplaceSerializerFactory sf = new NoWriteReplaceSerializerFactory(); |
| 119 | + sf.setAllowNonSerializable(true); |
| 120 | + out.setSerializerFactory(sf); |
| 121 | + out.writeObject(new HessianSerialization()); |
| 122 | + out.close(); |
| 123 | + return bos.toByteArray(); |
| 124 | + } |
| 125 | + |
| 126 | + private static byte[] object4_() throws IOException { |
| 127 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 128 | + AbstractHessianOutput out = new HessianOutput(bos); |
| 129 | + out.writeObject(new HessianSerialization()); |
| 130 | + out.close(); |
| 131 | + return bos.toByteArray(); |
| 132 | + } |
| 133 | + |
| 134 | + private static byte[] object5() throws IOException { |
| 135 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 136 | + AbstractHessianOutput out = new HessianOutput(bos); |
| 137 | + NoWriteReplaceSerializerFactory sf = new NoWriteReplaceSerializerFactory(); |
| 138 | + sf.setAllowNonSerializable(true); |
| 139 | + out.setSerializerFactory(sf); |
| 140 | + Map<String, String> map = new HashMap<>(); |
| 141 | + map.put("test", "test"); |
| 142 | + map.put("foo", "foo"); |
| 143 | + out.writeObject(map); |
| 144 | + out.close(); |
| 145 | + return bos.toByteArray(); |
| 146 | + } |
| 147 | + |
| 148 | + private static byte[] object5_() throws IOException { |
| 149 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 150 | + AbstractHessianOutput out = new HessianOutput(bos); |
| 151 | + Map<String, String> map = new HashMap<>(); |
| 152 | + map.put("test", "test"); |
| 153 | + map.put("foo", "foo"); |
| 154 | + out.writeObject(map); |
| 155 | + out.close(); |
| 156 | + return bos.toByteArray(); |
| 157 | + } |
| 158 | + |
| 159 | + private static byte[] object6() throws IOException { |
| 160 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 161 | + AbstractHessianOutput out = new HessianOutput(bos); |
| 162 | + NoWriteReplaceSerializerFactory sf = new NoWriteReplaceSerializerFactory(); |
| 163 | + sf.setAllowNonSerializable(true); |
| 164 | + out.setSerializerFactory(sf); |
| 165 | + Map<String, String> map = new HashMap<>(); |
| 166 | + map.put("test", "test"); |
| 167 | + out.writeObject(map); |
| 168 | + out.close(); |
| 169 | + return bos.toByteArray(); |
| 170 | + } |
| 171 | + |
| 172 | + private static byte[] object6_() throws IOException { |
| 173 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 174 | + AbstractHessianOutput out = new HessianOutput(bos); |
| 175 | + Map<String, String> map = new HashMap<>(); |
| 176 | + map.put("test", "test"); |
| 177 | + out.writeObject(map); |
| 178 | + out.close(); |
| 179 | + return bos.toByteArray(); |
| 180 | + } |
| 181 | + |
| 182 | + static class A implements Serializable {} |
| 183 | + static class B implements Serializable { |
| 184 | + A a; |
| 185 | + |
| 186 | + public B(A a) { |
| 187 | + this.a = a; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + public static class NoWriteReplaceSerializerFactory extends SerializerFactory { |
| 192 | + |
| 193 | + /** |
| 194 | + * {@inheritDoc} |
| 195 | + * |
| 196 | + * @see com.caucho.hessian.io.SerializerFactory#getObjectSerializer(java.lang.Class) |
| 197 | + */ |
| 198 | + @Override |
| 199 | + public Serializer getObjectSerializer ( Class<?> cl ) throws HessianProtocolException { |
| 200 | + return super.getObjectSerializer(cl); |
| 201 | + } |
| 202 | + |
| 203 | + |
| 204 | + /** |
| 205 | + * {@inheritDoc} |
| 206 | + * |
| 207 | + * @see com.caucho.hessian.io.SerializerFactory#getSerializer(java.lang.Class) |
| 208 | + */ |
| 209 | + @Override |
| 210 | + public Serializer getSerializer ( Class cl ) throws HessianProtocolException { |
| 211 | + Serializer serializer = super.getSerializer(cl); |
| 212 | + |
| 213 | + if ( serializer instanceof WriteReplaceSerializer) { |
| 214 | + return UnsafeSerializer.create(cl); |
| 215 | + } |
| 216 | + return serializer; |
| 217 | + } |
| 218 | + |
| 219 | + } |
| 220 | + |
| 221 | + |
| 222 | +} |
0 commit comments