Last active
December 20, 2015 05:19
-
-
Save marcolink/6077904 to your computer and use it in GitHub Desktop.
ByteArray Segmentation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ByteArrayHelper | |
{ | |
public static function getSegments(source : ByteArray, size: int = 1000) : Array | |
{ | |
var result : Array = []; | |
if(!source) | |
{ | |
return result; | |
} | |
var index : int = 0; | |
var total : int = Math.ceil(source.length / size); | |
while(index < total) | |
{ | |
var sendBytes : ByteArray = new ByteArray(); | |
source.position = index * size; | |
var currentLength : int = Math.min(size, source.bytesAvailable); | |
sendBytes.writeBytes(source, source.position, currentLength); | |
index++; | |
result.push(sendBytes); | |
} | |
return result; | |
} | |
public static function mergeSegments(segments : Array) : ByteArray | |
{ | |
var result : ByteArray = new ByteArray(); | |
for each (var segment : ByteArray in segments) | |
{ | |
result.writeBytes(segment); | |
} | |
result.position = 0; | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cuts ByteArray into segments of given length.if you have to transfer a huge amount of data via external interface, this is pretty handy