Skip to content

Commit

Permalink
issue #5 - cosmetic edits to API doc
Browse files Browse the repository at this point in the history
  • Loading branch information
bjouhier committed Sep 18, 2014
1 parent 20232e4 commit 85d8d58
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
16 changes: 14 additions & 2 deletions lib/helpers/binary._js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
/// ## helpers for binary streams
///
/// `var ez = require("ez-streams")`
///

var NUMBERS = [//
['Int8', 1], ['UInt8', 1], //
['Int16', 2], ['UInt16', 2], //
['Int32', 4], ['UInt32', 4], //
['Float', 4], ['Double', 8]];

///
/// ###
///
/// * `reader = ez.helpers.binary.reader(reader, options)`
/// Wraps a raw buffer reader and returns a reader with additional API to handle binary streams.
/// By default the reader is configured as big endian.
Expand Down Expand Up @@ -42,6 +44,7 @@ Reader.prototype.ensure = function(_, len) {
return Math.min(this.buf.length, len);
}

///
/// * `buf = reader.read(_, len)`
/// returns the `len` next bytes of the stream.
/// returns a buffer of length `len`, except at the end of the stream.
Expand All @@ -65,13 +68,15 @@ Reader.prototype.read = function(_, len, peekOnly) {
return result;
}

///
/// * `buf = reader.peek(_, len)`
/// Same as `read` but does not advance the read pointer.
/// Another `read` would read the same data again.
Reader.prototype.peek = function(_, len) {
return this.read(_, len, true);
}

///
/// * reader.unread(len)`
/// Unread the last `len` bytes read.
/// `len` cannot exceed the size of the last read.
Expand All @@ -80,6 +85,7 @@ Reader.prototype.unread = function(len) {
this.pos -= len;
}

///
/// * `val = reader.readInt8(_)`
/// * `val = reader.readUInt8(_)`
/// * `val = reader.readInt16(_)`
Expand Down Expand Up @@ -110,10 +116,13 @@ function numberReader(name, len, peekOnly) {
};
}

///
/// ###
///
/// * `writer = ez.helpers.binary.writer(writer, options)`
/// Wraps a raw buffer writer and returns a writer with additional API to handle binary streams.
/// By default the writer is configured as big endian.
/// You can configure it as little endian by setting the `endian` option to `"little"`.
/// You can configure it as little endian by setting the `endian` option to `"little"`.
/// The `bufSize` option controls the size of the intermediate buffer.
function Writer(writer, options) {
this.writer = writer;
Expand All @@ -122,6 +131,7 @@ function Writer(writer, options) {
this.buf = new Buffer(options.bufSize > 0 ? options.bufSize : 16384);
}

///
/// * `writer.flush(_)`
/// Flushes the buffer to the wrapped writer.
Writer.prototype.flush = function(_) {
Expand All @@ -137,6 +147,7 @@ Writer.prototype.ensure = function(_, len) {
}
}

///
/// * `writer.write(_, buf)`
/// Writes `buf`.
/// Note: writes are buffered.
Expand All @@ -152,6 +163,7 @@ Writer.prototype.write = function(_, buf) {
}
}

///
/// * `writer.writeInt8(_, val)`
/// * `writer.writeUInt8(_, val)`
/// * `writer.writeInt16(_, val)`
Expand Down
14 changes: 13 additions & 1 deletion lib/helpers/binary.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,28 @@

`var ez = require("ez-streams")`

###

* `reader = ez.helpers.binary.reader(reader, options)`
Wraps a raw buffer reader and returns a reader with additional API to handle binary streams.
By default the reader is configured as big endian.
You can configure it as little endian by setting the `endian` option to `"little"`.

* `buf = reader.read(_, len)`
returns the `len` next bytes of the stream.
returns a buffer of length `len`, except at the end of the stream.
The last chunk of the stream may have less than `len` bytes and afterwards the call
returns `undefined`.
If the `len` parameter is omitted, the call returns the next available chunk of data.

* `buf = reader.peek(_, len)`
Same as `read` but does not advance the read pointer.
Another `read` would read the same data again.

* reader.unread(len)`
Unread the last `len` bytes read.
`len` cannot exceed the size of the last read.

* `val = reader.readInt8(_)`
* `val = reader.readUInt8(_)`
* `val = reader.readInt16(_)`
Expand All @@ -37,17 +43,23 @@
* `val = reader.peekFloat(_)`
* `val = reader.peekDouble(_)`
Specialized peekers for numbers.

###

* `writer = ez.helpers.binary.writer(writer, options)`
Wraps a raw buffer writer and returns a writer with additional API to handle binary streams.
By default the writer is configured as big endian.
You can configure it as little endian by setting the `endian` option to `"little"`.
You can configure it as little endian by setting the `endian` option to `"little"`.
The `bufSize` option controls the size of the intermediate buffer.

* `writer.flush(_)`
Flushes the buffer to the wrapped writer.

* `writer.write(_, buf)`
Writes `buf`.
Note: writes are buffered.
Use the `flush(_)` call if you need to flush before the end of the stream.

* `writer.writeInt8(_, val)`
* `writer.writeUInt8(_, val)`
* `writer.writeInt16(_, val)`
Expand Down

0 comments on commit 85d8d58

Please sign in to comment.