Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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)
// Use the contents of byteArray.
var b2 = new ByteString(new ByteArray(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(arrayOfNumbers)
// Use the numbers in arrayOfNumbers as the bytes.
// If any element is outside the range 0...255, an exception (TODO) is thrown.
var b3 = new ByteString(testArray);
assert.isEqual(testArray.length, b3.length);
b3.length = 123;
assert.isEqual(testArray.length, b3.length);
assert.isEqual(1, b3.get(0));
assert.isEqual(4, b3.get(3));
};
exports.testDecodeToString = function() {
assert.isEqual("hello world", new ByteArray("hello world", "US-ASCII").decodeToString("US-ASCII"));
assert.isEqual("I ♥ JS", new ByteArray("I ♥ JS", "UTF-8").decodeToString("UTF-8"));
assert.isEqual("\u0024", new ByteArray([0x24]).decodeToString("UTF-8"));
assert.isEqual("\u00A2", new ByteArray([0xC2,0xA2]).decodeToString("UTF-8"));
assert.isEqual("\u20AC", new ByteArray([0xE2,0x82,0xAC]).decodeToString("UTF-8"));
// FIXME:
//assert.isEqual("\u10ABCD", (new ByteArray([0xF4,0x8A,0xAF,0x8D])).decodeToString("UTF-8"));
assert.isEqual("\u0024", new ByteArray("\u0024", "UTF-8").decodeToString("UTF-8"));
assert.isEqual("\u00A2", new ByteArray("\u00A2", "UTF-8").decodeToString("UTF-8"));
assert.isEqual("\u20AC", new ByteArray("\u20AC", "UTF-8").decodeToString("UTF-8"));
assert.isEqual("\u10ABCD", new ByteArray("\u10ABCD", "UTF-8").decodeToString("UTF-8"));
assert.isEqual("\u0024", new ByteArray("\u0024", "UTF-16").decodeToString("UTF-16"));
assert.isEqual("\u00A2", new ByteArray("\u00A2", "UTF-16").decodeToString("UTF-16"));
assert.isEqual("\u20AC", new ByteArray("\u20AC", "UTF-16").decodeToString("UTF-16"));
assert.isEqual("\u10ABCD", new ByteArray("\u10ABCD", "UTF-16").decodeToString("UTF-16"));
};
exports.testByteArrayConcat = function() {
var b = new ByteArray();
var b1 = b.concat(new ByteArray([1,2,3]));
assert.isEqual(3, b1.length);
assert.isEqual(1, b1.get(0));
assert.isEqual(2, b1.get(1));
assert.isEqual(3, b1.get(2));
var b2 = b1.concat(new ByteString([4,5,6]));
assert.isEqual(6, b2.length);
assert.isEqual(1, b2.get(0));
assert.isEqual(3, b2.get(2));
assert.isEqual(4, b2.get(3));
assert.isEqual(6, b2.get(5));
var b3 = b2.concat([b, b1, b2, new ByteString(), new ByteArray()]);
assert.isEqual(b.length + b1.length + b2.length + b2.length, b3.length);
exports.testByteArrayPush = function() {
var b1 = new ByteArray();
assert.isEqual(3, b1.push(1,2,3));
assert.isEqual(3, b1.length);
assert.isEqual(1, b1.get(0));
assert.isEqual(2, b1.get(1));
assert.isEqual(3, b1.get(2));
var b2 = new ByteArray([7, 8, 9]);
assert.isEqual(6, b1.push.apply(b1, b2.toArray()));
assert.isEqual(6, b1.length);
assert.isEqual(7, b1.get(3));
assert.isEqual(8, b1.get(4));
assert.isEqual(9, b1.get(5));
};
exports.testCopyTreeAsFiles = function() {
assert.isTrue(Files.exists(Paths.get(testFile)));
var stream = fs.open(testFile, "wb");
var ba = new ByteArray([0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]);
try {
stream.write(ba);
stream.flush();
} finally {
stream.close();
assert.isTrue(stream.closed());
}
var file2 = String(Files.createTempFile("copyofcopytest", ".txt").toAbsolutePath());
fs.copyTree(testFile, file2);
assert.isTrue(Files.exists(Paths.get(testFile)));
assert.isTrue(Files.exists(Paths.get(file2)));
assert.isFalse(Files.isDirectory(Paths.get(testFile)));
assert.isFalse(Files.isDirectory(Paths.get(file2)));
exports.testToArrayEncodings = function() {
var a1;
a1 = new ByteArray("\u0024\u00A2\u20AC", "UTF-8").toArray("UTF-8");
assert.isEqual(3, a1.length);
assert.isEqual(0x24, a1[0]);
assert.isEqual(0xA2, a1[1]);
assert.isEqual(0x20AC, a1[2]);
a1 = new ByteArray("\u0024\u00A2\u20AC", "UTF-16").toArray("UTF-16");
assert.isEqual(3, a1.length);
assert.isEqual(0x24, a1[0]);
assert.isEqual(0xA2, a1[1]);
assert.isEqual(0x20AC, a1[2]);
};
exports.testToByteString = function() {
var b1 = new ByteArray([1,2,3]),
b2 = b1.toByteString();
assert.isEqual(b1.length, b2.length);
assert.isEqual(b1.get(0), b2.get(0));
assert.isEqual(b1.get(2), b2.get(2));
assert.isEqual(1, b1.get(0));
assert.isEqual(1, b2.get(0));
b1.set(0, 10);
assert.isEqual(10, b1.get(0));
assert.isEqual(1, b2.get(0));
};
var ByteArray = require('binary').ByteArray;
var n = 128 * 1024;
var b = new ByteArray(n);
for (var i = 0; i < n; i++) {
b[i] = Math.floor(32 + (127 - 32) * Math.random());
}
exports.app = function(request) {
return {
status:200,
headers:{
"Content-Type":"text/plain"
},
body:[b.decodeToString("ascii")]
};
};
exports.createSalt = function() {
var salt = new ByteArray(8);
var random = SecureRandom.getInstance("SHA1PRNG");
random.nextBytes(salt);
return salt;
};