![](/img/trans.png)
[英]Can I send more than one 'stream' of jpeg ByteArray data in a single URLRequest and output 2 images? Flash AS3 -> PHP
[英]flash as3 - I need binary search in byteArray data
我在獲取部分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;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.