如何使对象的网络连载到一个文件,而不是一个共享对象? [英] How to make an object network serialize to a file, not a SharedObject?
问题描述
目前,我做我的模型对象序列化到 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.' );
}
同样,我加载使用SharedObject实例保存的模型。伟大工程。
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.
其实,我无法想象的FileStream / _model.writeExternal()如何能够序列化,因为它需要以某种方式知道,一个新的序列化操作即将开始。如果它不知道,也无法确定,哪一个对象实例的离开序列化
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.
请解释,我所缺少的。
我很乐意从共享对象读取原始ByteArray并将其写入文件。不幸的是,我没有找到一个方法来从一个共享对象某个属性的ByteArray检索,在我的情况 mySo.data.model
。
我的问题是松散与此相关的一项:为什么删除(DictionaryInstance [关键]);失败了呢?
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?
推荐答案
我曾经不得不在一个外化框架,我建立了执行单元测试,这是我做的:
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();
另外,我不认为你应该担心字节序,我觉得默认是Big Endian反正。
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)
,而不是
myObject.writeExternal(_prefsStream);
运行时应该叫的writeExternal
在模型自动的。
这篇关于如何使对象的网络连载到一个文件,而不是一个共享对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!