Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
198 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...