blob: 90f6155746010d1eadebdf66afcdff7a6b826c10 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001/* very simple input/output stream interface */
2var Stream = function() {
3};
4
5// input streams //////////////
6/** Returns the next byte, or -1 for EOF. */
7Stream.prototype.readByte = function() {
8 throw new Error("abstract method readByte() not implemented");
9};
10/** Attempts to fill the buffer; returns number of bytes read, or
11 * -1 for EOF. */
12Stream.prototype.read = function(buffer, bufOffset, length) {
13 var bytesRead = 0;
14 while (bytesRead < length) {
15 var c = this.readByte();
16 if (c < 0) { // EOF
17 return (bytesRead===0) ? -1 : bytesRead;
18 }
19 buffer[bufOffset++] = c;
20 bytesRead++;
21 }
22 return bytesRead;
23};
24Stream.prototype.seek = function(new_pos) {
25 throw new Error("abstract method seek() not implemented");
26};
27
28// output streams ///////////
29Stream.prototype.writeByte = function(_byte) {
30 throw new Error("abstract method readByte() not implemented");
31};
32Stream.prototype.write = function(buffer, bufOffset, length) {
33 var i;
34 for (i=0; i<length; i++) {
35 this.writeByte(buffer[bufOffset++]);
36 }
37 return length;
38};
39Stream.prototype.flush = function() {
40};
41
42module.exports = Stream;