//binary data writer tuned for creating
//postgres message packets as effeciently as possible by reusing the
//same buffer to avoid memcpy and limit memory allocations
var Writer = function(size) {
  this.size = size || 1024;
  this.buffer = new Buffer(this.size + 5);
  this.offset = 5;
  this.headerPosition = 0;
};

var p = Writer.prototype;

//resizes internal buffer if not enough size left
p._ensure = function(size) {
  var remaining = this.buffer.length - this.offset;
  if(remaining < size) {
    var oldBuffer = this.buffer;
    this.buffer = Buffer(oldBuffer.length + size);
    oldBuffer.copy(this.buffer);
  }
}

p.addInt32 = function(num) {
  this._ensure(4)
  this.buffer[this.offset++] = (num >>> 24 & 0xFF)
  this.buffer[this.offset++] = (num >>> 16 & 0xFF)
  this.buffer[this.offset++] = (num >>>  8 & 0xFF)
  this.buffer[this.offset++] = (num >>>  0 & 0xFF)
  return this;
}

p.addInt16 = function(num) {
  this._ensure(2)
  this.buffer[this.offset++] = (num >>>  8 & 0xFF)
  this.buffer[this.offset++] = (num >>>  0 & 0xFF)
  return this;
}

p.addCString = function(string) {
  //just write a 0 for empty or null strings
  if(!string) {
    this._ensure(1);
    this.buffer[this.offset++] = 0;
    return this;
  }
  var len = Buffer.byteLength(string) + 1;
  this._ensure(len);
  this.buffer.write(string, this.offset);
  this.offset += len;
  this.buffer[this.offset] = 0; //add null terminator
  return this;
}

p.addChar = function(char) {
  this._ensure(1);
  this.buffer.write(char, this.offset);
  this.offset++;
  return this;
}

p.addString = function(string) {
  var string = string || "";
  var len = Buffer.byteLength(string);
  this._ensure(len);
  this.buffer.write(string, this.offset);
  this.offset += len;
  return this;
}

p.getByteLength = function() {
  return this.offset - 5;
}

p.add = function(otherBuffer) {
  this._ensure(otherBuffer.length);
  otherBuffer.copy(this.buffer, this.offset);
  this.offset += otherBuffer.length;
  return this;
}

p.clear = function() {
  this.offset = 5;
  this.headerPosition = 0;
  this.lastEnd = 0;
}

//appends a header block to all the written data since the last
//subsequent header or to the beginning if there is only one data block
p.addHeader = function(code, last) {
  var origOffset = this.offset;
  this.offset = this.headerPosition;
  this.buffer[this.offset++] = code;
  //length is everything in this packet minus the code
  this.addInt32(origOffset - (this.headerPosition+1))
  //set next header position
  this.headerPosition = origOffset;
  //make space for next header
  this.offset = origOffset;
  if(!last) {
    this._ensure(5);
    this.offset += 5;
  }
}

p.join = function(code) {
  if(code) {
    this.addHeader(code, true);
  }
  return this.buffer.slice(code ? 0 : 5, this.offset);
}

p.flush = function(code) {
  var result = this.join(code);
  this.clear();
  return result;
}

module.exports = Writer;

@@ -545,16 +545,20 @@
 buffer =
+ new
  Buffer(
@@ -1181,63 +1181,19 @@
 ;%0A  
-  this.buffer%5Bthis.offset++%5D = 0;%0A    return this;
+%7D else %7B
 %0A  
-%7D%0A
   va
@@ -1229,14 +1229,12 @@
 ing)
- + 1
 ;%0A
+  
   th
@@ -1239,35 +1239,66 @@
 this._ensure(len
-);%0A
+ + 1); //+1 for null terminator%0A  
   this.buffer.wr
@@ -1312,35 +1312,42 @@
 ing, this.offset
+, len
 );%0A
+  
   this.offset +=
@@ -1352,16 +1352,21 @@
 += len;%0A
+  %7D%0A%0A
   this.b
@@ -1382,16 +1382,18 @@
 s.offset
+++
 %5D = 0; /
@@ -1393,19 +1393,16 @@
  = 0; //
-add
  null te
@@ -1505,32 +1505,35 @@
 har, this.offset
+, 1
 );%0A  this.offset

fix for writing null terminated buffers