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;
022
023import org.conqat.lib.commons.serialization.SerializedEntityPool;
024import org.conqat.lib.commons.serialization.SerializedEntitySerializer;
025
026/**
027 * A serialized object of type string.
028 */
029public class SerializedStringObject extends SerializedObjectBase {
030
031        /** The string value of this object. */
032        private String value;
033
034        /** Constructor. */
035        public SerializedStringObject(String value, SerializedEntityPool pool) {
036                // strings are serialized with an explicit tag code and hence have no
037                // class (at least not in the serialization stream); we use null here.
038                super(pool, SerializedEntityPool.NULL_HANDLE);
039                this.value = value;
040        }
041
042        /**
043         * Constructor reading the string from an input stream.
044         * 
045         * @param longString
046         *            if this is true, the string is a long string.
047         */
048        public SerializedStringObject(DataInputStream din, SerializedEntityPool pool, boolean longString)
049                        throws IOException {
050                // strings are serialized with an explicit tag code and hence have no
051                // class (at least not in the serialization stream); we use null here.
052                super(pool, SerializedEntityPool.NULL_HANDLE);
053                if (longString) {
054                        this.value = LongStringUtils.readLongString(din);
055                } else {
056                        this.value = din.readUTF();
057                }
058        }
059
060        /** Returns the string value. */
061        public String getValue() {
062                return value;
063        }
064
065        /** Sets the string value */
066        public void setValue(String value) {
067                this.value = value;
068        }
069
070        /** {@inheritDoc} */
071        @Override
072        public void serialize(DataOutputStream dos, SerializedEntitySerializer serializer) throws IOException {
073                // we never want stings to use references here, as references are
074                // handled in the serializer itself
075                serializeContent(dos, serializer);
076        }
077
078        /** {@inheritDoc} */
079        @Override
080        protected void serializeContent(DataOutputStream dos, SerializedEntitySerializer serializer) throws IOException {
081                serializer.serializeStringObject(value);
082        }
083
084        /** {@inheritDoc} */
085        @Override
086        protected byte getObjectTagConstant() {
087                throw new AssertionError("Should not be called!");
088        }
089
090        /** {@inheritDoc} */
091        @Override
092        protected void serializeObjectContent(DataOutputStream dos, SerializedEntitySerializer serializer) {
093                throw new AssertionError("Should not be called!");
094        }
095}