# buffer

***

**1. Create a Buffer from a String:**

```js
const buf = Buffer.from('Hello, world!');
```

**2. Create a Buffer from an Array of Numbers:**

```js
const buf = Buffer.from([1, 2, 3, 4, 5]);
```

**3. Create a Buffer from an Array of Strings:**

```js
const buf = Buffer.from(['a', 'b', 'c', 'd', 'e']);
```

**4. Create a Buffer from a File:**

```js
const fs = require('fs');
const buf = Buffer.from(fs.readFileSync('file.txt'));
```

**5. Convert a Buffer to a String:**

```js
const str = buf.toString();
```

**6. Convert a Buffer to an Array of Numbers:**

```js
const arr = buf.toJSON().data;
```

**7. Convert a Buffer to an Array of Strings:**

```js
const arr = buf.toString().split('');
```

**8. Concatenate Buffers:**

```js
const buf1 = Buffer.from('Hello, ');
const buf2 = Buffer.from('world!');
const buf3 = Buffer.concat([buf1, buf2]);
```

**9. Slice a Buffer:**

```js
const buf = Buffer.from('Hello, world!');
const slicedBuf = buf.slice(0, 5); // 'Hello'
```

**10. Copy a Buffer:**

```js
const buf1 = Buffer.from('Hello, world!');
const buf2 = Buffer.alloc(buf1.length);
buf1.copy(buf2);
```

**11. Compare Buffers:**

```js
const buf1 = Buffer.from('Hello, world!');
const buf2 = Buffer.from('Hello, world!');
const result = buf1.compare(buf2); // 0 (equal)
```

**12. Write to a Buffer:**

```js
const buf = Buffer.alloc(10);
buf.write('Hello, world!', 0, 12); // 'Hello, world!' written at offset 0
```

**13. Read from a Buffer:**

```js
const buf = Buffer.from('Hello, world!');
const str = buf.toString('utf8', 0, 5); // 'Hello'
```

**14. Inspect a Buffer:**

```js
console.log(buf.inspect());
```

**15. Fill a Buffer with a Value:**

```js
const buf = Buffer.alloc(10);
buf.fill(0); // Fill with zeros
```

**16. Convert a Buffer to a Base64 String:**

```js
const base64Str = buf.toString('base64');
```

**17. Convert a Base64 String to a Buffer:**

```js
const buf = Buffer.from(base64Str, 'base64');
```

**18. Create a Buffer from a Stream:**

```js
const stream = fs.createReadStream('file.txt');
const buf = Buffer.from(stream);
```

**19. Write a Buffer to a Stream:**

```js
const stream = fs.createWriteStream('file.txt');
buf.copy(stream);
```

**20. Send a Buffer over a TCP Socket:**

```js
const net = require('net');
const socket = net.connect(port, host);
socket.write(buf);
```

**21. Receive a Buffer from a TCP Socket:**

```js
const net = require('net');
const socket = net.connect(port, host);
socket.on('data', (buf) => {
  // Handle received buffer
});
```

**22. Write a Buffer to a File:**

```js
const fs = require('fs');
fs.writeFileSync('file.txt', buf);
```

**23. Read a Buffer from a File:**

```js
const fs = require('fs');
const buf = fs.readFileSync('file.txt');
```

**24. Encrypt a Buffer with AES-256:**

```js
const crypto = require('crypto');
const cipher = crypto.createCipher('aes-256-cbc', 'mypassword');
const encryptedBuf = cipher.update(buf);
```

**25. Decrypt a Buffer with AES-256:**

```js
const crypto = require('crypto');
const decipher = crypto.createDecipher('aes-256-cbc', 'mypassword');
const decryptedBuf = decipher.update(encryptedBuf);
```

**26. Hash a Buffer with SHA-256:**

```js
const crypto = require('crypto');
const hash = crypto.createHash('sha256');
hash.update(buf);
const hashedBuf = hash.digest();
```

**27. Sign a Buffer with RSA:**

```js
const crypto = require('crypto');
const sign = crypto.createSign('RSA-SHA256');
sign.update(buf);
const signature = sign.sign('myprivateKey');
```

**28. Verify a Buffer with RSA:**

```js
const crypto = require('crypto');
const verify = crypto.createVerify('RSA-SHA256');
verify.update(buf);
const verified = verify.verify('mypublicKey', signature);
```

**29. Generate a Random Buffer:**

```js
const crypto = require('crypto');
const randomBuf = crypto.randomBytes(10);
```

**30. Shuffle a Buffer:**

```js
const crypto = require('crypto');
crypto.randomFill(buf, (err, buf) => { /* Shuffled buffer */ });
```

**31. Zero a Buffer:**

```js
buf.fill(0, 0, buf.length);
```

**32. Create a Buffer Pool:**

```js
const { BufferPool } = require('bufferpool');
const pool = new BufferPool();
const buf = pool.alloc(10);
```

**33. Use a Buffer Pool with a Stream:**

```js
const { PassThrough, BufferPool } = require('stream');
const pool = new BufferPool();
const passThroughStream = new PassThrough({ objectMode: true });
passThroughStream
  .on('data', (data, enc) => { pool.release(data); })
  .on('end', () => { pool.drain(); });
```

**34. Use a Buffer Pool with a Middleware:**

```js
const { BufferPool, Middleware } = require('express-bufferpool');
const pool = new BufferPool();
const middleware = new Middleware(pool);
app.use(middleware);
```

**35. Use a Buffer Pool with Request/Response:**

```js
const { BufferPool } = require('koa-bufferpool');
const pool = new BufferPool();
const app = new Koa();
app.use(async (ctx, next) => {
  ctx.set('bufferpool', pool);
  await next();
});
```

**36. Use a Buffer Pool with TCP Server:**

```js
const { BufferPool, Server } = require('net-bufferpool');
const pool = new BufferPool();
const server = new Server(pool);
server.listen(port);
```
