问题
I have a problem with getting parts of the ByteArray
data.
There is a binary text in fileData
:
var fileData:ByteArray = new ByteArray();
//..........here's code that fills this var with binary data
.....readBytes(fileData,0,1000);
//
Data is like this:
йYЯyeSВ–нkq(г<<<start>>>:xЪмЅdf”cйxЪsdfмЅ”cйdxЪмЅ”cй<<<end>>>В–нkВ
So, I need to find position of <<< start >>>
and <<< end >>>
and copy data, that is between them.
But searching fileData.toString().indexOf('<<< start >>>')
sometimes gets wrong position of this string, and sometimes can't find it at all.
What can I do to correctly determine the position of part of the data that I need?
回答1:
You should not use fileData.toString().indexOf()
since you are working with binary data. You have to search a sequence of bytes.
The following function retrieve the position of a specified pattern:
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;
}
Then you can use the function this way:
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);
}
Disclaimer: I haven't tested my code!
回答2:
Just a tad different approach:
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;
}
}
}
来源:https://stackoverflow.com/questions/11777348/flash-as3-i-need-binary-search-in-bytearray-data