module.exports = BinaryWriter;

function BinaryWriter(size, allowResize) {
    this.buffer = new Buffer(size);
    this.position = 0;
    this.allowResize = allowResize;
}

function _write(write, size) {
    return function (value) {
        this.ensureSize(size);

        write.call(this.buffer, value, this.position);
        this.position += size;
    };
}

BinaryWriter.prototype.writeUInt8 = _write(Buffer.prototype.writeUInt8, 1);
BinaryWriter.prototype.writeUInt16LE = _write(Buffer.prototype.writeUInt16LE, 2);
BinaryWriter.prototype.writeUInt16BE = _write(Buffer.prototype.writeUInt16BE, 2);
BinaryWriter.prototype.writeUInt32LE = _write(Buffer.prototype.writeUInt32LE, 4);
BinaryWriter.prototype.writeUInt32BE = _write(Buffer.prototype.writeUInt32BE, 4);
BinaryWriter.prototype.writeInt8 = _write(Buffer.prototype.writeInt8, 1);
BinaryWriter.prototype.writeInt16LE = _write(Buffer.prototype.writeInt16LE, 2);
BinaryWriter.prototype.writeInt16BE = _write(Buffer.prototype.writeInt16BE, 2);
BinaryWriter.prototype.writeInt32LE = _write(Buffer.prototype.writeInt32LE, 4);
BinaryWriter.prototype.writeInt32BE = _write(Buffer.prototype.writeInt32BE, 4);
BinaryWriter.prototype.writeFloatLE = _write(Buffer.prototype.writeFloatLE, 4);
BinaryWriter.prototype.writeFloatBE = _write(Buffer.prototype.writeFloatBE, 4);
BinaryWriter.prototype.writeDoubleLE = _write(Buffer.prototype.writeDoubleLE, 8);
BinaryWriter.prototype.writeDoubleBE = _write(Buffer.prototype.writeDoubleBE, 8);

BinaryWriter.prototype.writeBuffer = function (buffer) {
    this.ensureSize(buffer.length);

    buffer.copy(this.buffer, this.position, 0, buffer.length);
    this.position += buffer.length;
};

BinaryWriter.prototype.writeVarInt = function (value) {
    var length = 1;

    while ((value & 0xFFFFFF80) !== 0) {
        this.writeUInt8((value & 0x7F) | 0x80);
        value >>>= 7;
        length++;
    }

    this.writeUInt8(value & 0x7F);

    return length;
};

BinaryWriter.prototype.ensureSize = function (size) {
    if (this.buffer.length < this.position + size) {
        if (this.allowResize) {
            var tempBuffer = new Buffer(this.position + size);
            this.buffer.copy(tempBuffer, 0, 0, this.buffer.length);
            this.buffer = tempBuffer;
        }
        else {
            throw new RangeError('index out of range');
        }
    }
};

@@ -56,32 +56,45 @@
 ize, allowResize
+, asBigEndian
 ) %7B%0A    this.buf
@@ -176,16 +176,61 @@
 Resize;%0A
+    this.asBigEndian = asBigEndian %7C%7C false;%0A
 %7D%0A%0Afunct
@@ -245,16 +245,27 @@
 te(write
+LE, writeBE
 , size) 
@@ -324,24 +324,58 @@
 ize(size);%0A%0A
+        if (this.asBigEndian)%0A    
         writ
@@ -375,16 +375,18 @@
    write
+BE
 .call(th
@@ -419,16 +419,91 @@
 ition);%0A
+        else%0A            writeLE.call(this.buffer, value, this.position);%0A%0A
         
@@ -603,38 +603,28 @@
 writeUInt8, 
-1);%0ABinaryWrit
+Buff
 er.prototype
@@ -633,34 +633,33 @@
 riteUInt
-16LE = _write(Buff
+8, 1);%0ABinaryWrit
 er.proto
@@ -674,34 +674,30 @@
 teUInt16
-LE, 2);%0ABinaryWrit
+ = _write(Buff
 er.proto
@@ -708,36 +708,28 @@
 .writeUInt16
-BE = _write(
+LE, 
 Buffer.proto
@@ -782,26 +782,24 @@
 .writeUInt32
-LE
  = _write(Bu
@@ -824,38 +824,28 @@
 teUInt32LE, 
-4);%0ABinaryWrit
+Buff
 er.prototype
@@ -854,38 +854,40 @@
 riteUInt32BE
- = _write(Buff
+, 4);%0ABinaryWrit
 er.prototype
@@ -896,32 +896,26 @@
 rite
-U
 Int
-32BE, 4);%0ABinaryWrit
+8 = _write(Buff
 er.p
@@ -928,34 +928,26 @@
 pe.writeInt8
+,
  
-= _write(
 Buffer.proto
@@ -999,18 +999,16 @@
 iteInt16
-LE
  = _writ
@@ -1044,57 +1044,8 @@
 LE, 
-2);%0ABinaryWriter.prototype.writeInt16BE = _write(
 Buff
@@ -1108,18 +1108,16 @@
 iteInt32
-LE
  = _writ
@@ -1153,57 +1153,8 @@
 LE, 
-4);%0ABinaryWriter.prototype.writeInt32BE = _write(
 Buff
@@ -1217,18 +1217,16 @@
 iteFloat
-LE
  = _writ
@@ -1262,57 +1262,8 @@
 LE, 
-4);%0ABinaryWriter.prototype.writeFloatBE = _write(
 Buff
@@ -1327,18 +1327,16 @@
 teDouble
-LE
  = _writ
@@ -1373,58 +1373,8 @@
 LE, 
-8);%0ABinaryWriter.prototype.writeDoubleBE = _write(
 Buff

add support for big endian byte order