Skip to content

Commit ac684f3

Browse files
committed
Add legacy 'binary' encoding/decoding methods to Buffer
1 parent b80f6e9 commit ac684f3

4 files changed

Lines changed: 58 additions & 4 deletions

File tree

lib/http.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,11 @@ function newParser (type) {
8787
case 'ascii':
8888
string = b.asciiSlice(start, start+len);
8989
break;
90+
case 'binary':
91+
string = b.binarySlice(start, start+len);
92+
break;
9093
default:
91-
throw new Error('Unsupported encoding ' + self._encoding + '. Use Buffer');
94+
throw new Error('Unsupported encoding ' + enc + '. Use Buffer');
9295
}
9396
parser.incoming.emit('data', string);
9497
}

lib/net.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,18 +429,23 @@ Stream.prototype._writeString = function (data, encoding) {
429429
}
430430
}
431431

432-
encoding = encoding || 'utf8'; // default to utf8
432+
encoding = (encoding || 'utf8').toLowerCase(); // default to utf8
433433

434434
var charsWritten;
435435
var bytesWritten;
436436

437-
if (encoding.toLowerCase() == 'utf8') {
437+
438+
if (encoding == 'utf8') {
438439
charsWritten = buffer.utf8Write(data, buffer.used);
439440
bytesWritten = Buffer.byteLength(data.slice(0, charsWritten));
440-
} else {
441+
} else if (encoding == 'ascii') {
441442
// ascii
442443
charsWritten = buffer.asciiWrite(data, buffer.used);
443444
bytesWritten = charsWritten;
445+
} else {
446+
// binary
447+
charsWritten = buffer.binaryWrite(data, buffer.used);
448+
bytesWritten = charsWritten;
444449
}
445450

446451
buffer.used += bytesWritten;

src/node_buffer.cc

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,20 @@ Buffer::~Buffer() {
171171
}
172172

173173

174+
Handle<Value> Buffer::BinarySlice(const Arguments &args) {
175+
HandleScope scope;
176+
Buffer *parent = ObjectWrap::Unwrap<Buffer>(args.This());
177+
SLICE_ARGS(args[0], args[1])
178+
179+
const char *data = const_cast<char*>(parent->data_ + start);
180+
//Local<String> string = String::New(data, end - start);
181+
182+
Local<Value> b = Encode(data, end - start, BINARY);
183+
184+
return scope.Close(b);
185+
}
186+
187+
174188
Handle<Value> Buffer::AsciiSlice(const Arguments &args) {
175189
HandleScope scope;
176190
Buffer *parent = ObjectWrap::Unwrap<Buffer>(args.This());
@@ -267,6 +281,34 @@ Handle<Value> Buffer::AsciiWrite(const Arguments &args) {
267281
}
268282

269283

284+
Handle<Value> Buffer::BinaryWrite(const Arguments &args) {
285+
HandleScope scope;
286+
287+
Buffer *buffer = ObjectWrap::Unwrap<Buffer>(args.This());
288+
289+
if (!args[0]->IsString()) {
290+
return ThrowException(Exception::TypeError(String::New(
291+
"Argument must be a string")));
292+
}
293+
294+
Local<String> s = args[0]->ToString();
295+
296+
size_t offset = args[1]->Int32Value();
297+
298+
if (offset >= buffer->length_) {
299+
return ThrowException(Exception::TypeError(String::New(
300+
"Offset is out of bounds")));
301+
}
302+
303+
char *p = (char*)buffer->data_ + offset;
304+
305+
size_t towrite = MIN((unsigned long) s->Length(), buffer->length_ - offset);
306+
307+
int written = DecodeWrite(p, towrite, s, BINARY);
308+
return scope.Close(Integer::New(written));
309+
}
310+
311+
270312
// buffer.unpack(format, index);
271313
// Starting at 'index', unpacks binary from the buffer into an array.
272314
// 'format' is a string
@@ -361,6 +403,7 @@ void Buffer::Initialize(Handle<Object> target) {
361403
constructor_template->SetClassName(String::NewSymbol("Buffer"));
362404

363405
// copy free
406+
NODE_SET_PROTOTYPE_METHOD(constructor_template, "binarySlice", Buffer::BinarySlice);
364407
NODE_SET_PROTOTYPE_METHOD(constructor_template, "asciiSlice", Buffer::AsciiSlice);
365408
NODE_SET_PROTOTYPE_METHOD(constructor_template, "slice", Buffer::Slice);
366409
// TODO NODE_SET_PROTOTYPE_METHOD(t, "utf16Slice", Utf16Slice);
@@ -369,6 +412,7 @@ void Buffer::Initialize(Handle<Object> target) {
369412

370413
NODE_SET_PROTOTYPE_METHOD(constructor_template, "utf8Write", Buffer::Utf8Write);
371414
NODE_SET_PROTOTYPE_METHOD(constructor_template, "asciiWrite", Buffer::AsciiWrite);
415+
NODE_SET_PROTOTYPE_METHOD(constructor_template, "binaryWrite", Buffer::BinaryWrite);
372416
NODE_SET_PROTOTYPE_METHOD(constructor_template, "unpack", Buffer::Unpack);
373417

374418
NODE_SET_METHOD(constructor_template->GetFunction(),

src/node_buffer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ class Buffer : public ObjectWrap {
4545
static v8::Persistent<v8::FunctionTemplate> constructor_template;
4646
static v8::Handle<v8::Value> New(const v8::Arguments &args);
4747
static v8::Handle<v8::Value> Slice(const v8::Arguments &args);
48+
static v8::Handle<v8::Value> BinarySlice(const v8::Arguments &args);
4849
static v8::Handle<v8::Value> AsciiSlice(const v8::Arguments &args);
4950
static v8::Handle<v8::Value> Utf8Slice(const v8::Arguments &args);
51+
static v8::Handle<v8::Value> BinaryWrite(const v8::Arguments &args);
5052
static v8::Handle<v8::Value> AsciiWrite(const v8::Arguments &args);
5153
static v8::Handle<v8::Value> Utf8Write(const v8::Arguments &args);
5254
static v8::Handle<v8::Value> ByteLength(const v8::Arguments &args);

0 commit comments

Comments
 (0)