@@ -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+
174188Handle<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 (),
0 commit comments