# buffer-utils

> Stream and buffer utils.

Latest version **1.1.0** (published 2017-09-02) · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install buffer-utils
pnpm add buffer-utils
yarn add buffer-utils
bun add buffer-utils
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2017-09-02 |
| First published | 2015-02-08 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 4.0.0 |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Lucas Tan |
| Maintainers | lucastan |
| Keywords | buffer, data reader, data writer, buffer streams |

## Links

- npm: https://www.npmjs.com/package/buffer-utils
- Repository: https://github.com/lucastan/node-buffer-utils
- Homepage: https://github.com/lucastan/node-buffer-utils#readme
- Issues: https://github.com/lucastan/node-buffer-utils/issues
- npm.io page: https://npm.io/package/buffer-utils

## Dependencies (1)

- [stream-buffers](https://npm.io/package/stream-buffers.md) 1.1.0

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M weekly downloads
- [jwt-simple](https://npm.io/package/jwt-simple.md) — 259.5K weekly downloads
- [@exodus/patch-broken-hermes-typed-arrays](https://npm.io/package/@exodus/patch-broken-hermes-typed-arrays.md) — 28.5K weekly downloads
- [@native-to-anchor/buffer-layout](https://npm.io/package/@native-to-anchor/buffer-layout.md) — 12.2K weekly downloads
- [binary-parser-encoder](https://npm.io/package/binary-parser-encoder.md) — 5.3K weekly downloads

## Recent versions

- 1.1.0 (latest) — 2017-09-02
- 1.0.1 — 2015-02-11
- 1.0.0 — 2015-02-08

## README

[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coveralls Status][coveralls-image]][coveralls-url] [![Known Vulnerabilities](https://snyk.io/test/github/lucastan/node-buffer-utils/badge.svg)](https://snyk.io/test/github/lucastan/node-buffer-utils) [![Downloads][downloads-image]][npm-url]

Buffer utils that allow you to read and write bytes, strings, ints and floats sequentially.

BufferWriter
============
```js
var BufferWriter = require('buffer-utils').BufferWriter;

var bw = new BufferWriter();

// Writes sequentially.
bw.writeInt8(0x12)
  .writeInt16LE(0x1234); // chaining allowed.

// Writes a buffer
bw.writeBytes(someBuffer);

// Writes a string
bw.writeString("hello world", "utf8");

// Gets the number of bytes written.
var bytesWritten = bw.size();

// Gets the contents and resets to empty.
var contents = bw.getContents();

// now bw.size() === 0
```

BufferReader
============
```js
var BufferReader = require('buffer-utils').BufferReader;

// Wrapper over the buffer that keeps track of the current offset.
var br = new BufferReader(contents);

// Read integers sequentially.
// Supports all readInt*, readUInt*, readFloat*, readDouble* from Buffer.
var i8 = br.readInt8();     // 0x12
var i16 = br.readInt16LE(); // 0x1234

// Reads a chunk of bytes.
// Modifying `buf` will modify `contents` since it is a `slice` of
// `contents`.
var buf = br.readBytes();

// Reads a string
var str = br.readString('utf8'); // hello world

// Gets the number of bytes left.
var bytesLeft = br.bytesLeft(); // 0
```

RetainedBuffer
==============
This is useful for reading a stream of data that is non-seekable and you do
not want to store the entire stream in memory (e.g., when the data is huge).
This util always retains the last X number of bytes read as you feed data into
it, useful for reading variable-sized data followed by a fix-sized
footer structure.

```js
var retainedBuf = new RetainedBuffer(16); // retain 16 bytes

file.on('data', function(incomingChunk){
    // `chunks` is an array of Buffer's. Could be empty if all bytes read are
    // retained.
    var chunks = retainedBuf.read(incomingChunk);

    chunks.forEach(function(chunk){
        // do something with `chunk`...
        // the individual chunks will ultimately piece up to the
        // data read, minus the retained last 16 bytes.
    });
})
.on('end', function(){
    // `retained` is a buffer of 16 bytes if we have read at least 16 bytes
    // so far.
    var retained = retainedBuf.getRetained();
});
```

[downloads-image]: http://img.shields.io/npm/dm/buffer-utils.svg

[npm-url]: https://npmjs.org/package/buffer-utils
[npm-image]: http://img.shields.io/npm/v/buffer-utils.svg

[travis-url]: https://travis-ci.org/lucastan/node-buffer-utils
[travis-image]: http://img.shields.io/travis/lucastan/node-buffer-utils.svg

[coveralls-url]: https://coveralls.io/r/lucastan/node-buffer-utils
[coveralls-image]: http://img.shields.io/coveralls/lucastan/node-buffer-utils/master.svg

---
_Source: https://npm.io/package/buffer-utils · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
