Search code examples
javascriptphantomjsarraybuffer

Output a Uint8ClampedArray buffer as a stream of bytes


In a PhantomJS script, I have a Uint8ClampedArray (image data from a HTML canvas) that I would like to output to stdout as a byte stream.

In Node.js I would do:

buf = new Buffer(...);
process.stdout.write(buf);

How would I go about doing the same thing in PhantomJS?

I can't just output String.fromCharCode() for each of my bytes, as for example String.fromCharCode(255) outputs two bytes, not one (i.e. any byte between 128 and 255 will output two bytes).


Solution

  • I've been able to write to stdout using the fs phantomjs module, thus being able to write as binary:

    var fs = require('fs');
    var stream = fs.open('/dev/stdout', 'wb');
    stream.write(String.fromCharCode(128));
    stream.flush();
    

    It is still pretty slow for large volume of data, for some reason.