Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
buffer: add .read*() and .write*() methods to SlowBuffer prototype
Browse files Browse the repository at this point in the history
Fixes #2138.
  • Loading branch information
bnoordhuis committed Nov 18, 2011
1 parent 06d71ad commit 59a9a9b
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 48 deletions.
29 changes: 29 additions & 0 deletions lib/buffer.js
Expand Up @@ -1119,3 +1119,32 @@ Buffer.prototype.writeDoubleLE = function(value, offset, noAssert) {
Buffer.prototype.writeDoubleBE = function(value, offset, noAssert) {
writeDouble(this, value, offset, true, noAssert);
};

SlowBuffer.prototype.readUInt8 = Buffer.prototype.readUInt8;
SlowBuffer.prototype.readUInt16LE = Buffer.prototype.readUInt16LE;
SlowBuffer.prototype.readUInt16BE = Buffer.prototype.readUInt16BE;
SlowBuffer.prototype.readUInt32LE = Buffer.prototype.readUInt32LE;
SlowBuffer.prototype.readUInt32BE = Buffer.prototype.readUInt32BE;
SlowBuffer.prototype.readInt8 = Buffer.prototype.readInt8;
SlowBuffer.prototype.readInt16LE = Buffer.prototype.readInt16LE;
SlowBuffer.prototype.readInt16BE = Buffer.prototype.readInt16BE;
SlowBuffer.prototype.readInt32LE = Buffer.prototype.readInt32LE;
SlowBuffer.prototype.readInt32BE = Buffer.prototype.readInt32BE;
SlowBuffer.prototype.readFloatLE = Buffer.prototype.readFloatLE;
SlowBuffer.prototype.readFloatBE = Buffer.prototype.readFloatBE;
SlowBuffer.prototype.readDoubleLE = Buffer.prototype.readDoubleLE;
SlowBuffer.prototype.readDoubleBE = Buffer.prototype.readDoubleBE;
SlowBuffer.prototype.writeUInt8 = Buffer.prototype.writeUInt8;
SlowBuffer.prototype.writeUInt16LE = Buffer.prototype.writeUInt16LE;
SlowBuffer.prototype.writeUInt16BE = Buffer.prototype.writeUInt16BE;
SlowBuffer.prototype.writeUInt32LE = Buffer.prototype.writeUInt32LE;
SlowBuffer.prototype.writeUInt32BE = Buffer.prototype.writeUInt32BE;
SlowBuffer.prototype.writeInt8 = Buffer.prototype.writeInt8;
SlowBuffer.prototype.writeInt16LE = Buffer.prototype.writeInt16LE;
SlowBuffer.prototype.writeInt16BE = Buffer.prototype.writeInt16BE;
SlowBuffer.prototype.writeInt32LE = Buffer.prototype.writeInt32LE;
SlowBuffer.prototype.writeInt32BE = Buffer.prototype.writeInt32BE;
SlowBuffer.prototype.writeFloatLE = Buffer.prototype.writeFloatLE;
SlowBuffer.prototype.writeFloatBE = Buffer.prototype.writeFloatBE;
SlowBuffer.prototype.writeDoubleLE = Buffer.prototype.writeDoubleLE;
SlowBuffer.prototype.writeDoubleBE = Buffer.prototype.writeDoubleBE;
8 changes: 5 additions & 3 deletions test/simple/test-readdouble.js
@@ -1,13 +1,14 @@
/*
* Tests to verify we're reading in doubles correctly
*/
var SlowBuffer = process.binding('buffer').SlowBuffer;
var ASSERT = require('assert');

/*
* Test (64 bit) double
*/
function test() {
var buffer = new Buffer(8);
function test(clazz) {
var buffer = new clazz(8);

buffer[0] = 0x55;
buffer[1] = 0x55;
Expand Down Expand Up @@ -104,4 +105,5 @@ function test() {
}


test();
test(Buffer);
test(SlowBuffer);
8 changes: 5 additions & 3 deletions test/simple/test-readfloat.js
@@ -1,13 +1,14 @@
/*
* Tests to verify we're reading in floats correctly
*/
var SlowBuffer = process.binding('buffer').SlowBuffer;
var ASSERT = require('assert');

/*
* Test (32 bit) float
*/
function test() {
var buffer = new Buffer(4);
function test(clazz) {
var buffer = new clazz(4);

buffer[0] = 0;
buffer[1] = 0;
Expand Down Expand Up @@ -66,4 +67,5 @@ function test() {
}


test();
test(Buffer);
test(SlowBuffer);
22 changes: 13 additions & 9 deletions test/simple/test-readint.js
@@ -1,13 +1,14 @@
/*
* Tests to verify we're reading in signed integers correctly
*/
var SlowBuffer = process.binding('buffer').SlowBuffer;
var ASSERT = require('assert');

/*
* Test 8 bit signed integers
*/
function test8() {
var data = new Buffer(4);
function test8(clazz) {
var data = new clazz(4);

data[0] = 0x23;
ASSERT.equal(0x23, data.readInt8(0));
Expand All @@ -26,8 +27,8 @@ function test8() {
}


function test16() {
var buffer = new Buffer(6);
function test16(clazz) {
var buffer = new clazz(6);

buffer[0] = 0x16;
buffer[1] = 0x79;
Expand Down Expand Up @@ -59,8 +60,8 @@ function test16() {
}


function test32() {
var buffer = new Buffer(6);
function test32(clazz) {
var buffer = new clazz(6);

buffer[0] = 0x43;
buffer[1] = 0x53;
Expand Down Expand Up @@ -91,6 +92,9 @@ function test32() {
}


test8();
test16();
test32();
test8(Buffer);
test8(SlowBuffer);
test16(Buffer);
test16(SlowBuffer);
test32(Buffer);
test32(SlowBuffer);
22 changes: 13 additions & 9 deletions test/simple/test-readuint.js
Expand Up @@ -2,6 +2,7 @@
* A battery of tests to help us read a series of uints
*/

var SlowBuffer = process.binding('buffer').SlowBuffer;
var ASSERT = require('assert');

/*
Expand All @@ -11,8 +12,8 @@ var ASSERT = require('assert');
* - Correctly using the offsets
* - Correctly interpreting values that are beyond the signed range as unsigned
*/
function test8() {
var data = new Buffer(4);
function test8(clazz) {
var data = new clazz(4);

data[0] = 23;
data[1] = 23;
Expand All @@ -36,8 +37,8 @@ function test8() {
* - Correctly using the offsets
* - Correctly interpreting values that are beyond the signed range as unsigned
*/
function test16() {
var data = new Buffer(4);
function test16(clazz) {
var data = new clazz(4);

data[0] = 0;
data[1] = 0x23;
Expand All @@ -64,8 +65,8 @@ function test16() {
* - Correctly using the offsets
* - Correctly interpreting values that are beyond the signed range as unsigned
*/
function test32() {
var data = new Buffer(8);
function test32(clazz) {
var data = new clazz(8);

data[0] = 0x32;
data[1] = 0x65;
Expand All @@ -82,6 +83,9 @@ function test32() {
}


test8();
test16();
test32();
test8(Buffer);
test8(SlowBuffer);
test16(Buffer);
test16(SlowBuffer);
test32(Buffer);
test32(SlowBuffer);
8 changes: 5 additions & 3 deletions test/simple/test-writedouble.js
@@ -1,10 +1,11 @@
/*
* Tests to verify we're writing doubles correctly
*/
var SlowBuffer = process.binding('buffer').SlowBuffer;
var ASSERT = require('assert');

function test() {
var buffer = new Buffer(16);
function test(clazz) {
var buffer = new clazz(16);

buffer.writeDoubleBE(2.225073858507201e-308, 0);
buffer.writeDoubleLE(2.225073858507201e-308, 8);
Expand Down Expand Up @@ -103,4 +104,5 @@ function test() {
}


test();
test(Buffer);
test(SlowBuffer);
8 changes: 5 additions & 3 deletions test/simple/test-writefloat.js
@@ -1,10 +1,11 @@
/*
* Tests to verify we're writing floats correctly
*/
var SlowBuffer = process.binding('buffer').SlowBuffer;
var ASSERT = require('assert');

function test() {
var buffer = new Buffer(8);
function test(clazz) {
var buffer = new clazz(8);

buffer.writeFloatBE(1, 0);
buffer.writeFloatLE(1, 4);
Expand Down Expand Up @@ -63,4 +64,5 @@ function test() {
}


test();
test(Buffer);
test(SlowBuffer);
22 changes: 13 additions & 9 deletions test/simple/test-writeint.js
@@ -1,10 +1,11 @@
/*
* Tests to verify we're writing signed integers correctly
*/
var SlowBuffer = process.binding('buffer').SlowBuffer;
var ASSERT = require('assert');

function test8() {
var buffer = new Buffer(2);
function test8(clazz) {
var buffer = new clazz(2);

buffer.writeInt8(0x23, 0);
buffer.writeInt8(-5, 1);
Expand Down Expand Up @@ -35,8 +36,8 @@ function test8() {
}


function test16() {
var buffer = new Buffer(6);
function test16(clazz) {
var buffer = new clazz(6);

buffer.writeInt16BE(0x0023, 0);
buffer.writeInt16LE(0x0023, 2);
Expand Down Expand Up @@ -88,8 +89,8 @@ function test16() {
}


function test32() {
var buffer = new Buffer(8);
function test32(clazz) {
var buffer = new clazz(8);

buffer.writeInt32BE(0x23, 0);
buffer.writeInt32LE(0x23, 4);
Expand Down Expand Up @@ -161,6 +162,9 @@ function test32() {
}


test8();
test16();
test32();
test8(Buffer);
test8(SlowBuffer);
test16(Buffer);
test16(SlowBuffer);
test32(Buffer);
test32(SlowBuffer);
22 changes: 13 additions & 9 deletions test/simple/test-writeuint.js
@@ -1,6 +1,7 @@
/*
* A battery of tests to help us read a series of uints
*/
var SlowBuffer = process.binding('buffer').SlowBuffer;
var ASSERT = require('assert');

/*
Expand All @@ -10,8 +11,8 @@ var ASSERT = require('assert');
* - Correctly using the offsets
* - Correctly interpreting values that are beyond the signed range as unsigned
*/
function test8() {
var data = new Buffer(4);
function test8(clazz) {
var data = new clazz(4);

data.writeUInt8(23, 0);
data.writeUInt8(23, 1);
Expand Down Expand Up @@ -39,9 +40,9 @@ function test8() {
}


function test16() {
function test16(clazz) {
var value = 0x2343;
var data = new Buffer(4);
var data = new clazz(4);

data.writeUInt16BE(value, 0);
ASSERT.equal(0x23, data[0]);
Expand Down Expand Up @@ -78,8 +79,8 @@ function test16() {
}


function test32() {
var data = new Buffer(6);
function test32(clazz) {
var data = new clazz(6);
var value = 0xe7f90a6d;

data.writeUInt32BE(value, 0);
Expand Down Expand Up @@ -120,6 +121,9 @@ function test32() {
}


test8();
test16();
test32();
test8(Buffer);
test8(SlowBuffer);
test16(Buffer);
test16(SlowBuffer);
test32(Buffer);
test32(SlowBuffer);

0 comments on commit 59a9a9b

Please sign in to comment.