Performance AS-3-如何在另一个ByteArray中插入ByteArray
我想在另一个ByteArray之间的任意位置插入一个ByteArrayPerformance AS-3-如何在另一个ByteArray中插入ByteArray,performance,actionscript-3,insert,bytearray,Performance,Actionscript 3,Insert,Bytearray,我想在另一个ByteArray之间的任意位置插入一个ByteArray var main: ByteArray = ...; var bytes2insert: ByteArray = ...; var index: int = ...; // index is 0 to main.length and tells me where to write the bytes2insert in main main.writeBytes(bytes2insert, index, bytes2inser
var main: ByteArray = ...;
var bytes2insert: ByteArray = ...;
var index: int = ...;
// index is 0 to main.length and tells me where to write the bytes2insert in main
main.writeBytes(bytes2insert, index, bytes2insert.length);
如果我尝试使用随机索引写入字节,我会因为“IndexOutOfBounds”之类的东西而出错。我如何才能实现插入?我可能会使用一些for循环,但出于性能原因,我希望(主要)使用给定的方法。
编辑:我认为AS-3文档有点欠缺(但没有查看adobe.com)
//因为脚本需要从bytes2insert读取并写入main
//main.writeBytes(数组、偏移量、长度);
main.position=//(在这种情况下似乎很重要)
偏移量=/(我误解了这个)
长度=
解决方案:
public function astest()
{
var main: ByteArray = new ByteArray();
var bytes2insert: ByteArray = new ByteArray();
var index: int = 5;
for(var i:int = 0; i < 20; i++)
main.writeByte(99);
for(var j:int = 0; j < 30; j++)
bytes2insert.writeByte(100);
trace("1", main);
insertBytes(main, bytes2insert, index);
trace("2", main);
}
private function insertBytes(target:ByteArray, insert:ByteArray, index:int):void
{
if(index < target.length)
{
var tmp:ByteArray = new ByteArray();
tmp.writeBytes(target, index);
target.position = index;
target.writeBytes(insert);
target.writeBytes(tmp);
}
}
//output:
//1 cccccccccccccccccccc
//2 cccccddddddddddddddddddddddddddddddccccccccccccccc
公共函数astest()
{
var main:ByteArray=新的ByteArray();
var bytes2insert:ByteArray=newbytearray();
var指数:int=5;
对于(变量i:int=0;i<20;i++)
main.writeByte(99);
对于(var j:int=0;j<30;j++)
字节2插入写入字节(100);
跟踪(“1”,主);
insertBytes(main、bytes2insert、index);
痕迹(“2”,主要);
}
私有函数insertBytes(目标:ByteArray,插入:ByteArray,索引:int):void
{
如果(索引<目标长度)
{
var tmp:ByteArray=newbytearray();
tmp.writeBytes(目标,索引);
target.position=索引;
target.writeBytes(插入);
目标写入字节(tmp);
}
}
//输出:
//1中交
//2 CCCCDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
解决方案:
public function astest()
{
var main: ByteArray = new ByteArray();
var bytes2insert: ByteArray = new ByteArray();
var index: int = 5;
for(var i:int = 0; i < 20; i++)
main.writeByte(99);
for(var j:int = 0; j < 30; j++)
bytes2insert.writeByte(100);
trace("1", main);
insertBytes(main, bytes2insert, index);
trace("2", main);
}
private function insertBytes(target:ByteArray, insert:ByteArray, index:int):void
{
if(index < target.length)
{
var tmp:ByteArray = new ByteArray();
tmp.writeBytes(target, index);
target.position = index;
target.writeBytes(insert);
target.writeBytes(tmp);
}
}
//output:
//1 cccccccccccccccccccc
//2 cccccddddddddddddddddddddddddddddddccccccccccccccc
公共函数astest()
{
var main:ByteArray=新的ByteArray();
var bytes2insert:ByteArray=newbytearray();
var指数:int=5;
对于(变量i:int=0;i<20;i++)
main.writeByte(99);
对于(var j:int=0;j<30;j++)
字节2插入写入字节(100);
跟踪(“1”,主);
insertBytes(main、bytes2insert、index);
痕迹(“2”,主要);
}
私有函数insertBytes(目标:ByteArray,插入:ByteArray,索引:int):void
{
如果(索引<目标长度)
{
var tmp:ByteArray=newbytearray();
tmp.writeBytes(目标,索引);
target.position=索引;
target.writeBytes(插入);
目标写入字节(tmp);
}
}
//输出:
//1中交
//2 CCCCDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD