Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

more examples, readme stuff

  • Loading branch information...
commit de624253e4764bdb6b196eeb0e20189633182eeb 1 parent 369acb1
James Halliday authored
24 README
View
@@ -1,24 +0,0 @@
-Hackneyed Example:
- var sys = require('sys');
- var Buffer = require('buffer').Buffer;
- var BufferList = require('bufferlist').BufferList;
-
- var buf1 = new Buffer(5); buf1.write('abcde');
- var buf2 = new Buffer(3); buf2.write('xyz');
- var buf3 = new Buffer(5); buf3.write('11358');
-
- var b = new BufferList;
- b.push(buf1,buf2,buf3);
-
- sys.puts(b.take(10)); // abcdexyz11
- sys.puts(b.take(3)); // abc
- sys.puts(b.take(100)); // abcdexyz11358
-
-Buffer objects don't need to be buffer objects necessarily, but they should
-implement methods that act like Buffer's toString, length, and copy.
-
-To use a different kind of buffer object, set the constructor property:
- var b = new BufferList({ construct : Bluffer });
-
- var b = new BufferList;
- b.construct = Buffur;
59 README.markdown
View
@@ -0,0 +1,59 @@
+BufferList
+==========
+BufferList provides an interface to treat a linked list of buffers as a single
+stream. This is useful for events that produce a many small Buffers, such as
+network streams.
+
+Simple Bufferlist Example
+=========================
+
+ #!/usr/bin/env node
+ var sys = require('sys');
+ var Buffer = require('buffer').Buffer;
+ var BufferList = require('bufferlist').BufferList;
+
+ var b = new BufferList;
+ ['abcde','xyz','11358'].forEach(function (s) {
+ var buf = new Buffer(s.length);
+ buf.write(s);
+ b.push(buf);
+ });
+
+ sys.puts(b.take(10)); // abcdexyz11
+
+Binary
+======
+This distribution also contains a Binary module for parsing these bufferlists.
+
+Simple Binary Example
+=====================
+
+ #!/usr/bin/env node
+
+ var sys = require('sys');
+ var Buffer = require('buffer').Buffer;
+ var BufferList = require('bufferlist').BufferList;
+ var Binary = require('bufferlist/binary').Binary;
+
+ var bufferList = new BufferList;
+ var binary = Binary(bufferList)
+ .getWord16be('xLen')
+ .getBuffer('xs', 'xLen')
+ .tap(function (vars) {
+ vars.moo = 'xs:' + vars.xLen + ':' + vars.xs;
+ })
+ .end()
+ ;
+
+ var buf = new Buffer(6);
+ buf.write('\x00\x04meow', 'binary');
+ bufferList.push(buf);
+
+ sys.puts(binary.vars.moo); // xs:4:meow
+
+See Also
+========
+See the examples/ directory for more involved examples.
+
+See http://github.com/substack/node-rfb for a practical application of this
+distribution.
22 examples/simple_binary.js
View
@@ -0,0 +1,22 @@
+#!/usr/bin/env node
+
+var sys = require('sys');
+var Buffer = require('buffer').Buffer;
+var BufferList = require('bufferlist').BufferList;
+var Binary = require('bufferlist/binary').Binary;
+
+var bufferList = new BufferList;
+var binary = Binary(bufferList)
+ .getWord16be('xLen')
+ .getBuffer('xs', 'xLen')
+ .tap(function (vars) {
+ vars.moo = 'xs:' + vars.xLen + ':' + vars.xs;
+ })
+ .end()
+;
+
+var buf = new Buffer(6);
+buf.write('\x00\x04meow', 'binary');
+bufferList.push(buf);
+
+sys.puts(binary.vars.moo); // xs:4:meow
13 examples/simple_bufferlist.js
View
@@ -0,0 +1,13 @@
+#!/usr/bin/env node
+var sys = require('sys');
+var Buffer = require('buffer').Buffer;
+var BufferList = require('bufferlist').BufferList;
+
+var b = new BufferList;
+['abcde','xyz','11358'].forEach(function (s) {
+ var buf = new Buffer(s.length);
+ buf.write(s);
+ b.push(buf);
+});
+
+sys.puts(b.take(10)); // abcdexyz11
Please sign in to comment.
Something went wrong with that request. Please try again.