Permalink
Browse files

more buffer

  • Loading branch information...
1 parent 054e479 commit 1b52ad8f71753bed756438c6e36ed2ef40b2b26f @tj tj committed Aug 15, 2010
Showing with 19 additions and 2 deletions.
  1. +14 −1 chapters/buffers.md
  2. +5 −1 src/buffer/examples.js
@@ -49,4 +49,17 @@ The `.length` property of a buffer instance contains the byte length of the stre
console.log(ellipsis);
// => <Buffer e2 80 a6>
-When working with JavaScript strings, we can also pass a string through the `Buffer.byteLength()` method to determine it's byte length.
+When working with JavaScript strings, we can also pass a string through the `Buffer.byteLength()` method to determine it's byte length.
+
+The api is written in such a way that it is Array-like, so for example we can work with "slices" of a `Buffer` by passing offsets to the `slice()` method:
+
+ var chunk = buf.slice(4, 9);
+ console.log(chunk.toString());
+ // => "some"
+
+Alternatively when expecting a string we can pass offsets to `Buffer#toString()`:
+
+ var buf = new Buffer('just some data');
+ console.log(buf.toString('ascii', 4, 9));
+ // => "some"
+
View
@@ -23,4 +23,8 @@ buf.write('l', 2);
buf.write('lo', 3);
console.log(buf.toString());
-console.log(new Buffer('', 'ascii').toString());
+console.log(new Buffer('', 'ascii').toString());
+
+var buf = new Buffer('just some data');
+console.log(buf.slice(4, 9).toString());
+console.log(buf.toString('ascii', 4, 9));

0 comments on commit 1b52ad8

Please sign in to comment.