binary.md
October 8, 2016 ยท View on GitHub
helpers for binary streams
import * as ez from '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 theendianoption to"little". -
buf = reader.read(_, len)
returns thelennext bytes of the stream.
returns a buffer of lengthlen, except at the end of the stream.
The last chunk of the stream may have less thanlenbytes and afterwards the call returnsundefined.
If thelenparameter is omitted, the call returns the next available chunk of data. -
buf = reader.peek(_, len)
Same asreadbut does not advance the read pointer.
Anotherreadwould read the same data again. -
reader.unread(len)
Unread the lastlenbytes read.
lencannot exceed the size of the last read. -
val = reader.readInt8(_) -
val = reader.readUInt8(_) -
val = reader.readInt16(_) -
val = reader.readUInt16(_) -
val = reader.readInt32(_) -
val = reader.readUInt32(_) -
val = reader.readFloat(_) -
val = reader.readDouble(_)
Specialized readers for numbers. -
val = reader.peekInt8(_) -
val = reader.peekUInt8(_) -
val = reader.peekInt16(_) -
val = reader.peekUInt16(_) -
val = reader.peekInt32(_) -
val = reader.peekUInt32(_) -
val = reader.peekFloat(_) -
val = reader.peekDouble(_)
Specialized peekers for numbers. -
val = reader.unreadInt8() -
val = reader.unreadUInt8() -
val = reader.unreadInt16() -
val = reader.unreadUInt16() -
val = reader.unreadInt32() -
val = reader.unreadUInt32() -
val = reader.unreadFloat() -
val = reader.unreadDouble()
Specialized unreaders 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 theendianoption to"little".
ThebufSizeoption controls the size of the intermediate buffer. -
writer.flush(_)
Flushes the buffer to the wrapped writer. -
writer.write(_, buf)
Writesbuf.
Note: writes are buffered.
Use theflush(_)call if you need to flush before the end of the stream. -
writer.writeInt8(_, val) -
writer.writeUInt8(_, val) -
writer.writeInt16(_, val) -
writer.writeUInt16(_, val) -
writer.writeInt32(_, val) -
writer.writeUInt32(_, val) -
writer.writeFloat(_, val) -
writer.writeDouble(_, val)
Specialized writers for numbers.