1

I am using parcel to cache Items in binary file. Everything was fine until I need to store into single ArrayList various type of objects:

        fout = new FileOutputStream(file);

        Parcel parcel = Parcel.obtain();
        ArrayList<Object> list = new ArrayList<Object>(items);
        Log.d(TAG, "write items to cache: " + items.size());
        parcel.writeList(list);
        byte[] data = parcel.marshall();
        fout.write(data);

But reading don't work properly:

fin = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
fin.read(data);
Log.d(TAG, "file size: " + file.length());

Parcel parcel = Parcel.obtain();
parcel.unmarshall(data, 0, data.length);

ArrayList<Object> rawList = parcel.readArrayList(Object.class.getClassLoader());

I think problem is in Object classLoader, who can't create object for extended class.

Have any idea how I can easily fix this? Or maybe could somebody to give me advise for my task which is to cache ArrayList or Set into file with objects of type <? extends Object?>. Logcat:

08-20 12:16:52.200: D/ItemList(6637): writing data
08-20 12:16:52.200: D/ItemList(6637): write items to cache: 2
08-20 12:17:45.940: D/ItemList(6925): reading data
08-20 12:17:46.650: D/ItemList(6925): reading data
08-20 12:17:47.050: D/ItemList(6925): reading data
08-20 12:17:47.060: D/ItemList(6925): cache file found
08-20 12:31:18.050: D/ItemList(7349): file size: 760
08-20 12:17:47.060: D/ItemList(6925): founded items in cache: 0

More logcat:

08-20 12:49:02.930: D/ItemList(9241): file size: 1136
08-20 12:49:02.930: D/ItemList(9241): dataAvail(): 0
08-20 12:49:02.930: D/ItemList(9241): dataSize(): 1136
08-20 12:49:02.930: D/ItemList(9241): dataCapacity(): 1136
08-20 12:49:02.930: D/ItemList(9241): founded items in cache: 0

Websocket message interupts dynamic loading of ExtJS class

in my ExtJS 4.1 application I use a websocket connection to remotely insantiate and control ExtJS classes from the server. The client is registered to websocket.onmessage and is waiting for incoming commands.

I defined a simple protocol for that. The server sends a "CREATE classname id". On client side I use Ext.create to instantiate the class. The server then can send commands via the websocket to the object. E.g. "DOSTUFF id". I'm using the dynamic loading mechnism of ExtJS.

In Chrome everything works fine.

The problem with Firefox is, that the second command message (DOSTUFF) is executed BEFORE the object has been created. This leads to an error because the object cannot be found. It seems that the second websocket commandmessage is executed before ExtJS has loaded the file via HTTP-GET.

In my world JavaScript is executed sequentially (I don't use webworkers). I think the call of Ext.create(..) should be executed synchronously with the HTTP-GET in background, shouldn't it?

Here is a "pseudo" trace output of my client application:

ExecuteCommand (Enter): CREATE ("MyClass", "1")
HTTP-GET "MyClass.js"
ExecuteCommand (Enter): DOSTUFF ("1")
ExecuteCommand (Error): DOSTUFF ("1"): Object not found
ExecuteCommand (Enter): CREATE ("MyClass", "1"): OK! Object created!
4

1 に答える 1

2

わかりました、見つけました。ファイルからデータを読み取ることはありません。

fin = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];

入力ストリームを作成し、バイト バッファを割り当てるだけです。

編集 パーセルをアンマーシャリングする方法に関するコードを追加します

Parcel parcel = Parcel.obtain();
parcel.unmarshall(data, 0, data.length);
parcel.setDataPosition(0); // Set the position in the parcel back to the beginning
                           //  so that we can read the stuff out of it
ArrayList<Object> rawList = parcel.readArrayList(Object.class.getClassLoader());
于 2012-08-20T09:25:56.597 に答える