From: Ryan Dahl Date: Sat, 20 Mar 2010 03:33:09 +0000 (-0700) Subject: Add legacy 'binary' encoding/decoding methods to Buffer X-Git-Tag: v0.1.92~84^2~6 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ac684f358391400a4533fc81fa7c522090aec884;p=platform%2Fupstream%2Fnodejs.git Add legacy 'binary' encoding/decoding methods to Buffer --- diff --git a/lib/http.js b/lib/http.js index a8d09d3..7ff1f8e 100644 --- a/lib/http.js +++ b/lib/http.js @@ -87,8 +87,11 @@ function newParser (type) { case 'ascii': string = b.asciiSlice(start, start+len); break; + case 'binary': + string = b.binarySlice(start, start+len); + break; default: - throw new Error('Unsupported encoding ' + self._encoding + '. Use Buffer'); + throw new Error('Unsupported encoding ' + enc + '. Use Buffer'); } parser.incoming.emit('data', string); } diff --git a/lib/net.js b/lib/net.js index b963fc7..72618e1 100644 --- a/lib/net.js +++ b/lib/net.js @@ -429,18 +429,23 @@ Stream.prototype._writeString = function (data, encoding) { } } - encoding = encoding || 'utf8'; // default to utf8 + encoding = (encoding || 'utf8').toLowerCase(); // default to utf8 var charsWritten; var bytesWritten; - if (encoding.toLowerCase() == 'utf8') { + + if (encoding == 'utf8') { charsWritten = buffer.utf8Write(data, buffer.used); bytesWritten = Buffer.byteLength(data.slice(0, charsWritten)); - } else { + } else if (encoding == 'ascii') { // ascii charsWritten = buffer.asciiWrite(data, buffer.used); bytesWritten = charsWritten; + } else { + // binary + charsWritten = buffer.binaryWrite(data, buffer.used); + bytesWritten = charsWritten; } buffer.used += bytesWritten; diff --git a/src/node_buffer.cc b/src/node_buffer.cc index b0efba4..c16ead3 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -171,6 +171,20 @@ Buffer::~Buffer() { } +Handle Buffer::BinarySlice(const Arguments &args) { + HandleScope scope; + Buffer *parent = ObjectWrap::Unwrap(args.This()); + SLICE_ARGS(args[0], args[1]) + + const char *data = const_cast(parent->data_ + start); + //Local string = String::New(data, end - start); + + Local b = Encode(data, end - start, BINARY); + + return scope.Close(b); +} + + Handle Buffer::AsciiSlice(const Arguments &args) { HandleScope scope; Buffer *parent = ObjectWrap::Unwrap(args.This()); @@ -267,6 +281,34 @@ Handle Buffer::AsciiWrite(const Arguments &args) { } +Handle Buffer::BinaryWrite(const Arguments &args) { + HandleScope scope; + + Buffer *buffer = ObjectWrap::Unwrap(args.This()); + + if (!args[0]->IsString()) { + return ThrowException(Exception::TypeError(String::New( + "Argument must be a string"))); + } + + Local s = args[0]->ToString(); + + size_t offset = args[1]->Int32Value(); + + if (offset >= buffer->length_) { + return ThrowException(Exception::TypeError(String::New( + "Offset is out of bounds"))); + } + + char *p = (char*)buffer->data_ + offset; + + size_t towrite = MIN((unsigned long) s->Length(), buffer->length_ - offset); + + int written = DecodeWrite(p, towrite, s, BINARY); + return scope.Close(Integer::New(written)); +} + + // buffer.unpack(format, index); // Starting at 'index', unpacks binary from the buffer into an array. // 'format' is a string @@ -361,6 +403,7 @@ void Buffer::Initialize(Handle target) { constructor_template->SetClassName(String::NewSymbol("Buffer")); // copy free + NODE_SET_PROTOTYPE_METHOD(constructor_template, "binarySlice", Buffer::BinarySlice); NODE_SET_PROTOTYPE_METHOD(constructor_template, "asciiSlice", Buffer::AsciiSlice); NODE_SET_PROTOTYPE_METHOD(constructor_template, "slice", Buffer::Slice); // TODO NODE_SET_PROTOTYPE_METHOD(t, "utf16Slice", Utf16Slice); @@ -369,6 +412,7 @@ void Buffer::Initialize(Handle target) { NODE_SET_PROTOTYPE_METHOD(constructor_template, "utf8Write", Buffer::Utf8Write); NODE_SET_PROTOTYPE_METHOD(constructor_template, "asciiWrite", Buffer::AsciiWrite); + NODE_SET_PROTOTYPE_METHOD(constructor_template, "binaryWrite", Buffer::BinaryWrite); NODE_SET_PROTOTYPE_METHOD(constructor_template, "unpack", Buffer::Unpack); NODE_SET_METHOD(constructor_template->GetFunction(), diff --git a/src/node_buffer.h b/src/node_buffer.h index 569943e..9f497c5 100644 --- a/src/node_buffer.h +++ b/src/node_buffer.h @@ -45,8 +45,10 @@ class Buffer : public ObjectWrap { static v8::Persistent constructor_template; static v8::Handle New(const v8::Arguments &args); static v8::Handle Slice(const v8::Arguments &args); + static v8::Handle BinarySlice(const v8::Arguments &args); static v8::Handle AsciiSlice(const v8::Arguments &args); static v8::Handle Utf8Slice(const v8::Arguments &args); + static v8::Handle BinaryWrite(const v8::Arguments &args); static v8::Handle AsciiWrite(const v8::Arguments &args); static v8::Handle Utf8Write(const v8::Arguments &args); static v8::Handle ByteLength(const v8::Arguments &args);