001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.user.client.rpc.impl;
017:
018: import com.google.gwt.user.client.rpc.SerializationException;
019: import com.google.gwt.user.client.rpc.SerializationStreamWriter;
020:
021: /**
022: * Base class for the client and server serialization streams. This class
023: * handles the basic serialization and deserialization formatting for primitive
024: * types since these are common between the client and the server.
025: */
026: public abstract class AbstractSerializationStreamWriter extends
027: AbstractSerializationStream implements
028: SerializationStreamWriter {
029:
030: @Override
031: public abstract String toString();
032:
033: public void writeBoolean(boolean fieldValue) {
034: append(fieldValue ? "1" : "0");
035: }
036:
037: public void writeByte(byte fieldValue) {
038: append(String.valueOf(fieldValue));
039: }
040:
041: public void writeChar(char ch) {
042: // just use an int, it's more foolproof
043: append(String.valueOf((int) ch));
044: }
045:
046: public void writeDouble(double fieldValue) {
047: append(String.valueOf(fieldValue));
048: }
049:
050: public void writeFloat(float fieldValue) {
051: append(String.valueOf(fieldValue));
052: }
053:
054: public void writeInt(int fieldValue) {
055: append(String.valueOf(fieldValue));
056: }
057:
058: public void writeLong(long fieldValue) {
059: append(String.valueOf(fieldValue));
060: }
061:
062: public void writeObject(Object instance)
063: throws SerializationException {
064: if (instance == null) {
065: // write a null string
066: writeString(null);
067: return;
068: }
069:
070: int objIndex = getIndexForObject(instance);
071: if (objIndex >= 0) {
072: // We've already encoded this object, make a backref
073: // Transform 0-based to negative 1-based
074: writeInt(-(objIndex + 1));
075: return;
076: }
077:
078: saveIndexForObject(instance);
079:
080: // Serialize the type signature
081: String typeSignature = getObjectTypeSignature(instance);
082: writeString(typeSignature);
083: // Now serialize the rest of the object
084: serialize(instance, typeSignature);
085: }
086:
087: public void writeShort(short value) {
088: append(String.valueOf(value));
089: }
090:
091: public void writeString(String value) {
092: writeInt(addString(value));
093: }
094:
095: /**
096: * Add a string to the string table and return its index.
097: *
098: * @param string the string to add
099: * @return the index to the string
100: */
101: protected abstract int addString(String string);
102:
103: /**
104: * Append a token to the underlying output buffer.
105: *
106: * @param token the token to append
107: */
108: protected abstract void append(String token);
109:
110: /**
111: * Get the index for an object that may have previously been saved via
112: * {@link #saveIndexForObject(Object)}.
113: *
114: * @param instance the object to save
115: * @return the index associated with this object, or -1 if this object hasn't
116: * been seen before
117: */
118: protected abstract int getIndexForObject(Object instance);
119:
120: /**
121: * Compute and return the type signature for an object.
122: *
123: * @param instance the instance to inspect
124: * @return the type signature of the instance
125: */
126: protected abstract String getObjectTypeSignature(Object instance);
127:
128: /**
129: * Remember this object as having been seen before.
130: *
131: * @param instance the object to remember
132: */
133: protected abstract void saveIndexForObject(Object instance);
134:
135: /**
136: * Serialize an object into the stream.
137: *
138: * @param instance the object to serialize
139: * @param typeSignature the type signature of the object
140: * @throws SerializationException
141: */
142: protected abstract void serialize(Object instance,
143: String typeSignature) throws SerializationException;
144:
145: }
|