Add legacy 'binary' encoding/decoding methods to Buffer
authorRyan Dahl <ry@tinyclouds.org>
Sat, 20 Mar 2010 03:33:09 +0000 (20:33 -0700)
committerRyan Dahl <ry@tinyclouds.org>
Sat, 20 Mar 2010 03:33:09 +0000 (20:33 -0700)
lib/http.js
lib/net.js
src/node_buffer.cc
src/node_buffer.h

index a8d09d3..7ff1f8e 100644 (file)
@@ -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);
       }
index b963fc7..72618e1 100644 (file)
@@ -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;
index b0efba4..c16ead3 100644 (file)
@@ -171,6 +171,20 @@ Buffer::~Buffer() {
 }
 
 
+Handle<Value> Buffer::BinarySlice(const Arguments &args) {
+  HandleScope scope;
+  Buffer *parent = ObjectWrap::Unwrap<Buffer>(args.This());
+  SLICE_ARGS(args[0], args[1])
+
+  const char *data = const_cast<char*>(parent->data_ + start);
+  //Local<String> string = String::New(data, end - start);
+
+  Local<Value> b =  Encode(data, end - start, BINARY);
+
+  return scope.Close(b);
+}
+
+
 Handle<Value> Buffer::AsciiSlice(const Arguments &args) {
   HandleScope scope;
   Buffer *parent = ObjectWrap::Unwrap<Buffer>(args.This());
@@ -267,6 +281,34 @@ Handle<Value> Buffer::AsciiWrite(const Arguments &args) {
 }
 
 
+Handle<Value> Buffer::BinaryWrite(const Arguments &args) {
+  HandleScope scope;
+
+  Buffer *buffer = ObjectWrap::Unwrap<Buffer>(args.This());
+
+  if (!args[0]->IsString()) {
+    return ThrowException(Exception::TypeError(String::New(
+            "Argument must be a string")));
+  }
+
+  Local<String> 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<Object> 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<Object> 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(),
index 569943e..9f497c5 100644 (file)
@@ -45,8 +45,10 @@ class Buffer : public ObjectWrap {
   static v8::Persistent<v8::FunctionTemplate> constructor_template;
   static v8::Handle<v8::Value> New(const v8::Arguments &args);
   static v8::Handle<v8::Value> Slice(const v8::Arguments &args);
+  static v8::Handle<v8::Value> BinarySlice(const v8::Arguments &args);
   static v8::Handle<v8::Value> AsciiSlice(const v8::Arguments &args);
   static v8::Handle<v8::Value> Utf8Slice(const v8::Arguments &args);
+  static v8::Handle<v8::Value> BinaryWrite(const v8::Arguments &args);
   static v8::Handle<v8::Value> AsciiWrite(const v8::Arguments &args);
   static v8::Handle<v8::Value> Utf8Write(const v8::Arguments &args);
   static v8::Handle<v8::Value> ByteLength(const v8::Arguments &args);