KickJava   Java API By Example, From Geeks To Geeks.

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


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.core.client.GWT;
19 import com.google.gwt.core.client.JavaScriptObject;
20 import com.google.gwt.user.client.rpc.SerializationException;
21
22 import java.util.ArrayList JavaDoc;
23
24 /**
25  * For internal use only. Used for server call serialization.
26  */

27 public final class ClientSerializationStreamWriter extends
28     AbstractSerializationStreamWriter {
29
30   private static void append(StringBuffer JavaDoc sb, String JavaDoc token) {
31     assert (token != null);
32     sb.append(token);
33     sb.append('\uffff');
34   }
35
36   private static native JavaScriptObject createJavaScriptObject() /*-{
37     return {};
38   }-*/
;
39
40   /*
41    * Accessed from JSNI code, so ignore unused warning.
42    */

43   private JavaScriptObject objectMap;
44
45   /*
46    * Accesses need to be prefixed with ':' to prevent conflict with built-in
47    * JavaScript properties.
48    *
49    * Accessed from JSNI code, so ignore unused warning.
50    */

51   private JavaScriptObject stringMap;
52
53   private StringBuffer JavaDoc encodeBuffer;
54
55   private int objectCount;
56
57   private Serializer serializer;
58
59   private ArrayList JavaDoc stringTable = new ArrayList JavaDoc();
60
61   public ClientSerializationStreamWriter(Serializer serializer) {
62     this.serializer = serializer;
63   }
64
65   /**
66    * Call this method before attempting to append any tokens. This method
67    * implementation <b>must</b> be called by any overridden version.
68    */

69   public void prepareToWrite() {
70     objectCount = 0;
71     objectMap = createJavaScriptObject();
72     stringMap = createJavaScriptObject();
73     stringTable.clear();
74     encodeBuffer = new StringBuffer JavaDoc();
75   }
76
77   public String JavaDoc toString() {
78     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
79     writeHeader(buffer);
80     writeStringTable(buffer);
81     writePayload(buffer);
82     return buffer.toString();
83   }
84
85   protected int addString(String JavaDoc string) {
86     if (string == null) {
87       return 0;
88     }
89
90     int index = getIntForString(string);
91     if (index > 0) {
92       return index;
93     }
94     stringTable.add(string);
95     // index is 1-based (that's why we're taking the size AFTER add)
96
index = stringTable.size();
97     setIntForString(string, index);
98     return index;
99   }
100
101   /**
102    * Appends a token to the end of the buffer.
103    */

104   protected void append(String JavaDoc token) {
105     append(encodeBuffer, token);
106   }
107
108   protected int getIndexForObject(Object JavaDoc instance) {
109     return getIntForInt(System.identityHashCode(instance));
110   }
111
112   protected String JavaDoc getObjectTypeSignature(Object JavaDoc o) {
113     String JavaDoc typeName = GWT.getTypeName(o);
114     String JavaDoc serializationSignature = serializer.getSerializationSignature(typeName);
115     if (serializationSignature != null) {
116       typeName += "/" + serializationSignature;
117     }
118     return typeName;
119   }
120
121   protected void saveIndexForObject(Object JavaDoc instance) {
122     setIntForInt(System.identityHashCode(instance), objectCount++);
123   }
124
125   protected void serialize(Object JavaDoc instance, String JavaDoc typeSignature)
126       throws SerializationException {
127     serializer.serialize(this, instance, typeSignature);
128   }
129
130   private native int getIntForInt(int key) /*-{
131     var result = this.@com.google.gwt.user.client.rpc.impl.ClientSerializationStreamWriter::objectMap[key];
132     return (result == null) ? -1 : result;
133   }-*/
;
134
135   // prefix needed to prevent conflict with built-in JavaScript properties.
136
private native int getIntForString(String JavaDoc key) /*-{
137     var result = this.@com.google.gwt.user.client.rpc.impl.ClientSerializationStreamWriter::stringMap[':' + key];
138     return (result == null) ? 0 : result;
139   }-*/
;
140
141   private native void setIntForInt(int key, int value) /*-{
142     this.@com.google.gwt.user.client.rpc.impl.ClientSerializationStreamWriter::objectMap[key] = value;
143   }-*/
;
144
145   // prefix needed to prevent conflict with built-in JavaScript properties.
146
private native void setIntForString(String JavaDoc key, int value) /*-{
147     this.@com.google.gwt.user.client.rpc.impl.ClientSerializationStreamWriter::stringMap[':' + key] = value;
148   }-*/
;
149
150   private void writeHeader(StringBuffer JavaDoc buffer) {
151     append(buffer, String.valueOf(SERIALIZATION_STREAM_VERSION));
152     append(buffer, String.valueOf(getFlags()));
153   }
154
155   private void writePayload(StringBuffer JavaDoc buffer) {
156     buffer.append(encodeBuffer.toString());
157   }
158
159   private StringBuffer JavaDoc writeStringTable(StringBuffer JavaDoc buffer) {
160     int stringTableSize = stringTable.size();
161     append(buffer, String.valueOf(stringTableSize));
162     for (int i = 0; i < stringTableSize; ++i) {
163       append(buffer, (String JavaDoc) stringTable.get(i));
164     }
165     return buffer;
166   }
167
168 }
169
Popular Tags