blob: 23ff0fb03f220b2c2ea319736d4e9fbf97603935 [file] [log] [blame]
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/**
* @fileoverview Represents a write stream of VOM messages backed by a byte
* array.
* @private
*/
module.exports = ByteStreamMessageWriter;
var RawVomWriter = require('./raw-vom-writer.js');
/**
* Create a VOM message writer that writes to a byte array.
* @constructor
* @memberof module:vanadium.vom
*/
function ByteStreamMessageWriter() {
this.rawWriter = new RawVomWriter();
this.rawWriter._writeRawBytes(new Uint8Array([0x80]));
}
/**
* Write a value message.
* @private
* @param {number} typeId The type ID of the message.
* @param {boolean} sendLength true if the message length should be sent in the
* header, false otherwise.
* @param {Uint8Array} message The body of the message.
*/
ByteStreamMessageWriter.prototype.writeValueMessage = function(
typeId, sendLength, message) {
if (typeId <= 0) {
throw new Error('Type ids should be positive integers.');
}
this.rawWriter.writeInt(typeId);
if (sendLength) {
this.rawWriter.writeUint(message.length);
}
this.rawWriter._writeRawBytes(message);
};
/**
* Write a type message.
* @private
* @param {number} typeId The type ID to define.
* @param {Uint8Array} message The body of the type description message.
*/
ByteStreamMessageWriter.prototype.writeTypeMessage = function(typeId, message) {
if (typeId <= 0) {
throw new Error('Type ids should be positive integers.');
}
this.rawWriter.writeInt(-typeId);
this.rawWriter.writeUint(message.length);
this.rawWriter._writeRawBytes(message);
};
/**
* Get the written bytes.
* @return {Uint8Array} The bytes that were written.
*/
ByteStreamMessageWriter.prototype.consumeBytes = function() {
var bytes = this.rawWriter.getBytes();
this.rawWriter = new RawVomWriter();
return bytes;
};