Skip to content

Instantly share code, notes, and snippets.

@marcolink
Last active December 20, 2015 05:19
Show Gist options
  • Save marcolink/6077904 to your computer and use it in GitHub Desktop.
Save marcolink/6077904 to your computer and use it in GitHub Desktop.
ByteArray Segmentation
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;
}
}
@marcolink
Copy link
Author

cuts ByteArray into segments of given length.if you have to transfer a huge amount of data via external interface, this is pretty handy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment