Skip to content

Instantly share code, notes, and snippets.

@geNAZt
Created February 16, 2013 11:04
Show Gist options
  • Save geNAZt/4966446 to your computer and use it in GitHub Desktop.
Save geNAZt/4966446 to your computer and use it in GitHub Desktop.
var buffer = new Buffer(150000),
buffer.fill("0"),
events = require('events'),
util = require('util');
function BufferStream(buffer) {
events.EventEmitter.call(this);
this.readable = true;
this.writeable = false;
this.paused = true;
this._offset = 0;
this._chunkSize = 16;
this._buffer = buffer;
this._bufferLength = buffer.length;
this.pause = function() {
this.paused = true;
this._offset -= this._chunkSize;
}
this.resume = function() {
this.paused = false;
}
var self = this;
this._emitData = setInterval(function() {
if (self.paused === false) {
if (self._offset + self._chunkSize > self._bufferLength) {
self._chunkSize = self._bufferLength - self._offset;
}
self.emit('data', self._buffer(self._offset, self._offset + self._chunkSize));
self._offset += self._chunkSize;
}
}, 0);
}
util.inherits(BufferStream, events.EventEmitter);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment