| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | /* very simple input/output stream interface */ |
| 2 | var Stream = function() { |
| 3 | }; |
| 4 | |
| 5 | // input streams ////////////// |
| 6 | /** Returns the next byte, or -1 for EOF. */ |
| 7 | Stream.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. */ |
| 12 | Stream.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 | }; |
| 24 | Stream.prototype.seek = function(new_pos) { |
| 25 | throw new Error("abstract method seek() not implemented"); |
| 26 | }; |
| 27 | |
| 28 | // output streams /////////// |
| 29 | Stream.prototype.writeByte = function(_byte) { |
| 30 | throw new Error("abstract method readByte() not implemented"); |
| 31 | }; |
| 32 | Stream.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 | }; |
| 39 | Stream.prototype.flush = function() { |
| 40 | }; |
| 41 | |
| 42 | module.exports = Stream; |