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.core.client.JavaScriptObject;
019: import com.google.gwt.user.client.rpc.SerializationException;
020:
021: /**
022: * For internal use only. Used for server call serialization.
023: */
024: public final class ClientSerializationStreamReader extends
025: AbstractSerializationStreamReader {
026:
027: private static native JavaScriptObject eval(String encoded) /*-{
028: return eval(encoded);
029: }-*/;
030:
031: private static native int getLength(JavaScriptObject array) /*-{
032: return array.length;
033: }-*/;
034:
035: int index;
036:
037: JavaScriptObject results;
038:
039: JavaScriptObject stringTable;
040:
041: private Serializer serializer;
042:
043: public ClientSerializationStreamReader(Serializer serializer) {
044: this .serializer = serializer;
045: }
046:
047: @Override
048: public void prepareToRead(String encoded)
049: throws SerializationException {
050: results = eval(encoded);
051: index = getLength(results);
052: super .prepareToRead(encoded);
053: stringTable = readJavaScriptObject();
054: }
055:
056: public native boolean readBoolean() /*-{
057: return !!this.@com.google.gwt.user.client.rpc.impl.ClientSerializationStreamReader::results[--this.@com.google.gwt.user.client.rpc.impl.ClientSerializationStreamReader::index];
058: }-*/;
059:
060: public native byte readByte() /*-{
061: return this.@com.google.gwt.user.client.rpc.impl.ClientSerializationStreamReader::results[--this.@com.google.gwt.user.client.rpc.impl.ClientSerializationStreamReader::index];
062: }-*/;
063:
064: public native char readChar() /*-{
065: return this.@com.google.gwt.user.client.rpc.impl.ClientSerializationStreamReader::results[--this.@com.google.gwt.user.client.rpc.impl.ClientSerializationStreamReader::index];
066: }-*/;
067:
068: public native double readDouble() /*-{
069: return this.@com.google.gwt.user.client.rpc.impl.ClientSerializationStreamReader::results[--this.@com.google.gwt.user.client.rpc.impl.ClientSerializationStreamReader::index];
070: }-*/;
071:
072: public native float readFloat() /*-{
073: return this.@com.google.gwt.user.client.rpc.impl.ClientSerializationStreamReader::results[--this.@com.google.gwt.user.client.rpc.impl.ClientSerializationStreamReader::index];
074: }-*/;
075:
076: public native int readInt() /*-{
077: return this.@com.google.gwt.user.client.rpc.impl.ClientSerializationStreamReader::results[--this.@com.google.gwt.user.client.rpc.impl.ClientSerializationStreamReader::index];
078: }-*/;
079:
080: public native long readLong() /*-{
081: return this.@com.google.gwt.user.client.rpc.impl.ClientSerializationStreamReader::results[--this.@com.google.gwt.user.client.rpc.impl.ClientSerializationStreamReader::index];
082: }-*/;
083:
084: public native short readShort() /*-{
085: return this.@com.google.gwt.user.client.rpc.impl.ClientSerializationStreamReader::results[--this.@com.google.gwt.user.client.rpc.impl.ClientSerializationStreamReader::index];
086: }-*/;
087:
088: public String readString() {
089: return getString(readInt());
090: }
091:
092: @Override
093: protected Object deserialize(String typeSignature)
094: throws SerializationException {
095: Object instance = serializer.instantiate(this , typeSignature);
096: rememberDecodedObject(instance);
097: serializer.deserialize(this , instance, typeSignature);
098: return instance;
099: }
100:
101: @Override
102: protected native String getString(int index) /*-{
103: // index is 1-based
104: if (!index) {
105: return null;
106: }
107: return this.@com.google.gwt.user.client.rpc.impl.ClientSerializationStreamReader::stringTable[index - 1];
108: }-*/;
109:
110: private native JavaScriptObject readJavaScriptObject() /*-{
111: return this.@com.google.gwt.user.client.rpc.impl.ClientSerializationStreamReader::results[--this.@com.google.gwt.user.client.rpc.impl.ClientSerializationStreamReader::index];
112: }-*/;
113:
114: }
|