001/*-------------------------------------------------------------------------+
002|                                                                          |
003| Copyright 2005-2011 the ConQAT Project                                   |
004|                                                                          |
005| Licensed under the Apache License, Version 2.0 (the "License");          |
006| you may not use this file except in compliance with the License.         |
007| You may obtain a copy of the License at                                  |
008|                                                                          |
009|    http://www.apache.org/licenses/LICENSE-2.0                            |
010|                                                                          |
011| Unless required by applicable law or agreed to in writing, software      |
012| distributed under the License is distributed on an "AS IS" BASIS,        |
013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
014| See the License for the specific language governing permissions and      |
015| limitations under the License.                                           |
016+-------------------------------------------------------------------------*/
017package org.conqat.lib.commons.serialization.objects;
018
019import java.io.DataInputStream;
020import java.io.DataOutputStream;
021import java.io.IOException;
022import java.io.ObjectStreamConstants;
023import java.util.ArrayList;
024import java.util.List;
025
026import org.conqat.lib.commons.serialization.SerializedEntityParser;
027import org.conqat.lib.commons.serialization.SerializedEntityPool;
028import org.conqat.lib.commons.serialization.SerializedEntitySerializer;
029import org.conqat.lib.commons.serialization.classes.SerializedArrayField;
030import org.conqat.lib.commons.serialization.classes.SerializedClass;
031import org.conqat.lib.commons.serialization.classes.SerializedFieldBase;
032import org.conqat.lib.commons.serialization.classes.SerializedObjectField;
033import org.conqat.lib.commons.serialization.classes.SerializedPrimitiveFieldBase;
034
035/**
036 * An array in a serialized stream.
037 */
038public class SerializedArrayObject extends SerializedObjectBase {
039
040        /** The values in the array. */
041        private final List<Object> values = new ArrayList<>();
042
043        /** The type of the elements use for reading/writing them. */
044        private final SerializedFieldBase elementType;
045
046        /** Constructor. */
047        public SerializedArrayObject(DataInputStream din, SerializedEntityPool pool, SerializedEntityParser parser,
048                        int classHandle) throws IOException {
049                super(pool, classHandle);
050
051                String className = pool.getEntity(classHandle, SerializedClass.class).getName();
052                if (className.length() == 2 && className.charAt(0) == SerializedArrayField.TYPE_CODE) {
053                        elementType = SerializedPrimitiveFieldBase.fromTypeCode(className.charAt(1), null);
054                } else {
055                        elementType = new SerializedObjectField();
056                }
057
058                int size = din.readInt();
059                for (int i = 0; i < size; ++i) {
060                        values.add(elementType.readValue(din, parser));
061                }
062        }
063
064        /** {@inheritDoc} */
065        @Override
066        protected byte getObjectTagConstant() {
067                return ObjectStreamConstants.TC_ARRAY;
068        }
069
070        /** {@inheritDoc} */
071        @Override
072        protected void serializeObjectContent(DataOutputStream dos, SerializedEntitySerializer serializer)
073                        throws IOException {
074                dos.writeInt(values.size());
075                for (Object value : values) {
076                        elementType.writeValue(value, pool, dos, serializer);
077                }
078        }
079}