我使用以下代码从字节数组中读取错误消息,第一次运行正常,但如果我第二次尝试访问它,则会抛出错误:
errorData = process.standardError.readUTFBytes(process.standardError.bytesAvailable);
StandardError 的类型为 InboundPipe?
错误是:
Error: Error #3212: Cannot perform operation on a NativeProcess that is not running.
即使进程正在运行(process.running 为 true)。这似乎是第二次调用 readUTFBytes 的原因。
更新:
下面是依次调用同一个调用的代码。错误发生在下一行,并且 process.running 没有从 true 更改。发生在第二次通话时。
errorData = process.standardError.readUTFBytes(process.standardError.bytesAvailable);
errorData = process.standardError.readUTFBytes(process.standardError.bytesAvailable);
我还发现standardError是一个InboundPipe实例并实现了IDataInput。
更新2:
感谢您的所有帮助。我在查看 bytesAvailable 属性时找到了此文档。
[Read Only] Returns the number of bytes of data available for reading in the input buffer. User code must call
bytesAvailable
to ensure that sufficient data is available before trying to read it with one of the read methods.
当我调用 readUTFBytes() 时,它将可用字节重置为 0。因此,当我第二次读取它并且没有可用字节时,它会导致错误。在我看来,该错误是不正确的,或者可能是不正确的,或者 native process.running 标志不正确。
我查看了它是否具有 position
属性,但它没有,至少在本例中没有。
最佳答案
您能否尝试在读取过程之前将position
设置为零,尤其是在重复访问之前:
Moves, or returns the current position, in bytes, of the file pointer into the ByteArray object. This is the point at which the next call to a read method starts reading or a write method starts writing.
//ByteArray example
var source: String = "Some data";
var data: ByteArray = new ByteArray();
data.writeUTFBytes(source);
data.position = 0;
trace(data.readUTFBytes(data.bytesAvailable));
data.position = 0;
trace(data.readUTFBytes(data.bytesAvailable));
关于actionscript-3 - 第二次读取字节数组出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22913518/