KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > user > client > rpc > impl > AbstractSerializationStreamWriter


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.user.client.rpc.impl;
17
18 import com.google.gwt.user.client.rpc.SerializationException;
19 import com.google.gwt.user.client.rpc.SerializationStreamWriter;
20
21 /**
22  * Base class for the client and server serialization streams. This class
23  * handles the basic serialization and desirialization formatting for primitive
24  * types since these are common between the client and the server.
25  */

26 public abstract class AbstractSerializationStreamWriter extends
27     AbstractSerializationStream implements SerializationStreamWriter {
28
29   public abstract String JavaDoc toString();
30
31   public void writeBoolean(boolean fieldValue) {
32     append(fieldValue ? "1" : "0");
33   }
34
35   public void writeByte(byte fieldValue) {
36     append(String.valueOf(fieldValue));
37   }
38
39   public void writeChar(char ch) {
40     // just use an int, it's more foolproof
41
append(String.valueOf((int) ch));
42   }
43
44   public void writeDouble(double fieldValue) {
45     append(String.valueOf(fieldValue));
46   }
47
48   public void writeFloat(float fieldValue) {
49     append(String.valueOf(fieldValue));
50   }
51
52   public void writeInt(int fieldValue) {
53     append(String.valueOf(fieldValue));
54   }
55
56   public void writeLong(long fieldValue) {
57     append(String.valueOf(fieldValue));
58   }
59
60   public void writeObject(Object JavaDoc instance) throws SerializationException {
61     if (instance == null) {
62       // write a null string
63
writeString(null);
64       return;
65     }
66
67     int objIndex = getIndexForObject(instance);
68     if (objIndex >= 0) {
69       // We've already encoded this object, make a backref
70
// Transform 0-based to negative 1-based
71
writeInt(-(objIndex + 1));
72       return;
73     }
74
75     saveIndexForObject(instance);
76
77     // Serialize the type signature
78
String JavaDoc typeSignature = getObjectTypeSignature(instance);
79     writeString(typeSignature);
80     // Now serialize the rest of the object
81
serialize(instance, typeSignature);
82   }
83
84   public void writeShort(short value) {
85     append(String.valueOf(value));
86   }
87
88   public void writeString(String JavaDoc value) {
89     writeInt(addString(value));
90   }
91
92   /**
93    * Add a string to the string table and return its index.
94    *
95    * @param string the string to add
96    * @return the index to the string
97    */

98   protected abstract int addString(String JavaDoc string);
99
100   /**
101    * Append a token to the underlying output buffer.
102    *
103    * @param token the token to append
104    */

105   protected abstract void append(String JavaDoc token);
106
107   /**
108    * Get the index for an object that may have previously been saved via
109    * {@link #saveIndexForObject(Object)}.
110    *
111    * @param instance the object to save
112    * @return the index associated with this object, or -1 if this object hasn't
113    * been seen before
114    */

115   protected abstract int getIndexForObject(Object JavaDoc instance);
116
117   /**
118    * Compute and return the type signature for an object.
119    *
120    * @param instance the instance to inspect
121    * @return the type signature of the instance
122    */

123   protected abstract String JavaDoc getObjectTypeSignature(Object JavaDoc instance);
124
125   /**
126    * Remember this object as having been seen before.
127    *
128    * @param instance the object to remember
129    */

130   protected abstract void saveIndexForObject(Object JavaDoc instance);
131
132   /**
133    * Serialize an object into the stream.
134    *
135    * @param instance the object to serialize
136    * @param typeSignature the type signature of the object
137    * @throws SerializationException
138    */

139   protected abstract void serialize(Object JavaDoc instance, String JavaDoc typeSignature)
140       throws SerializationException;
141
142 }
143
Popular Tags