// Generated by CoffeeScript 1.8.0
(function () {
  var ByteBuffer, DataOutputBuffer, DataOutputStream,
    __bind = function (fn, me) { return function () { return fn.apply(me, arguments); }; },
    __slice = [].slice,
    __hasProp = {}.hasOwnProperty,
    __extends = function (child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor () { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  ByteBuffer = require('bytebuffer');

  DataOutputStream = (function () {
    function DataOutputStream (out) {
      this.out = out;
      this.writeDelimitedBuffers = __bind(this.writeDelimitedBuffers, this);
      this.writeInt = __bind(this.writeInt, this);
      this.writeByte = __bind(this.writeByte, this);
      this.write = __bind(this.write, this);
      this.written = 0;
    }

    DataOutputStream.prototype.write = function (b, offset, length) {
      if (!Buffer.isBuffer(b)) {
        b = new Buffer(b);
      }
      if (length == null) {
        length = b.length;
      }
      if (offset) {
        b = b.slice(offset, offset + length);
      }
      this.out.write(b);
      return this.written += length;
    };

    DataOutputStream.prototype.writeByte = function (b) {
      if (!Buffer.isBuffer(b)) {
        if (isNaN(b)) {
          b = new Buffer(b[0]);
        } else {
          b = new Buffer([b]);
        }
      }
      return this.write(b);
    };

    DataOutputStream.prototype.writeInt = function (i) {
      var b;

      b = new Buffer(4);
      b.writeInt32BE(i, 0);
      return this.write(b);
    };

    DataOutputStream.prototype.writeDelimitedBuffers = function () {
      var bb, buffer, buffers, i, length, varInt, _results;

      buffers = arguments.length >= 1 ? __slice.call(arguments, 0) : [];
      length = 0;
      varInt = [];
      for (i in buffers) {
        buffer = buffers[i];
        varInt[i] = ByteBuffer.calculateVarint32(buffer.length);
        length += varInt[i] + buffer.length;
      }
      this.writeInt(length);
      _results = [];
      for (i in buffers) {
        buffer = buffers[i];
        bb = new ByteBuffer(varInt[i]);
        bb.writeVarint32(buffer.length);
        this.write(bb.toBuffer());
        _results.push(this.write(buffer));
      }
      return _results;
    };

    return DataOutputStream;
  })();

  DataOutputBuffer = (function (_super) {
    __extends(DataOutputBuffer, _super);

    function DataOutputBuffer () {
      DataOutputBuffer.__super__.constructor.call(this);
    }

    return DataOutputBuffer;
  })(DataOutputStream);

  module.exports.DataOutputBuffer = DataOutputBuffer;

  module.exports.DataOutputStream = DataOutputStream;
}).call(this);
