actionscript-3 - flash as3 - 我需要在 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мЅ”cйdxЪмЅ”cй<<<end>>>В–нkВ

所以,我需要找到 <<< start >>> 的位置和 <<< end >>>并在它们之间复制数据。

但正在搜索 fileData.toString().indexOf('<<< start >>>')有时会弄错这个字符串的位置,有时根本找不到。

如何正确确定我需要的部分数据的位置?

最佳答案

你不应该使用 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;
}

然后你可以这样使用函数:

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);
}

免责声明:我没有测试我的代码!

关于actionscript-3 - flash as3 - 我需要在 byteArray 数据中进行二进制搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11777348/

相关文章:

ios - Windows 7 和 OpenSSL : creating p12 file

javascript - 如何使用 ActionScript 在 IE 中关闭网络摄像头

flash - 将 alpha 值添加到双线性调整大小算法

actionscript-3 - 算法 : Implementing custom hash table based dict

flash - 我可以在 ActionScript 中扩展 Function 的原型(prototype)吗?

javascript - Google Maps API 是否提供按名称搜索位置的方法?

java - 模糊快速字符串匹配和索引算法

elasticsearch - Elasticsearch搜索 bool +必须查询

actionscript-3 - 我可以将 swf 地址设为真实的 URL 而不使用# 吗?

actionscript-3 - 可播种随机数生成器,actionscript