159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/*
259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Copyright (c) 2009-2010 jMonkeyEngine, Java Game Networking
359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * All rights reserved.
459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Redistribution and use in source and binary forms, with or without
659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * modification, are permitted provided that the following conditions are
759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * met:
859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions of source code must retain the above copyright
1059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   notice, this list of conditions and the following disclaimer.
1159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
1259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions in binary form must reproduce the above copyright
1359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   notice, this list of conditions and the following disclaimer in the
1459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   documentation and/or other materials provided with the distribution.
1559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
1659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
1759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   may be used to endorse or promote products derived from this software
1859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   without specific prior written permission.
1959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
2059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
2459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
2759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
2859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */
3259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
3359b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapackage com.jme3.network.serializing.serializers;
3459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
3559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.network.serializing.Serializer;
3659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.io.IOException;
3759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.lang.reflect.Array;
3859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.lang.reflect.Modifier;
3959b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.nio.ByteBuffer;
4059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
4159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/**
4259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Array serializer
4359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
4459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @author Nathan Sweet
4559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */
4659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta@SuppressWarnings("unchecked")
4759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapublic class ArraySerializer extends Serializer {
4859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private int[] getDimensions (Object array) {
4959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        int depth = 0;
5059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Class nextClass = array.getClass().getComponentType();
5159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        while (nextClass != null) {
5259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            depth++;
5359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            nextClass = nextClass.getComponentType();
5459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
5559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        int[] dimensions = new int[depth];
5659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        dimensions[0] = Array.getLength(array);
5759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (depth > 1) collectDimensions(array, 1, dimensions);
5859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return dimensions;
5959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
6059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
6159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private void collectDimensions (Object array, int dimension, int[] dimensions) {
6259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        boolean elementsAreArrays = dimension < dimensions.length - 1;
6359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        for (int i = 0, s = Array.getLength(array); i < s; i++) {
6459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            Object element = Array.get(array, i);
6559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            if (element == null) continue;
6659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            dimensions[dimension] = Math.max(dimensions[dimension], Array.getLength(element));
6759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            if (elementsAreArrays) collectDimensions(element, dimension + 1, dimensions);
6859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
6959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
7059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
7159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public <T> T readObject(ByteBuffer data, Class<T> c) throws IOException {
7259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        byte dimensionCount = data.get();
7359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (dimensionCount == 0)
7459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            return null;
7559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
7659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        int[] dimensions = new int[dimensionCount];
7759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        for (int i = 0; i < dimensionCount; i++)
7859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                dimensions[i] = data.getInt();
7959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
8059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Serializer elementSerializer = null;
8159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
8259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Class elementClass = c;
8359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        while (elementClass.getComponentType() != null)
8459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            elementClass = elementClass.getComponentType();
8559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
8659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (Modifier.isFinal(elementClass.getModifiers())) elementSerializer = Serializer.getSerializer(elementClass);
8759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        // Create array and read in the data.
8859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        T array = (T)Array.newInstance(elementClass, dimensions);
8959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        readArray(elementSerializer, elementClass, data, array, 0, dimensions);
9059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return array;
9159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
9259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
9359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void writeObject(ByteBuffer buffer, Object object) throws IOException {
9459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (object == null){
9559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            buffer.put((byte)0);
9659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            return;
9759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
9859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
9959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        int[] dimensions = getDimensions(object);
10059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        buffer.put((byte)dimensions.length);
10159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        for (int dimension : dimensions) buffer.putInt(dimension);
10259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Serializer elementSerializer = null;
10359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
10459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Class elementClass = object.getClass();
10559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        while (elementClass.getComponentType() != null) {
10659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            elementClass = elementClass.getComponentType();
10759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
10859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
10959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (Modifier.isFinal(elementClass.getModifiers())) elementSerializer = Serializer.getSerializer(elementClass);
11059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        writeArray(elementSerializer, buffer, object, 0, dimensions.length);
11159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
11259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
11359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private void writeArray(Serializer elementSerializer, ByteBuffer buffer, Object array, int dimension, int dimensionCount) throws IOException {
11459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        int length = Array.getLength(array);
11559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (dimension > 0) {
11659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            buffer.putInt(length);
11759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
11859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        // Write array data.
11959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        boolean elementsAreArrays = dimension < dimensionCount - 1;
12059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        for (int i = 0; i < length; i++) {
12159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            Object element = Array.get(array, i);
12259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            if (elementsAreArrays) {
12359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                if (element != null) writeArray(elementSerializer, buffer, element, dimension + 1, dimensionCount);
12459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            } else if (elementSerializer != null) {
12559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                elementSerializer.writeObject(buffer, element);
12659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            } else {
12759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                // Each element could be a different type. Store the class with the object.
12859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                Serializer.writeClassAndObject(buffer, element);
12959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            }
13059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
13159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
13259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
13359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private void readArray (Serializer elementSerializer, Class elementClass, ByteBuffer buffer, Object array, int dimension, int[] dimensions) throws IOException {
13459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        boolean elementsAreArrays = dimension < dimensions.length - 1;
13559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        int length;
13659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (dimension == 0) {
13759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            length = dimensions[0];
13859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        } else {
13959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            length = buffer.getInt();
14059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
14159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        for (int i = 0; i < length; i++) {
14259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            if (elementsAreArrays) {
14359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                // Nested array.
14459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                Object element = Array.get(array, i);
14559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                if (element != null) readArray(elementSerializer, elementClass, buffer, element, dimension + 1, dimensions);
14659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            } else if (elementSerializer != null) {
14759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                // Use same converter (and class) for all elements.
14859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                Array.set(array, i, elementSerializer.readObject(buffer, elementClass));
14959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            } else {
15059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                // Each element could be a different type. Look up the class with the object.
15159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                Array.set(array, i, Serializer.readClassAndObject(buffer));
15259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            }
15359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
15459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
15559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta}
156