No Description

writable_streambuffer.js 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. var util = require("util"),
  2. stream = require("stream"),
  3. constants = require("./constants");
  4. // TODO: clear up specs on returning false from a write and emitting a drain event.
  5. // Does this mean if I return false from a write, I should ignore any write requests between that false return and the drain event?
  6. var WritableStreamBuffer = module.exports = function(opts) {
  7. var that = this;
  8. stream.Stream.call(this);
  9. opts = opts || {};
  10. var initialSize = opts.initialSize || constants.DEFAULT_INITIAL_SIZE;
  11. var incrementAmount = opts.incrementAmount || constants.DEFAULT_INCREMENT_AMOUNT;
  12. var buffer = new Buffer(initialSize);
  13. var size = 0;
  14. this.writable = true;
  15. this.readable = false;
  16. this.size = function() {
  17. return size;
  18. };
  19. this.maxSize = function() {
  20. return buffer.length;
  21. };
  22. this.getContents = function(length) {
  23. if(!size) return false;
  24. var data = new Buffer(Math.min(length || size, size));
  25. buffer.copy(data, 0, 0, data.length);
  26. if(data.length < size)
  27. buffer.copy(buffer, 0, data.length);
  28. size -= data.length;
  29. return data;
  30. };
  31. this.getContentsAsString = function(encoding, length) {
  32. if(!size) return false;
  33. var data = buffer.toString(encoding || "utf8", 0, Math.min(length || size, size));
  34. var dataLength = Buffer.byteLength(data);
  35. if(dataLength < size)
  36. buffer.copy(buffer, 0, dataLength);
  37. size -= dataLength;
  38. return data;
  39. };
  40. var increaseBufferIfNecessary = function(incomingDataSize) {
  41. if((buffer.length - size) < incomingDataSize) {
  42. var factor = Math.ceil((incomingDataSize - (buffer.length - size)) / incrementAmount);
  43. var newBuffer = new Buffer(buffer.length + (incrementAmount * factor));
  44. buffer.copy(newBuffer, 0, 0, size);
  45. buffer = newBuffer;
  46. }
  47. };
  48. this.write = function(data, encoding, callback) {
  49. if(!that.writable) return;
  50. if(Buffer.isBuffer(data)) {
  51. increaseBufferIfNecessary(data.length);
  52. data.copy(buffer, size, 0);
  53. size += data.length;
  54. }
  55. else {
  56. data = data + "";
  57. increaseBufferIfNecessary(Buffer.byteLength(data));
  58. buffer.write(data, size, encoding || "utf8");
  59. size += Buffer.byteLength(data);
  60. }
  61. if(typeof callback === "function") { callback() ;}
  62. };
  63. this.end = function() {
  64. var args = Array.prototype.slice.apply(arguments);
  65. if(args.length) that.write.apply(that, args);
  66. that.emit('finish');
  67. that.destroy();
  68. };
  69. this.destroySoon = this.destroy = function() {
  70. that.writable = false;
  71. that.emit("close");
  72. };
  73. };
  74. util.inherits(WritableStreamBuffer, stream.Stream);