How to make an object network serialize to a file, not a SharedObject?

若如初见. 提交于 2020-01-17 08:34:31

问题


Currently, I do serialize my model object to the SharedObject instance:

 try {

     var mySo:SharedObject = SharedObject.getLocal("sig");

     mySo.clear();
     mySo.data.model = _model;
     mySo.flush();

  } catch ( e:Error ) {

    Alert.show( 'Leider konnte kein Modell geladen werden.' );

  }

Likewise, I load the saved model using the SharedObject instance. Works great.

Ultimately, I'd like to serialize it to a file - which fails. Here is how:

 var fp: File = File.applicationStorageDirectory;

 fp = fp.resolvePath( PREFS_FILENAME );

 var    _prefsStream:FileStream;
 _prefsStream = new FileStream();
 _prefsStream.open( fp, FileMode.WRITE );
 _prefsStream.endian = Endian.BIG_ENDIAN;

_model.writeExternal( _prefsStream );
_prefsStream.close();

The complementing read operation suddenly breaks and reports missing bytes.

In fact, I can't image how FileStream / _model.writeExternal() is able to serialize, since it needs to somehow know, that a new serialization operation is about to start. If it doesn't know, it won't be able to determine, which object instances are left to serialize.

Thus, I image that my concept is completely wrong or I missed how to initialize the serialization operation.

Please explains, what I'm missing.

I'd be happy to read the raw ByteArray from the shared object and write it to a file. Unfortunately, I didn't find a method to retrieve from a SharedObject a ByteArray of a certain property, in my case mySo.data.model. My question is loosely related to this one: Why does delete( DictionaryInstance[ key ] ); fail?


回答1:


I once had to perform unit tests on an externalization framework I built and this is how I did it:

byteArray.writeObject(myObject);
byteArray.position = 0;
readValue = byteArray.readObject();

Also, I don't think you should have to worry about byte order, I think the default is big endian anyways.

So, for your case, I think you need something like:

fileStream.writeObject(myObject)

as opposed to:

myObject.writeExternal(_prefsStream);

The runtime should call writeExternal on your model automagically.



来源:https://stackoverflow.com/questions/7718346/how-to-make-an-object-network-serialize-to-a-file-not-a-sharedobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!