公司其他组有个公共的maven依赖包中的序列化与反序列化采用了kryo,所以我在新代码中也测试了下kryo,测试的时候有几个问题记录如下:
1.kryo反序列化时是通过反射默认调用类的无参构造函数,如果类或者类的对象属性没有无参构造构造函数,则会抛出反序列化失败的异常,解决方法可以写一个继承kryo类的子类,重新实现通过反射去获取实例的方法,参考博客:https://vyloy.iteye.com/blog/1483029
2.kryo在序列化和反序列化ArrayList和HashMap时需注册ArrayList.class和HashMap.class的序列化类,才可序列化和反序列化成功
MapSerializer serializer = new MapSerializer();
serializer.setKeyClass(String.class, new JavaSerializer());
serializer.setKeysCanBeNull(false);
serializer.setValueClass(clazz, new BeanSerializer<T>(kryo, clazz));
serializer.setValuesCanBeNull(true);
kryo.register(HashMap.class, serializer);
CollectionSerializer serializer = new CollectionSerializer();
serializer.setElementClass(clazz, new BeanSerializer<T>(kryo, clazz));
serializer.setElementsCanBeNull(false);
kryo.register(clazz, new BeanSerializer<T>(kryo, clazz));
kryo.register(ArrayList.class, serializer);
3.序列化的ArrayList的字节流过大时,需显示设置输出流的大小,否则可能因默认流限制导致字节流传送有误,反序列化失败
ByteArrayOutputStream byteBufferOutputStream = new ByteArrayOutputStream();
//设置输出流大小限制为100000字节
Output output = new Output(byteBufferOutputStream,100000);
kryo.writeObject(output, list);
//kryo中io目录下的output默认设置buffersize为4096字节
public Output(OutputStream outputStream) {
this(4096, 4096);
if (outputStream == null) {
throw new IllegalArgumentException("outputStream cannot be null.");
} else {
this.outputStream = outputStream;
}
}
4.传输对象的List属性不能用Arrays.asList(array)形式生成,Arrays.asList(array)的返回结果是Arrays的一个内部类,不是集合ArrayList,用Arrays内部类会导致反序列化异常
//这是Arrays中实现了RandomAccess和java.io.Serializable的一个内部类
/**
* @serial include
*/
private static class ArrayList<E> extends AbstractList<E>
implements RandomAccess, java.io.Serializable
{
private static final long serialVersionUID = -2764017481108945198L;
private final E[] a;
ArrayList(E[] array) {
a = Objects.requireNonNull(array);
}
@Override
public int size() {
return a.length;
}
@Override
public Object[] toArray() {
return a.clone();
}
@Override
@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
int size = size();
if (a.length < size)
return Arrays.copyOf(this.a, size,
(Class<? extends T[]>) a.getClass());
System.arraycopy(this.a, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}
@Override
public E get(int index) {
return a[index];
}
@Override
public E set(int index, E element) {
E oldValue = a[index];
a[index] = element;
return oldValue;
}
@Override
public int indexOf(Object o) {
E[] a = this.a;
if (o == null) {
for (int i = 0; i < a.length; i++)
if (a[i] == null)
return i;
} else {
for (int i = 0; i < a.length; i++)
if (o.equals(a[i]))
return i;
}
return -1;
}
@Override
public boolean contains(Object o) {
return indexOf(o) != -1;
}
@Override
public Spliterator<E> spliterator() {
return Spliterators.spliterator(a, Spliterator.ORDERED);
}
@Override
public void forEach(Consumer<? super E> action) {
Objects.requireNonNull(action);
for (E e : a) {
action.accept(e);
}
}
@Override
public void replaceAll(UnaryOperator<E> operator) {
Objects.requireNonNull(operator);
E[] a = this.a;
for (int i = 0; i < a.length; i++) {
a[i] = operator.apply(a[i]);
}
}
@Override
public void sort(Comparator<? super E> c) {
Arrays.sort(a, c);
}
}