Reading until terminator makes for a bad day #30

Closed
bmeck opened this Issue Oct 29, 2012 · 7 comments

Comments

Projects
None yet
7 participants
Member

bmeck commented Oct 29, 2012

Doing something equivalent to reading a null terminated string is horrifyingly hard to do right now if you want to read after it. Otherwise replays must be used or intermediate streams for buffering etc.

Imagine a structure with a null terminated string followed by a single byte:

Currently (holy crap allocations / problems):

var str = new Buffer();
//
// Must read one at a time so that read(x) works after the null
// without doing screwy replays
//
while (c = rstream.read(1) && c[0] !== 0x00) {
  str = Buffer.concat(str, c);
}
//
// inclusive i guess?
//
str = Buffer.concat(str, new Buffer([0]);
//
// Now read(x) still works right without replays!
//
rstream.read(1);

A nice to have would be:

rstream.readUntil(0x00);
rstream.read(1);

+1 for readUntil().

Contributor

TooTallNate commented Nov 19, 2012

I think a low-level .peek(n) function would be more beneficial. It would act just like the .read(n) function, except it wouldn't remove the Buffer from the stream. This would be the low-level building block that you could implement .readUntil(x) in in a performant way.

Contributor

Raynos commented Nov 19, 2012

+1 for .peek()

Member

bmeck commented Nov 19, 2012

@TooTallNate peek seems fine to me +1

dscape commented Dec 18, 2012

+1

this reminds me more and more of http://jsdb.org/jsdbhelp.html#Stream

Owner

rvagg commented Jan 9, 2014

closing under the assumption that this is stale. If there's still something to discuss then joyent/node would be the place to discuss it now.

@rvagg rvagg closed this Jan 9, 2014

@bmeck bmeck referenced this issue in whatwg/streams Feb 9, 2014

Closed

Pull based streams: ability to read $num items #74

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment