KickJava   Java API By Example, From Geeks To Geeks.

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


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.SerializationStreamReader;
20
21 import java.util.ArrayList JavaDoc;
22
23 /**
24  * Base class for the client and server serialization streams. This class
25  * handles the basic serialization and desirialization formatting for primitive
26  * types since these are common between the client and the server.
27  */

28 public abstract class AbstractSerializationStreamReader extends
29     AbstractSerializationStream implements SerializationStreamReader {
30
31   private ArrayList JavaDoc seenArray = new ArrayList JavaDoc();
32
33   public void prepareToRead(String JavaDoc encoded) throws SerializationException {
34     seenArray.clear();
35
36     // Read the stream version number
37
//
38
setVersion(readInt());
39
40     // Read the flags from the stream
41
//
42
setFlags(readInt());
43   }
44
45   public final Object JavaDoc readObject() throws SerializationException {
46     int token = readInt();
47
48     if (token < 0) {
49       // Negative means a previous object
50
// Transform negative 1-based to 0-based.
51
return seenArray.get(-(token + 1));
52     }
53
54     // Positive means a new object
55
String JavaDoc typeSignature = getString(token);
56     if (typeSignature == null) {
57       // a null string means a null instance
58
return null;
59     }
60
61     return deserialize(typeSignature);
62   }
63
64   /**
65    * Deserialize an object with the given type signature.
66    *
67    * @param typeSignature the type signature to deserialize
68    * @return the deserialized object
69    * @throws SerializationException
70    */

71   protected abstract Object JavaDoc deserialize(String JavaDoc typeSignature)
72       throws SerializationException;
73
74   /**
75    * Gets a string out of the string table.
76    *
77    * @param index the index of the string to get
78    * @return the string
79    */

80   protected abstract String JavaDoc getString(int index);
81
82   protected final void rememberDecodedObject(Object JavaDoc o) {
83     seenArray.add(o);
84   }
85
86 }
87
Popular Tags