All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.javakaffee.kryoserializers.ArraysAsListSerializer Maven / Gradle / Ivy

Go to download

Additional kryo (http://kryo.googlecode.com) serializers for standard jdk types (e.g. currency, jdk proxies) and some for external libs (e.g. joda time, cglib proxies, wicket).

The newest version!
/*
 * Copyright 2010 Martin Grotzke
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package de.javakaffee.kryoserializers;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;

/**
 * A kryo {@link Serializer} for lists created via {@link Arrays#asList(Object...)}.
 * 

* Note: This serializer does not support cyclic references, so if one of the objects * gets set the list as attribute this might cause an error during deserialization. *

* * @author Martin Grotzke */ public class ArraysAsListSerializer extends Serializer> { private Field _arrayField; public ArraysAsListSerializer() { try { _arrayField = Class.forName( "java.util.Arrays$ArrayList" ).getDeclaredField( "a" ); _arrayField.setAccessible( true ); } catch ( final Exception e ) { throw new RuntimeException( e ); } } @Override public List read(final Kryo kryo, final Input input, final Class> type) { final int length = input.readInt(true); Class componentType = kryo.readClass( input ).getType(); if (componentType.isPrimitive()) { componentType = getPrimitiveWrapperClass(componentType); } try { final Object items = Array.newInstance( componentType, length ); for( int i = 0; i < length; i++ ) { Array.set(items, i, kryo.readClassAndObject( input )); } return Arrays.asList( (Object[])items ); } catch ( final Exception e ) { throw new RuntimeException( e ); } } @Override public void write(final Kryo kryo, final Output output, final List obj) { try { final Object[] array = (Object[]) _arrayField.get( obj ); output.writeInt(array.length, true); final Class componentType = array.getClass().getComponentType(); kryo.writeClass( output, componentType ); for( final Object item : array ) { kryo.writeClassAndObject( output, item ); } } catch ( final RuntimeException e ) { // Don't eat and wrap RuntimeExceptions because the ObjectBuffer.write... // handles SerializationException specifically (resizing the buffer)... throw e; } catch ( final Exception e ) { throw new RuntimeException( e ); } } @Override public List copy(Kryo kryo, List original) { try { final Object[] array = (Object[]) _arrayField.get(original); kryo.reference(array); Object[] arrayCopy = kryo.copy(array); return Arrays.asList(arrayCopy); } catch (final Exception e) { throw new RuntimeException(e); } } private static Class getPrimitiveWrapperClass(final Class c) { if (c.isPrimitive()) { if (c.equals(Long.TYPE)) { return Long.class; } else if (c.equals(Integer.TYPE)) { return Integer.class; } else if (c.equals(Double.TYPE)) { return Double.class; } else if (c.equals(Float.TYPE)) { return Float.class; } else if (c.equals(Boolean.TYPE)) { return Boolean.class; } else if (c.equals(Character.TYPE)) { return Character.class; } else if (c.equals(Short.TYPE)) { return Short.class; } else if (c.equals(Byte.TYPE)) { return Byte.class; } } return c; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy