diffusion
Version:
Diffusion JavaScript client
158 lines (157 loc) • 5.51 kB
JavaScript
"use strict";
/**
* @module IO
*
* @brief Basic stream based input and output using an underlying `Uint8Array`
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.BufferOutputStream = void 0;
var errors_1 = require("./../../errors/errors");
var uInt8Util = require("../util/uint8array");
/**
* Wrapper around a single Uint8Array, providing auto-resizing capabilities.
*
* All write operations will append to the internal buffer.
*
* Loosely inspired by Java's ByteArrayOutputStream.
*/
var BufferOutputStream = /** @class */ (function () {
/**
* Create a new output stream from a buffer
*
* @param initial the initial size for the internal Uint8Array, or buffer to use as source.
*/
function BufferOutputStream(initial) {
if (initial === void 0) { initial = 32; }
if (typeof (initial) === 'number') {
this.buffer = new Uint8Array(initial);
this.count = 0;
}
else {
this.buffer = initial;
this.count = initial.length;
}
}
/**
* Write a single byte.
*
* @param val the byte to append
*/
BufferOutputStream.prototype.write = function (val) {
this.ensureCapacity(this.count + 1);
this.buffer[this.count++] = val;
};
/**
* Write many bytes.
*
* The contents of the provided buffer will be copied and appended. The full
* length of the buffer will be copied, irrespective of byte content.
*
* @param buffer - The bytes to append.
* @param [offset=0] - The offset to start copying from the provided buffer
* @param [length=buffer.length] - The amount of bytes to copy from the provided buffer
*/
BufferOutputStream.prototype.writeMany = function (buffer, offset, length) {
if (offset === void 0) { offset = 0; }
if (length === void 0) { length = buffer.length; }
if (length === 0) {
// nothing to write, just bail out here
return;
}
this.ensureCapacity(this.count + length);
this.buffer.set(buffer.subarray(offset, offset + length), this.count);
this.count += length;
};
/**
* Write a String in UTF-8 format.
*
* @param val the string value to be appended.
*/
BufferOutputStream.prototype.writeString = function (val) {
var strArr = uInt8Util.uint8FromUtf8(val);
var length = strArr.length;
this.ensureCapacity(this.count + length);
// strings are encoded as UTF-8 by default
this.buffer.set(strArr, this.count);
this.count += length;
};
/**
* Write a signed 8-bit integer.
* @param val the number to write
*/
BufferOutputStream.prototype.writeInt8 = function (val) {
this.ensureCapacity(this.count + 1);
uInt8Util.writeInt8(this.buffer, this.count++, val);
};
/**
* Write a signed, big-endian 32-bit integer.
* @param val the number to write
*/
BufferOutputStream.prototype.writeInt32 = function (val) {
this.ensureCapacity(this.count + 4);
uInt8Util.writeInt32BE(this.buffer, this.count, val);
this.count += 4;
};
/**
* Write a signed, big-endian 64-bit long.
* @param val the number to write.
*/
BufferOutputStream.prototype.writeInt64 = function (val) {
this.ensureCapacity(this.count + 8);
uInt8Util.writeInt32BE(this.buffer, this.count, val.getHighBits());
uInt8Util.writeInt32BE(this.buffer, this.count + 4, val.getLowBits());
this.count += 8;
};
/**
* Return the written bytes as a buffer, sized to the last value written.
* <p>
* Any modifications to the returned buffer will be reflected in this OutputStream.
*
* @returns A Uint8Array containing all the bytes current written to this OutputStream.
*/
BufferOutputStream.prototype.getBuffer = function () {
return this.buffer.subarray(0, this.count);
};
/**
* Return written bytes as a base64 encoded string.
* @returns The base64 encoded string
*/
BufferOutputStream.prototype.getBase64 = function () {
return uInt8Util.uint8ToBase64(this.getBuffer());
};
/**
* Ensure that the output stream has enough capacity
*
* @param min the capacity the buffer should have
*/
BufferOutputStream.prototype.ensureCapacity = function (min) {
if (min - this.buffer.length > 0) {
this.grow(min);
}
};
/**
* Grow the buffer up to the next minimum capacity (or higher).
*
* @param minCapacity the minimum capacity required
* @throws If the new capacity exceeds that with which a Uint8Array can be created,
* an error will be thrown.
*/
BufferOutputStream.prototype.grow = function (minCapacity) {
var oldCapacity = this.buffer.length;
/* eslint-disable-next-line no-bitwise */
var newCapacity = oldCapacity << 1;
if (newCapacity - minCapacity < 0) {
newCapacity = minCapacity;
}
try {
var replacement = new Uint8Array(newCapacity);
replacement.set(this.buffer);
this.buffer = replacement;
}
catch (e) {
throw new errors_1.IOError('Unable to resize BufferOutputStream to ' + newCapacity);
}
};
return BufferOutputStream;
}());
exports.BufferOutputStream = BufferOutputStream;