How to use the binary.ByteString function in binary

To help you get started, we’ve selected a few binary examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github kriskowal / narwhal-lib / tests / commonjs / bytearray-tests.js View on Github external
// ByteArray(length)
    // New ByteArray filled with length zero bytes.
    b = new ByteArray(10);
    assert.isEqual(10, b.length);
    for (var i = 0; i < 10; i++)
        assert.isEqual(0, b.get(i));
    assert.isNaN(b.get(10));
    b.length = 234;
    assert.isEqual(234, b.length);
    assert.isEqual(0, b.get(10));
    assert.isEqual(0, b.get(233));
    assert.isNaN(b.get(234));
    
    // ByteArray(byteString)
    // Copy contents of byteString.
    b = new ByteArray(new ByteString(testArray));
    assert.isEqual(testArray.length, b.length);
    b.length = 345;
    assert.isEqual(345, b.length);
    assert.isEqual(1, b.get(0));
    assert.isEqual(4, b.get(3));
    assert.isEqual(0, b.get(4));
    
    // ByteArray(byteArray)
    // Copy byteArray.
    b = new ByteArray(new ByteArray(testArray));
    assert.isEqual(testArray.length, b.length);
    b.length = 456;
    assert.isEqual(456, b.length);
    assert.isEqual(1, b.get(0));
    assert.isEqual(4, b.get(3));
    assert.isEqual(0, b.get(4));
github kriskowal / narwhal-lib / tests / commonjs / bytestring-encodings-tests.js View on Github external
assert.isEqual("hello world", new ByteString("hello world", "US-ASCII").decodeToString("US-ASCII"));
    
    assert.isEqual("I ♥ JS", new ByteString("I ♥ JS", "UTF-8").decodeToString("UTF-8"));
    
    assert.isEqual("\u0024", new ByteString([0x24]).decodeToString("UTF-8"));
    assert.isEqual("\u00A2", new ByteString([0xC2,0xA2]).decodeToString("UTF-8"));
    assert.isEqual("\u20AC", new ByteString([0xE2,0x82,0xAC]).decodeToString("UTF-8"));
    // FIXME:
    //assert.isEqual("\u10ABCD", (new ByteString([0xF4,0x8A,0xAF,0x8D])).decodeToString("UTF-8"));
    
    assert.isEqual("\u0024", new ByteString("\u0024", "UTF-8").decodeToString("UTF-8"));
    assert.isEqual("\u00A2", new ByteString("\u00A2", "UTF-8").decodeToString("UTF-8"));
    assert.isEqual("\u20AC", new ByteString("\u20AC", "UTF-8").decodeToString("UTF-8"));
    assert.isEqual("\u10ABCD", new ByteString("\u10ABCD", "UTF-8").decodeToString("UTF-8"));
    
    assert.isEqual("\u0024", new ByteString("\u0024", "UTF-16").decodeToString("UTF-16"));
    assert.isEqual("\u00A2", new ByteString("\u00A2", "UTF-16").decodeToString("UTF-16"));
    assert.isEqual("\u20AC", new ByteString("\u20AC", "UTF-16").decodeToString("UTF-16"));
    assert.isEqual("\u10ABCD", new ByteString("\u10ABCD", "UTF-16").decodeToString("UTF-16"));
};
github kriskowal / narwhal-lib / tests / commonjs / bytestring-tests.js View on Github external
exports.testByteStringConstructor = function() {
    var testArray = [1,2,3,4];
    
    // ByteString()
    // Construct an empty byte string.
    var b1 = new ByteString();
    //assert.isTrue(b1 instanceof Binary, "not instanceof Binary");
    assert.isTrue(b1 instanceof ByteString, "not instanceof ByteString");
    assert.isEqual(0, b1.length);
    b1.length = 123;
    assert.isEqual(0, b1.length);
    
    // ByteString(byteString)
    // Copies byteString.
    var b2 = new ByteString(new ByteString(testArray));
    assert.isEqual(testArray.length, b2.length);
    b2.length = 123;
    assert.isEqual(testArray.length, b2.length);
    assert.isEqual(1, b2.get(0));
    assert.isEqual(4, b2.get(3));
    
    // ByteString(byteArray)
github kriskowal / narwhal-lib / tests / commonjs / bytestring-encodings-tests.js View on Github external
assert.isEqual("I ♥ JS", new ByteString("I ♥ JS", "UTF-8").decodeToString("UTF-8"));
    
    assert.isEqual("\u0024", new ByteString([0x24]).decodeToString("UTF-8"));
    assert.isEqual("\u00A2", new ByteString([0xC2,0xA2]).decodeToString("UTF-8"));
    assert.isEqual("\u20AC", new ByteString([0xE2,0x82,0xAC]).decodeToString("UTF-8"));
    // FIXME:
    //assert.isEqual("\u10ABCD", (new ByteString([0xF4,0x8A,0xAF,0x8D])).decodeToString("UTF-8"));
    
    assert.isEqual("\u0024", new ByteString("\u0024", "UTF-8").decodeToString("UTF-8"));
    assert.isEqual("\u00A2", new ByteString("\u00A2", "UTF-8").decodeToString("UTF-8"));
    assert.isEqual("\u20AC", new ByteString("\u20AC", "UTF-8").decodeToString("UTF-8"));
    assert.isEqual("\u10ABCD", new ByteString("\u10ABCD", "UTF-8").decodeToString("UTF-8"));
    
    assert.isEqual("\u0024", new ByteString("\u0024", "UTF-16").decodeToString("UTF-16"));
    assert.isEqual("\u00A2", new ByteString("\u00A2", "UTF-16").decodeToString("UTF-16"));
    assert.isEqual("\u20AC", new ByteString("\u20AC", "UTF-16").decodeToString("UTF-16"));
    assert.isEqual("\u10ABCD", new ByteString("\u10ABCD", "UTF-16").decodeToString("UTF-16"));
};
github kriskowal / narwhal-lib / tests / commonjs / bytestring-encodings-tests.js View on Github external
assert.isEqual("I ♥ JS", new ByteString("I ♥ JS", "UTF-8").decodeToString("UTF-8"));
    
    assert.isEqual("\u0024", new ByteString([0x24]).decodeToString("UTF-8"));
    assert.isEqual("\u00A2", new ByteString([0xC2,0xA2]).decodeToString("UTF-8"));
    assert.isEqual("\u20AC", new ByteString([0xE2,0x82,0xAC]).decodeToString("UTF-8"));
    // FIXME:
    //assert.isEqual("\u10ABCD", (new ByteString([0xF4,0x8A,0xAF,0x8D])).decodeToString("UTF-8"));
    
    assert.isEqual("\u0024", new ByteString("\u0024", "UTF-8").decodeToString("UTF-8"));
    assert.isEqual("\u00A2", new ByteString("\u00A2", "UTF-8").decodeToString("UTF-8"));
    assert.isEqual("\u20AC", new ByteString("\u20AC", "UTF-8").decodeToString("UTF-8"));
    assert.isEqual("\u10ABCD", new ByteString("\u10ABCD", "UTF-8").decodeToString("UTF-8"));
    
    assert.isEqual("\u0024", new ByteString("\u0024", "UTF-16").decodeToString("UTF-16"));
    assert.isEqual("\u00A2", new ByteString("\u00A2", "UTF-16").decodeToString("UTF-16"));
    assert.isEqual("\u20AC", new ByteString("\u20AC", "UTF-16").decodeToString("UTF-16"));
    assert.isEqual("\u10ABCD", new ByteString("\u10ABCD", "UTF-16").decodeToString("UTF-16"));
};
github kriskowal / narwhal-lib / packages / zip / lib / inflate.js View on Github external
var zip_inflate = function(bytes) {
    var out, buff;
    var i, j;

    zip_inflate_start();
    zip_inflate_data = bytes;
    zip_inflate_pos = 0;

    buff = new Array(1024);
    out = new ByteIO();
    while((i = zip_inflate_internal(buff, 0, buff.length)) > 0) {
	    out.write(new ByteString(buff.slice(0, i)));
    }
    zip_inflate_data = null; // G.C.
    return out.toByteString();
}
github kriskowal / narwhal-lib / lib / narwhal / global-commonjs.js View on Github external
"value": function(charset) {
            // RHINO bug: it thinks "this" is a Java string (?!)
            var binary = require("binary");
            return new binary.ByteString(String(this), charset);
        },
        "enumerable": false
github kriskowal / narwhal-lib / tests / commonjs / bytestring-encodings-tests.js View on Github external
exports.testToByteStringEncodings = function() {
    var testString = "I ♥ JS";
    assert.isEqual(testString, new ByteString(testString, "UTF-8").toByteString("UTF-8", "UTF-16").decodeToString("UTF-16"));
};
github ringo / stick / lib / middleware / gzip.js View on Github external
res.body = new ResponseFilter(res.body, function(part) {
                if (!(part instanceof Binary)) {
                    part = part.toByteString();
                }
                gzip.write(part);
                if (bytes.size() > 1024) {
                    const zipped = bytes.toByteArray();
                    bytes.reset();
                    return new ByteString(zipped);
                }
                return null;
            });
            res.body.close = function(fn) {
github kriskowal / narwhal-lib / lib / struct.js View on Github external
exports.binl2bin = function (bin, _characterSize) {
    if (util.no(_characterSize)) 
        _characterSize = exports.characterSize;
    var str = [];
    var mask = (1 << _characterSize) - 1;
    for (var i = 0; i < bin.length * 32; i += _characterSize)
        str.push((bin[i>>5] >>> (i % 32)) & mask);
    return binary.ByteString(str);
};