Code coverage report for usr/local/google/home/trevj/src/uproxy-lib/build/dev/uproxy-lib/arraybuffers/arraybuffers.js

Statements: 98.73% (78 / 79)      Branches: 83.33% (10 / 12)      Functions: 100% (11 / 11)      Lines: 98.73% (78 / 79)      Ignored: none     

All files » usr/local/google/home/trevj/src/uproxy-lib/build/dev/uproxy-lib/arraybuffers/ » arraybuffers.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123    1 15 15 15 2 13 111     13   1       1 6 6 6 26     6 6 6 26 26   6   1       1 7 7 7 7 42 42 2     40   42   7   1   1 5 5 5 21   5   1   1 5 5 5 21   5   1     1 14 14 14 57   14   1     1 7 4   3 3 3 3 21   3   1         1 7 7 7 30   7   1   1 7 7 7 30   7   1  
// Byte-wise equality check of array buffers by comparison of each byte's
// value.
function byteEquality(b1, b2) {
    var a1 = new Uint8Array(b1);
    var a2 = new Uint8Array(b2);
    if (a1.byteLength !== a2.byteLength)
        return false;
    for (var i = 0; i < a1.byteLength; ++i) {
        Iif (a1[i] !== a2[i])
            return false;
    }
    return true;
}
exports.byteEquality = byteEquality;
// Concat |ArrayBuffer|s into a single ArrayBuffer. If size is given, then
// the destination array buffer is of the given size. If size is not given or
// zero, the  size of all buffers is summed to make the new array buffer.
function concat(buffers, size) {
    Eif (!size) {
        size = 0;
        buffers.forEach(function (a) {
            size += a.byteLength;
        });
    }
    var accumulatorBuffer = new Uint8Array(size);
    var location = 0;
    buffers.forEach(function (a) {
        accumulatorBuffer.set(new Uint8Array(a), location);
        location += a.byteLength;
    });
    return accumulatorBuffer.buffer;
}
exports.concat = concat;
// Break an array buffer into multiple array buffers that are at most |size|
// types long. Returns 'chunked' array buffers. The array buffers are in
// order such that |byteEquality(concat(chunk(a, n)),a)===true|
function chunk(buffer, size) {
    var startByte = 0;
    var endByte;
    var chunks = [];
    while (startByte < buffer.byteLength) {
        endByte = Math.min(startByte + size, buffer.byteLength);
        if (startByte === 0 && endByte === buffer.byteLength) {
            chunks.push(buffer);
        }
        else {
            chunks.push(buffer.slice(startByte, endByte));
        }
        startByte += size;
    }
    return chunks;
}
exports.chunk = chunk;
// Converts an ArrayBuffer to a string.
function arrayBufferToString(buffer) {
    var bytes = new Uint8Array(buffer);
    var a = [];
    for (var i = 0; i < bytes.length; ++i) {
        a.push(String.fromCharCode(bytes[i]));
    }
    return a.join('');
}
exports.arrayBufferToString = arrayBufferToString;
// Converts a string to an ArrayBuffer.
function stringToArrayBuffer(s) {
    var buffer = new ArrayBuffer(s.length);
    var bytes = new Uint8Array(buffer);
    for (var i = 0; i < s.length; ++i) {
        bytes[i] = s.charCodeAt(i);
    }
    return buffer;
}
exports.stringToArrayBuffer = stringToArrayBuffer;
// Converts an ArrayBuffer to a string of hex codes (of the regexp form
// /(hh\.)*hh/).
function arrayBufferToHexString(buffer) {
    var bytes = new Uint8Array(buffer);
    var a = [];
    for (var i = 0; i < buffer.byteLength; ++i) {
        a.push(bytes[i].toString(16));
    }
    return a.join('.');
}
exports.arrayBufferToHexString = arrayBufferToHexString;
// Converts a HexString of the regexp form /(hh\.)*hh/ (where `h` is a
// hex-character) to an ArrayBuffer.
function hexStringToArrayBuffer(hexString) {
    if (hexString === '') {
        return new ArrayBuffer(0);
    }
    var hexChars = hexString.split('.');
    var buffer = new ArrayBuffer(hexChars.length);
    var bytes = new Uint8Array(buffer);
    for (var i = 0; i < hexChars.length; ++i) {
        bytes[i] = parseInt('0x' + hexChars[i]);
    }
    return buffer;
}
exports.hexStringToArrayBuffer = hexStringToArrayBuffer;
// Converts arrayBuffer which has a string encoded in UTF8 to a
// Javascript string.
//
// Note: the array buffer should have a valid string with no zero inside.
function arrayBufferDecodedAsUtf8String(buffer) {
    var bytes = new Uint8Array(buffer);
    var a = [];
    for (var i = 0; i < bytes.length; ++i) {
        a.push(String.fromCharCode(bytes[i]));
    }
    return decodeURIComponent(escape(a.join('')));
}
exports.arrayBufferDecodedAsUtf8String = arrayBufferDecodedAsUtf8String;
// Converts javascript string to array buffer using UTF8 encoding.
function stringToUtf8EncodedArrayBuffer(str) {
    var strUtf8 = unescape(encodeURIComponent(str));
    var ab = new Uint8Array(strUtf8.length);
    for (var i = 0; i < strUtf8.length; i++) {
        ab[i] = strUtf8.charCodeAt(i);
    }
    return ab.buffer;
}
exports.stringToUtf8EncodedArrayBuffer = stringToUtf8EncodedArrayBuffer;