}
}
- 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;
}
+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());
}
+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
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);
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(),
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);