1 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 ; 23 24 27 public final class ClientSerializationStreamWriter extends 28 AbstractSerializationStreamWriter { 29 30 private static void append(StringBuffer sb, String token) { 31 assert (token != null); 32 sb.append(token); 33 sb.append('\uffff'); 34 } 35 36 private static native JavaScriptObject createJavaScriptObject() ; 39 40 43 private JavaScriptObject objectMap; 44 45 51 private JavaScriptObject stringMap; 52 53 private StringBuffer encodeBuffer; 54 55 private int objectCount; 56 57 private Serializer serializer; 58 59 private ArrayList stringTable = new ArrayList (); 60 61 public ClientSerializationStreamWriter(Serializer serializer) { 62 this.serializer = serializer; 63 } 64 65 69 public void prepareToWrite() { 70 objectCount = 0; 71 objectMap = createJavaScriptObject(); 72 stringMap = createJavaScriptObject(); 73 stringTable.clear(); 74 encodeBuffer = new StringBuffer (); 75 } 76 77 public String toString() { 78 StringBuffer buffer = new StringBuffer (); 79 writeHeader(buffer); 80 writeStringTable(buffer); 81 writePayload(buffer); 82 return buffer.toString(); 83 } 84 85 protected int addString(String 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 = stringTable.size(); 97 setIntForString(string, index); 98 return index; 99 } 100 101 104 protected void append(String token) { 105 append(encodeBuffer, token); 106 } 107 108 protected int getIndexForObject(Object instance) { 109 return getIntForInt(System.identityHashCode(instance)); 110 } 111 112 protected String getObjectTypeSignature(Object o) { 113 String typeName = GWT.getTypeName(o); 114 String serializationSignature = serializer.getSerializationSignature(typeName); 115 if (serializationSignature != null) { 116 typeName += "/" + serializationSignature; 117 } 118 return typeName; 119 } 120 121 protected void saveIndexForObject(Object instance) { 122 setIntForInt(System.identityHashCode(instance), objectCount++); 123 } 124 125 protected void serialize(Object instance, String typeSignature) 126 throws SerializationException { 127 serializer.serialize(this, instance, typeSignature); 128 } 129 130 private native int getIntForInt(int key) ; 134 135 private native int getIntForString(String key) ; 140 141 private native void setIntForInt(int key, int value) ; 144 145 private native void setIntForString(String key, int value) ; 149 150 private void writeHeader(StringBuffer buffer) { 151 append(buffer, String.valueOf(SERIALIZATION_STREAM_VERSION)); 152 append(buffer, String.valueOf(getFlags())); 153 } 154 155 private void writePayload(StringBuffer buffer) { 156 buffer.append(encodeBuffer.toString()); 157 } 158 159 private StringBuffer writeStringTable(StringBuffer buffer) { 160 int stringTableSize = stringTable.size(); 161 append(buffer, String.valueOf(stringTableSize)); 162 for (int i = 0; i < stringTableSize; ++i) { 163 append(buffer, (String ) stringTable.get(i)); 164 } 165 return buffer; 166 } 167 168 } 169 | Popular Tags |