Skip to content

Instantly share code, notes, and snippets.

@zwetan
Created April 10, 2015 11:34
Show Gist options
  • Save zwetan/69fe6311d691e1818cbb to your computer and use it in GitHub Desktop.
Save zwetan/69fe6311d691e1818cbb to your computer and use it in GitHub Desktop.
#!/usr/bin/as3shebang --
/* Note:
Again the joy of using POSIX from AS3 :p
see:
Copy a file in an sane, safe and efficient way
http://stackoverflow.com/questions/10195343/copy-a-file-in-an-sane-safe-and-efficient-way
BUFSIZ
http://www.gnu.org/software/libc/manual/html_node/Controlling-Buffering.html
running this shell script on a 10GB file:
start copying ...
copied 10737418240 bytes in 21310ms
21310ms = 21.31 seconds
running this command line:
$ time cp test10GB copy10GB
real 0m21.906s
user 0m0.012s
sys 0m9.247s
yep, AS3 or native command line is about 21 seconds
that's what I call performance =)
In fact AS3 is faster:
- shebang send file to an exe
- redtamarin startup time
- as3shebang cut line and eval script
- then the code run
- see post
"Performing the copy with cp takes 0.44 seconds unbuffered
und 0.30 seconds buffered. So cp is a little bit slower
than the POSIX sample. Looks fine for me."
*/
import flash.utils.*;
import C.errno.*;
import C.stdio.*;
/**
* fcopy - copy file of any size using stdio functions
*
* @param origin the filepath to copy from.
* @param destination the filepath to copy to.
* @param buffer:uint how mnay bytes to copy per cycle (default is 8192, eg. BUFSIZ)
*/
var fcopy:Function = function( origin:String, destination:String, buffer:uint = 8192 ):Number
{
// open for reading
var source:* = fopen( origin, "rb" );
if( !source )
{
//trace( "source is null" );
var e:* = new CError( "", errno );
trace( e );
return -1;
}
// open for writing
var dest:* = fopen( destination, "wb" );
if( !dest )
{
fclose( source );
//trace( "dest is null" );
var e:* = new CError( "", errno );
trace( e );
return -1;
}
var b:* = new ByteArray();
var size:int;
var wrote:int;
var total:Number = 0;
while( size = fread( b, int(buffer), source ) )
{
wrote = fwrite( b, b.length, dest );
if( wrote < 0 )
{
//trace( "Error while writing stream" );
var e:* = new CError( "", errno );
trace( e );
return wrote;
}
//trace( "wrote " + wrote + " bytes" );
total += wrote;
b.clear();
}
// ALWAYS close your file streams!!
fclose( source );
fclose( dest );
if( size < 0)
{
//trace( "Error while reading stream" );
var e:* = new CError( "", errno );
trace( e );
return size;
}
//trace( "copied " + total + " bytes" );
return total;
}
// linux
// $ fallocate -l 100M test100MB
// $ fallocate -l 10G test10GB
// mac os x
// $ mkfile -n 100m test100MB
// $ mkfile -n 10g test10GB
//fcopy( "test100MB", "copy100MB" );
trace( "start copying ..." );
var t0:uint = getTimer();
var total:Number = fcopy( "test10GB", "copy10GB", 8192 * 100 );
var t1:uint = getTimer();
trace( "copied " + total + " bytes in " + (t1-t0) + "ms" );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment