2323import java .lang .reflect .Field ;
2424import java .util .Arrays ;
2525import java .util .List ;
26+ import java .util .HashMap ;
27+ import java .util .Map ;
2628
2729import com .esotericsoftware .kryo .Kryo ;
2830import com .esotericsoftware .kryo .Serializer ;
4042 */
4143public class ArraysAsListSerializer extends Serializer <List <?>> {
4244
45+ private static final Map <Class <?>, Class <?>> primitives = new HashMap <Class <?>, Class <?>>(8 , 1.0F );
46+
47+ static {
48+ primitives .put (byte .class , Byte .class );
49+ primitives .put (short .class , Short .class );
50+ primitives .put (int .class , Integer .class );
51+ primitives .put (long .class , Long .class );
52+ primitives .put (char .class , Character .class );
53+ primitives .put (float .class , Float .class );
54+ primitives .put (double .class , Double .class );
55+ primitives .put (boolean .class , Boolean .class );
56+ }
57+
4358 static public IKryoRegistrar registrar () {
4459 return new SingleRegistrar (Arrays .asList ("" ).getClass (), new ArraysAsListSerializer ());
4560 }
@@ -58,34 +73,42 @@ public ArraysAsListSerializer() {
5873 @ Override
5974 public List <?> read (final Kryo kryo , final Input input , final Class <List <?>> type ) {
6075 final int length = input .readInt (true );
61- final Class <?> componentType = kryo .readClass ( input ).getType ();
76+ Class <?> componentType = kryo .readClass (input ).getType ();
6277 try {
63- final Object [] items = ( Object []) Array .newInstance ( componentType , length );
64- for ( int i = 0 ; i < length ; i ++ ) {
65- items [ i ] = kryo .readClassAndObject ( input );
78+ final Object items = Array .newInstance (getBoxedClass ( componentType ) , length );
79+ for ( int i = 0 ; i < length ; i ++) {
80+ Array . set ( items , i , kryo .readClassAndObject (input ) );
6681 }
67- return Arrays .asList ( items );
68- } catch ( final Exception e ) {
69- throw new RuntimeException ( e );
82+ return Arrays .asList (( Object []) items );
83+ } catch (final Exception e ) {
84+ throw new RuntimeException (e );
7085 }
7186 }
7287
7388 @ Override
7489 public void write (final Kryo kryo , final Output output , final List <?> obj ) {
7590 try {
76- final Object [] array = (Object []) _arrayField .get ( obj );
91+ final Object [] array = (Object []) _arrayField .get (obj );
7792 output .writeInt (array .length , true );
7893 final Class <?> componentType = array .getClass ().getComponentType ();
79- kryo .writeClass ( output , componentType );
80- for ( final Object item : array ) {
81- kryo .writeClassAndObject ( output , item );
94+ kryo .writeClass (output , componentType );
95+ for ( final Object item : array ) {
96+ kryo .writeClassAndObject (output , item );
8297 }
83- } catch ( final RuntimeException e ) {
84- // Don't eat and wrap RuntimeExceptions because the ObjectBuffer.write...
85- // handles SerializationException specifically (resizing the buffer)...
86- throw e ;
87- } catch ( final Exception e ) {
88- throw new RuntimeException ( e );
98+ } catch (final RuntimeException e ) {
99+ // Don't eat and wrap RuntimeExceptions because the ObjectBuffer.write...
100+ // handles SerializationException specifically (resizing the buffer)...
101+ throw e ;
102+ } catch (final Exception e ) {
103+ throw new RuntimeException (e );
104+ }
105+ }
106+
107+ private static Class <?> getBoxedClass (final Class <?> c ) {
108+ if (c .isPrimitive ()) {
109+ Class <?> x ;
110+ return (x = primitives .get (c )) != null ? x : c ;
89111 }
112+ return c ;
90113 }
91114}
0 commit comments