我在获取部分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);
}
免责声明:我还没有测试我的代码!
只是一种稍微不同的方法:
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;
}
}
}