From aa5c4bac1cf32bec285a5867efe2feb6eced5fc8 Mon Sep 17 00:00:00 2001 From: sidorares Date: Wed, 20 Jul 2011 14:45:46 +1000 Subject: [PATCH] allow Buffer to be used as 'a' format pack parameter --- lib/x11/unpackstream.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/x11/unpackstream.js b/lib/x11/unpackstream.js index ed7e5be..1e963b6 100644 --- a/lib/x11/unpackstream.js +++ b/lib/x11/unpackstream.js @@ -244,6 +244,7 @@ UnpackStream.prototype.pack = function(format, arguments) var n = arguments[arg++]; buf[offset++] = n; break; + case 's': // TODO: implement signed INT16!!! case 'S': var n = arguments[arg++]; buf[offset++] = n & 0xff; @@ -256,11 +257,17 @@ UnpackStream.prototype.pack = function(format, arguments) buf[offset++] = (n >> 16) & 0xff; buf[offset++] = (n >> 24) & 0xff; break; - case 'a': // string + case 'a': // string or buffer var str = arguments[arg++]; - // TODO: buffer.write could be faster - for (var c = 0; c < str.length; ++c) - buf[offset++] = str.charCodeAt(c); + if (Buffer.isBuffer(str)) + { + str.copy(buf, offset); + offset += str.length; + } else { + // TODO: buffer.write could be faster + for (var c = 0; c < str.length; ++c) + buf[offset++] = str.charCodeAt(c); + } break; case 'p': // padded string var str = arguments[arg++];