Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add lines about additonal uses of Buffer #4257

Closed
wants to merge 2 commits into from

4 participants

@trevnorris
Owner

That Buffers can be used with Typed Array Views and DataViews. Included are a couple simple examples.

@trevnorris trevnorris Add lines about additonal uses of Buffer
That Buffers can be used with Typed Array Views and DataViews. Included
are a couple simple examples.
5d81eda
@TooTallNate
Owner

Looks good, I'll merge.

The apparent global var leaks in your example kinda bugs me, mind fixing those first?

@trevnorris trevnorris Fix global var leak
Per TooTallNate's request, variables in doc have now been explicitly
declared.
dfecc7b
@trevnorris
Owner

@TooTallNate There ya go. Thanks for the feedback.

@Mithgol

For a future reference, since which Node version is using Buffers with Typed Array Views and DataViews supported?

(I am considering some changes in jDataView and jParser but I am not sure how backwards-compatible that would be.)

@bnoordhuis

@Mithgol 0.8. The feature was landed in one of the 0.7 dev releases.

@TooTallNate TooTallNate closed this pull request from a commit
@trevnorris trevnorris doc: Add lines about additonal uses of Buffer
That Buffers can be used with Typed Array Views and DataViews. Included
are a couple simple examples.

Closes #4257.
bb867c0
@TooTallNate
Owner

Thanks @trevnorris, merged in bb867c0.

@Mithgol

@bnoordhuis I've just tried the following in Node v0.6.21:

buff = new Buffer(4);
view = new DataView(buff);
view.setInt16(0, 1);
view.setInt16(2, 2, true);
console.log(buff);

I got <Buffer 00 01 02 00>.

So the DataView() of buffers seems to be possible before 0.8 and 0.7.

I wonder how far in the past it goes and how it should be tested.

@Mithgol

Tested in 0.6.0, got the same result.

@bnoordhuis

@Mithgol The feature I mean was landed in 5b05429. It doesn't matter though, v0.6's end of life is in two weeks.

@Mithgol

What was the Node version when the commit 5b05429 happened?

(Currently jDataView supports Node versions of 0.5.5 and above, so it does matter: I'd have to persuade that library's author to drop some support if I write some uncompatible pull request.)

@bnoordhuis

What was the Node version when the commit 5b05429 happened?

git tag --contains 5b05429 :-)

(v0.7.0 is the first one.)

@Mithgol

Wait, I understand now; the feature you meant was getting a typed array (such as Uint16Array()) out of a buffer, however, DataView() of a buffer was possible earlier.

I've just tested the same code in Node v0.5.6:

buff = new Buffer(4);
view = new DataView(buff);
view.setInt16(0, 1);
view.setInt16(2, 2, true);
console.log(buff);

Got the same output (<Buffer 00 01 02 00>).

@Mithgol

Node v0.5.4 says DataView is not defined.

Node v0.5.5 cannot run on Windows XP.

I guess the least of the compatible versions is v0.5.6 (or v0.5.5, but I am unable to test that).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 9, 2012
  1. @trevnorris

    Add lines about additonal uses of Buffer

    trevnorris authored
    That Buffers can be used with Typed Array Views and DataViews. Included
    are a couple simple examples.
Commits on Nov 22, 2012
  1. @trevnorris

    Fix global var leak

    trevnorris authored
    Per TooTallNate's request, variables in doc have now been explicitly
    declared.
This page is out of date. Refresh to see the latest.
Showing with 17 additions and 0 deletions.
  1. +17 −0 doc/api/buffer.markdown
View
17 doc/api/buffer.markdown
@@ -40,6 +40,23 @@ encoding method. Here are the different string encodings.
* `'hex'` - Encode each byte as two hexadecimal characters.
+`Buffer` can also be used with Typed Array Views and DataViews.
+
+ var buff = new Buffer(4);
+ var ui16 = new Uint16Array(buff);
+ var view = new DataView(buff);
+
+ ui16[0] = 1;
+ ui16[1] = 2;
+ console.log(buff);
+
+ view.setInt16(0, 1); // set big-endian int16 at byte offset 0
+ view.setInt16(2, 2, true); // set little-endian int16 at byte offset 2
+ console.log(buff);
+
+ // <Buffer 01 00 02 00>
+ // <Buffer 00 01 02 00>
+
## Class: Buffer
The Buffer class is a global type for dealing with binary data directly.
Something went wrong with that request. Please try again.