Buffer subclass with some smack-your-head-obvious methods (push, pop, growToAccommodate, clone, dataLength, etc.)
Want to see pretty graphs? Log in now!
npm install better-buffer
|2||downloads in the last month|
|Last Published By|
|Version||0.0.2 last updated 2 years ago|
part of me wanted to call this module
please god let someone get the joke. preferably a girl. preferably gorgeous.
It blows my mind that these methods don't exist on the core node.js
object. srsly guise?
BetterBuffer(initialSize, growSize = 512)
Constructor is compatible with existing
Buffer constructor, but also adds an
growSize parameter. The only time that a
BetterBuffer will grow
is when you call the
growToAccommodate() method -- there's no auto-growing on
Buffer::copy() -- at least not yet.
Ensures that the buffer is at least big enough to store
octets. The buffer will grow in intervals defind by its
growSize member, so
you may sometimes end up with a buffer that's slightly larger than necessary.
Queue-like functionality for buffers. Appends
sourceBuffer to the buffer at
the end of that buffer's actual data, as indicated by its
dataLength member. So
for example, if you have:
var myBuffer = new BetterBuffer(10, 5); // myBuffer.length == 10 // myBuffer.dataLength == 0
...and you append, say,
length of which is
myBuffer.pushBack(secondBuffer); // myBuffer.length == 10 // myBuffer.dataLength == 6
myBuffer.dataLength is now 6. Now let's append
thirdBuffer, the length of
myBuffer.pushBack(thirdBuffer); // myBuffer.length == 10 // myBuffer.dataLength == 8
myBuffer.dataLength is now 8, and
thirdBuffer was written into
starting at position 6.
In combination with
BetterBuffer::popFront(), this makes it much easier to
create queue- or stream-like behavior using simple buffer objects.
dataLength property is incremented accordingly.
Just a simple
pop function, like you'd find on a stack or queue. Hacks off the
num octets in the buffer, shifts everything in the buffer forward by the
same distance, and returns the popped octets in a new
dataLength property is decremented accordingly.
Creates a new
BetterBuffer containing an exact copy of the contents of the
buffer. NOTE: the returned buffer's size is equal to the source buffer's
dataLength property, not its
For example, let's say you have a buffer
myBuffer of size
256, and you've only
pushBacked one buffer of size
100 into it. This means that
100. So when you call
cloneDataIntoNewBuffer, it will return a buffer of size
tl;dr (for the hyperactive and/or distractible)
var myBuf = new BetterBuffer(10, 5); myBuf.fill(0x00); // myBuf.length == 10 // myBuf.dataLength == 0 // myBuf = <Buffer 00 00 00 00 00 00 00 00 00 00> var smallerBuffer = new BetterBuffer(8); smallerBuffer.fill(0xff); // smallerBuffer.length == 8 // smallerBuffer.dataLength == 0 // smallerBuffer = <Buffer ff ff ff ff ff ff ff ff> // now we explicitly set smallerBuffer.dataLength -- it's not necessary, but // it makes things clearer conceptually smallerBuffer.dataLength = smallerBuffer.length; // what happens when we call growToAccommodate to ensure we have enough room? myBuf.growToAccommodate(smallerBuffer.dataLength); // smallerBuffer is smaller than myBuf, so myBuf.length is still 10 // go ahead and eat the smaller buffer myBuf.pushBack(smallerBuffer); // myBuf = <Buffer ff ff ff ff ff ff ff ff 00 00> // myBuf.length == 10 // myBuf.dataLength == 8 var anotherBuffer = new BetterBuffer(8); anotherBuffer.fill(0xaa); // anotherBuffer.length == 8 // anotherBuffer.dataLength == 0 // anotherBuffer = <Buffer aa aa aa aa aa aa aa aa> // again, we explicitly set anotherBuffer.dataLength for the sake of code readability anotherBuffer.dataLength = anotherBuffer.length; // let's try calling growToAccommodate again and see if anything happens this time myBuf.growToAccommodate(myBuf.dataLength + anotherBuffer.dataLength); // now, myBuf.length == 20 because: // myBuf.dataLength == 8 // anotherBuffer.dataLength == 8 // and myBuf.growSize == 5 (myBuf's initial size was 10, and 10 + 5 + 5 == 20) // now we can pushBack() safely myBuf.pushBack(anotherBuffer); // myBuf = <Buffer ff ff ff ff ff ff ff ff aa aa aa aa aa aa aa aa 00 00 00 00> // myBuf.length == 20 // myBuf.dataLength == 16 // and just for good measure let's tack on a shitty example of popFront() var justAPiece = myBuf.popFront(5); // justAPiece = <Buffer ff ff ff ff ff> // myBuf = <Buffer ff ff ff aa aa aa aa aa aa aa aa 00 00 00 00>
Authors and contributors
bryn austin bellomy < email@example.com >
License (MIT license)
Copyright (c) 2012 bryn austin bellomy, signalenvelope / signals.io »
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.