Permalink
Browse files

Use Node's Buffer.concat if available (v0.8.x and later) or provide o…

…ur own implementation.
  • Loading branch information...
1 parent ed0d73e commit 828617dfa3408fec29359906a138649492123a52 @pekim pekim committed Jul 8, 2012
@@ -1,23 +1,20 @@
+if !Buffer.concat
+ Buffer.concat = (buffers) ->
+ length = 0
+ for buffer in buffers
+ length += buffer.length
-Buffer.prototype.concat = () ->
- length = @length
+ result = new Buffer(length);
- for buffer in arguments
- length += buffer.length
+ position = 0
+ for buffer in buffers
+ buffer.copy(result, position, 0)
+ position += buffer.length
- result = new Buffer(length);
-
- @copy(result, 0, 0)
- position = @length
-
- for buffer in arguments
- buffer.copy(result, position, 0)
- position += buffer.length
-
- result
+ result
Buffer.prototype.toByteArray = () ->
- Array.prototype.slice.call(@, 0)
+ Array.prototype.slice.call(@, 0)
Buffer.prototype.equals = (other) ->
if @.length != other.length
@@ -302,7 +302,7 @@ class Connection extends EventEmitter
@messageBuffer = new Buffer(0)
addToMessageBuffer: (data) ->
- @messageBuffer = @messageBuffer.concat(data)
+ @messageBuffer = Buffer.concat([@messageBuffer, data])
processPreLoginResponse: ->
preloginPayload = new PreloginPayload(@messageBuffer)
@@ -171,7 +171,8 @@ class Login7Payload
@addVariableDataString(variableData, @changePassword) # Introduced in TDS 7.2
variableData.offsetsAndLengths.writeUInt32LE(@sspiLong) # Introduced in TDS 7.2
- variableData.offsetsAndLengths.data.concat(variableData.data.data)
+ variableData.offsetsAndLengths.data =
+ Buffer.concat([variableData.offsetsAndLengths.data, variableData.data.data])
addVariableDataBuffer: (variableData, buffer) ->
variableData.offsetsAndLengths.writeUInt16LE(variableData.offset)
@@ -15,7 +15,7 @@ class MessageIO extends EventEmitter
eventData: (data) =>
if (@packetBuffer.length > 0)
- @packetBuffer = new Buffer(@packetBuffer.concat(data))
+ @packetBuffer = Buffer.concat([@packetBuffer, data])
else
@packetBuffer = data
View
@@ -68,7 +68,7 @@ class Packet
status &= 0xFF - STATUS.EOM
@buffer.writeUInt8(status, OFFSET.Status)
-
+
@isLast()
isLast: ->
@@ -81,7 +81,7 @@ class Packet
@buffer.readUInt8(OFFSET.PacketID)
addData: (data) ->
- @buffer = new Buffer(@buffer.concat(data))
+ @buffer = Buffer.concat([@buffer, data])
@setLength()
@
@@ -96,7 +96,7 @@ class Packet
statuses = for name, value of STATUS
if status & value
name
- statuses.join(' ').trim()
+ statuses.join(' ').trim()
headerToString: (indent) ->
indent ||= ''
@@ -13,10 +13,10 @@ class ReadableTrackingBuffer
@encoding = undefined
@encoding ||= 'utf8'
- @position = 0
+ @position = 0
add: (buffer) ->
- @buffer = @buffer.slice(@position).concat(buffer)
+ @buffer = Buffer.concat([@buffer.slice(@position), buffer])
@position = 0
assertEnoughLeftFor: (lengthRequired) ->
@@ -25,11 +25,11 @@ class WritableTrackingBuffer
newBuffer: (size) ->
size ||= @sizeIncrement
-
+
buffer = @buffer.slice(0, @position)
-
+
if @compositeBuffer
- @compositeBuffer = @compositeBuffer.concat(buffer)
+ @compositeBuffer = Buffer.concat([@compositeBuffer, buffer])
else
@compositeBuffer = buffer
@@ -4,9 +4,9 @@ exports.concatOneArgument = (test) ->
buffer1 = new Buffer([1, 2])
buffer2 = new Buffer([3, 4])
- buffer12 = buffer1.concat(buffer2)
+ buffer12 = Buffer.concat([buffer1, buffer2])
- test.ok(bufferEqual(buffer12, [1, 2, 3, 4]))
+ test.deepEqual(buffer12, new Buffer([1, 2, 3, 4]))
test.done()
@@ -15,9 +15,9 @@ exports.concatTwoArguments = (test) ->
buffer2 = new Buffer([3, 4])
buffer3 = new Buffer([5, 6])
- buffer123 = buffer1.concat(buffer2, buffer3)
+ buffer123 = Buffer.concat([buffer1, buffer2, buffer3])
- test.ok(bufferEqual(buffer123, [1, 2, 3, 4, 5, 6]))
+ test.deepEqual(buffer123, new Buffer([1, 2, 3, 4, 5, 6]))
test.done()
@@ -1,5 +1,6 @@
Debug = require('../../src/debug')
EventEmitter = require('events').EventEmitter
+require('../../src/buffertools')
MessageIO = require('../../src/message-io')
Packet = require('../../src/packet').Packet
require('../../src/buffertools')
@@ -176,7 +177,7 @@ exports.receiveTwoPacketsWithChunkSpanningPackets = (test) ->
packet2.addData(payload.slice(2, 4))
connection.emit('data', packet1.buffer.slice(0, 6))
- connection.emit('data', packet1.buffer.slice(6).concat(packet2.buffer.slice(0, 4)))
+ connection.emit('data', Buffer.concat([packet1.buffer.slice(6), packet2.buffer.slice(0, 4)]))
connection.emit('data', packet2.buffer.slice(4))
exports.receiveMultiplePacketsWithMoreThanOnePacketFromOneChunk = (test) ->
@@ -192,7 +193,7 @@ exports.receiveMultiplePacketsWithMoreThanOnePacketFromOneChunk = (test) ->
io = new MessageIO(connection, packetSize, new Debug())
io.on('data', (data) ->
- receivedData = receivedData.concat(data)
+ receivedData = Buffer.concat([receivedData, data])
)
io.on('message', ->
@@ -210,7 +211,7 @@ exports.receiveMultiplePacketsWithMoreThanOnePacketFromOneChunk = (test) ->
packet3.last(true)
packet3.addData(payload.slice(4, 6))
- allData = new Buffer(packet1.buffer.concat(packet2.buffer, packet3.buffer))
+ allData = Buffer.concat([packet1.buffer, packet2.buffer, packet3.buffer])
data1 = allData.slice(0, 5)
data2 = allData.slice(5)
@@ -35,7 +35,7 @@ exports.packetId = (test) ->
exports.data = (test) ->
data1 = new Buffer([0x01, 0x02, 0x03])
data2 = new Buffer([0xFF, 0xFE])
- allData = data1.concat(data2)
+ allData = Buffer.concat([data1, data2])
packet = new Packet(TYPE.PRELOGIN)
test.strictEqual(packet.length(), 8)

0 comments on commit 828617d

Please sign in to comment.