Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: ef1c8b62fb
Fetching contributors…

Cannot retrieve contributors at this time

file 144 lines (113 sloc) 3.352 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
buffertools = require('../buffertools')

###
A Buffer-like class that tracks position.

As values are written, the position advances by the size of the written data.
When writing, automatically allocates new buffers if there's not enough space.
###
class WritableTrackingBuffer
  constructor: (@sizeIncrement, @encoding) ->
    @encoding ||= 'ucs2'

    @buffer = new Buffer(@sizeIncrement)
    @position = 0

    @.__defineGetter__("data", ->
      @newBuffer(0)

      @compositeBuffer
    )

  makeRoomFor: (requiredLength) ->
    if @buffer.length - @position < requiredLength
      @newBuffer(requiredLength)

  newBuffer: (size) ->
    size ||= @sizeIncrement

    buffer = @buffer.slice(0, @position)

    if @compositeBuffer
      @compositeBuffer = Buffer.concat([@compositeBuffer, buffer])
    else
      @compositeBuffer = buffer

    @buffer = new Buffer(size)
    @position = 0

  writeUInt8: (value) ->
    length = 1
    @makeRoomFor(length)
    @buffer.writeUInt8(value, @position)
    @position += length

  writeUInt16LE: (value) ->
    length = 2
    @makeRoomFor(length)
    @buffer.writeUInt16LE(value, @position)
    @position += length

  writeUShort: (value) ->
    @writeUInt16LE(value)

  writeUInt16BE: (value) ->
    length = 2
    @makeRoomFor(length)
    @buffer.writeUInt16BE(value, @position)
    @position += length

  writeUInt32LE: (value) ->
    length = 4
    @makeRoomFor(length)
    @buffer.writeUInt32LE(value, @position)
    @position += length

  writeUInt64LE: (value) ->
    low = value % 0x100000000
    high = Math.floor(value / 0x100000000)
    @writeUInt32LE(low)
    @writeUInt32LE(high)

  writeUInt32BE: (value) ->
    length = 4
    @makeRoomFor(length)
    @buffer.writeUInt32BE(value, @position)
    @position += length

  writeInt8: (value) ->
    length = 1
    @makeRoomFor(length)
    @buffer.writeInt8(value, @position)
    @position += length

  writeInt16LE: (value) ->
    length = 2
    @makeRoomFor(length)
    @buffer.writeInt16LE(value, @position)
    @position += length

  writeInt16BE: (value) ->
    length = 2
    @makeRoomFor(length)
    @buffer.writeInt16BE(value, @position)
    @position += length

  writeInt32LE: (value) ->
    length = 4
    @makeRoomFor(length)
    @buffer.writeInt32LE(value, @position)
    @position += length

  writeInt32BE: (value) ->
    length = 4
    @makeRoomFor(length)
    @buffer.writeInt32BE(value, @position)
    @position += length

  writeFloatLE: (value) ->
    length = 4
    @makeRoomFor(length)
    @buffer.writeFloatLE(value, @position)
    @position += length

  writeDoubleLE: (value) ->
    length = 8
    @makeRoomFor(length)
    @buffer.writeDoubleLE(value, @position)
    @position += length

  writeString: (value, encoding) ->
    encoding ||= @encoding

    length = Buffer.byteLength(value, encoding)
    @makeRoomFor(length)
    bytesWritten = @buffer.write(value, @position, encoding)
    @position += length

    bytesWritten

  writeBVarchar: (value, encoding) ->
    @writeUInt8(value.length)
    @writeString(value, encoding)

  writeUsVarchar: (value, encoding) ->
    @writeUInt16LE(value.length)
    @writeString(value, encoding)

  writeBuffer: (value) ->
    length = value.length
    @makeRoomFor(length)
    value.copy(@buffer, @position)
    @position += length

module.exports = WritableTrackingBuffer
Something went wrong with that request. Please try again.