Actionscript 3 flash as3-我需要在byteArray数据中进行二进制搜索
我在获取ByteArray的部分Actionscript 3 flash as3-我需要在byteArray数据中进行二进制搜索,actionscript-3,flash,search,binary,bytearray,Actionscript 3,Flash,Search,Binary,Bytearray,我在获取ByteArray的部分数据时遇到问题。 fileData中有一个二进制文本: var fileData:ByteArray = new ByteArray(); //..........here's code that fills this var with binary data .....readBytes(fileData,0,1000); // 数据如下: йYЯyeSВ–нkq(г<<<start>>>:xЪмЅdf”cйxЪsdfмЅ”
数据时遇到问题。
fileData
中有一个二进制文本:
var fileData:ByteArray = new ByteArray();
//..........here's code that fills this var with binary data
.....readBytes(fileData,0,1000);
//
数据如下:
йYЯyeSВ–нkq(г<<<start>>>:xЪмЅdf”cйxЪsdfмЅ”cйdxЪмЅ”cй<<<end>>>В–нkВ
l YЯyeSЯb–цkq(Γ:xЪmЅdf“cоxЪsdfЅ”cоdxЪmЅcоb–kз
所以,我需要找到
和
的位置,并复制它们之间的数据
但是搜索fileData.toString().indexOf('>')
有时会得到这个字符串的错误位置,有时甚至根本找不到它
如何才能正确确定所需部分数据的位置?您不应使用fileData.toString().indexOf()
,因为您使用的是二进制数据。您必须搜索字节序列
以下函数用于检索指定图案的位置:
public function indexOf(bytes:ByteArray, search:String, startOffset:uint = 0):void
{
if (bytes == null || bytes.length == 0) {
throw new ArgumentError("bytes parameter should not be null or empty");
}
if (search == null || search.length == 0) {
throw new ArgumentError("search parameter should not be null or empty");
}
// Fast return is the search pattern length is shorter than the bytes one
if (bytes.length < startOffset + search.length) {
return -1;
}
// Create the pattern
var pattern:ByteArray = new ByteArray();
pattern.writeUTFBytes(search);
// Initialize loop variables
var end:Boolean;
var found:Boolean;
var i:uint = startOffset;
var j:uint = 0;
var p:uint = pattern.length;
var n:uint = bytes.length - p;
// Repeat util end
do {
// Compare the current byte with the first one of the pattern
if (bytes[i] == pattern[0]) {
found = true;
j = p;
// Loop through every byte of the pattern
while (--j) {
if (bytes[i + j] != pattern[j]) {
found = false;
break;
}
}
// Return the pattern position
if (found) {
return i;
}
}
// Check if end is reach
end = (++i > n);
} while (!end);
// Pattern not found
return -1;
}
public function indexOf(字节:ByteArray,搜索:String,startOffset:uint=0):void
{
if(bytes==null | | bytes.length==0){
抛出新ArgumentError(“bytes参数不应为null或空”);
}
if(search==null | | search.length==0){
抛出新ArgumentError(“搜索参数不应为null或空”);
}
//快速返回是指搜索模式长度比字节长度短
if(bytes.lengthn);
}而(!end);
//找不到模式
返回-1;
}
然后,您可以通过以下方式使用该功能:
var extractedBytes = new ByteArray();
var startPos:int = indexOf(fileData, "<<<start>>>");
var endPos:int;
if (startPos == -1) {
trace("<<<start>>> not found");
} else {
endPos = indexOf(fileData, "<<<end>>>", startPos + 11); // "<<<start>>>".length = 11
}
if (startPos == -1) {
trace("<<<end>>> not found");
} else {
// Extract the bytes between <<<start>>> and <<<end>>>
fileData.readBytes(extractedBytes, startPos + 11, endPos);
}
var extractedBytes=new ByteArray();
var startPos:int=indexOf(fileData,“”);
var-endPos:int;
如果(startPos==-1){
跟踪(“未找到”);
}否则{
endPos=indexOf(fileData,“,startPos+11);/”。长度=11
}
如果(startPos==-1){
跟踪(“未找到”);
}否则{
//提取和之间的字节
readBytes(extractedBytes,startPos+11,endPos);
}
免责声明:我还没有测试我的代码!只是有点不同的方法:
package
{
import flash.display.Sprite;
import flash.utils.ByteArray;
public class ByteArraySearch extends Sprite
{
public function ByteArraySearch()
{
super();
this.test();
}
private function test():void
{
var bytes:ByteArray = new ByteArray();
var start:ByteArray = new ByteArray();
var end:ByteArray = new ByteArray();
var subseq:ByteArray = new ByteArray();
var startPosition:int;
var endPosition:int;
bytes.writeUTFBytes("йYЯyeSВ–нkq(г<<<start>>>:xЪмЅdf”cйxЪsdfмЅ”cйdxЪмЅ”cй<<<end>>>В–нkВ");
start.writeUTFBytes("<<<start>>>");
end.writeUTFBytes("<<<end>>>");
startPosition = this.searchBytes(start, bytes);
endPosition = this.searchBytes(end, bytes);
subseq.writeBytes(bytes, startPosition + start.length,
endPosition - startPosition - start.length);
trace(startPosition, endPosition, subseq);
}
private function searchBytes(needle:ByteArray, heystack:ByteArray):int
{
var position:int;
var trackback:int;
var searcheable:int;
var head:int;
var readNeedle:Boolean;
var hasTrackBack:Boolean;
var needlePosition:int;
var current:int;
if (!needle || !needle.length || !heystack || !heystack.length ||
needle.length > heystack.length)
return -1;
searcheable = heystack.length - needle.length;
head = needle[0];
for (; position < searcheable; position++)
{
current = heystack[position];
// first state - we didn't yet find the first matching byte
if (!readNeedle)
{
// if this is the first mathing byte
if (readNeedle = current == head)
{
// then set both the trackback and position in the
// needle to the first byte in the needle, as this
// is what will be checked next
trackback = needlePosition = 1;
// we don't know yet if the first (or any later) byte
// can be tracked back to in our search, false by default
hasTrackBack = false;
}
}
else
{
// we found the match
if (needlePosition == needle.length)
{
position -= needlePosition;
break;
}
// if we haven't yet found a position to track back to
// and the current byte is the same as the first byte in the
// needle, then this is the trackback position.
if (!hasTrackBack && current == head)
hasTrackBack = true;
if (needle[needlePosition] == current)
{
// advance the position in the needle and the trackback,
// if we didn't find any point to track back to
needlePosition++;
if (!hasTrackBack) trackback = needlePosition;
}
else
{
// since the current byte didn't match, reset the position to
// the first trackback point that we found.
readNeedle = false;
position = position - needlePosition + trackback;
}
}
}
if (position == searcheable) position = -1;
return position;
}
}
}
包
{
导入flash.display.Sprite;
导入flash.utils.ByteArray;
公共类ByteArraySearch扩展了Sprite
{
公共函数ByteArraySearch()
{
超级();
这个。test();
}
私有函数测试():void
{
变量字节:ByteArray=newbytearray();
var start:ByteArray=newbytearray();
var end:ByteArray=新的ByteArray();
var subseq:ByteArray=新的ByteArray();
var起始位置:int;
变量结束位置:int;
字节。写入字节(“l YЯyeSЯb–зkq(Γ:xЪМЅdf“cоxЪsdfЅ”cоdxЪЅ“cоb–kз”);
start.writeUTFBytes(“”);
结束。可写字节(“”);
startPosition=this.searchBytes(开始,字节);
endPosition=this.searchBytes(结束,字节);
subseq.writeBytes(字节,起始位置+起始长度,
结束位置-开始位置-开始长度);
轨迹(起始位置、结束位置、水下);
}
专用函数searchBytes(指针:ByteArray,heystack:ByteArray):int
{
变量位置:int;
var trackback:int;
可搜索变量:int;
var头:int;
变量readNeedle:布尔值;
var hasTrackBack:布尔型;
位置:int;
无功电流:int;
如果(!needle | | |!needle.length | |!heystack | |!heystack.length||
指针长度>heystack.length)
返回-1;
searchable=heystack.length-pinder.length;
头=针[0];
对于(;位置<可搜索;位置++)
{
当前=heystack[位置];
//第一个状态-我们还没有找到第一个匹配的字节
如果(!读取指针)
{
//如果这是第一个匹配字节
如果(读针=电流==磁头)
{
//然后在中设置轨迹和位置
//指针到指针中的第一个字节,如下所示
//接下来要检查什么
trackback=needposition=1;
//我们还不知道第一个(或以后的)字节
//可以在搜索中追溯到,默认为false
hasTrackBack=false;
}
}
其他的
{
//我们找到了火柴
if(针位置==针长度)
{
位置-=针位;
打破
}
//如果我们还没有找到可以追踪的位置
//并且当前字节与中的第一个字节相同
//打捆针,然后这是轨迹反馈位置。
如果(!hasTrackBack&¤t==头)
hasTrackB