|
16 | 16 |
|
17 | 17 | #### Methods
|
18 | 18 |
|
| 19 | +##### copy() |
| 20 | + |
| 21 | +> **copy**(`target`: `Uint8Array`, `targetStart`?: `number`, `sourceStart`?: `number`, `sourceEnd`?: `number`): `number` |
| 22 | +
|
| 23 | +Copies data from a region of `buf` to a region in `target`, even if the `target`memory region overlaps with `buf`. |
| 24 | + |
| 25 | +[`TypedArray.prototype.set()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/set) performs the same operation, and is available |
| 26 | +for all TypedArrays, including `Buffer`s, although it takes |
| 27 | +different function arguments. |
| 28 | + |
| 29 | +```js |
| 30 | +import { Buffer } from 'buffer'; |
| 31 | + |
| 32 | +// Create two `Buffer` instances. |
| 33 | +const buf1 = Buffer.allocUnsafe(26); |
| 34 | +const buf2 = Buffer.allocUnsafe(26).fill('!'); |
| 35 | + |
| 36 | +for (let i = 0; i < 26; i++) { |
| 37 | + // 97 is the decimal ASCII value for 'a'. |
| 38 | + buf1[i] = i + 97; |
| 39 | +} |
| 40 | + |
| 41 | +// Copy `buf1` bytes 16 through 19 into `buf2` starting at byte 8 of `buf2`. |
| 42 | +buf1.copy(buf2, 8, 16, 20); |
| 43 | +// This is equivalent to: |
| 44 | +// buf2.set(buf1.subarray(16, 20), 8); |
| 45 | + |
| 46 | +console.log(buf2.toString('ascii', 0, 25)); |
| 47 | +// Prints: !!!!!!!!qrst!!!!!!!!!!!!! |
| 48 | +``` |
| 49 | + |
| 50 | +```js |
| 51 | +import { Buffer } from 'buffer'; |
| 52 | + |
| 53 | +// Create a `Buffer` and copy data from one region to an overlapping region |
| 54 | +// within the same `Buffer`. |
| 55 | + |
| 56 | +const buf = Buffer.allocUnsafe(26); |
| 57 | + |
| 58 | +for (let i = 0; i < 26; i++) { |
| 59 | + // 97 is the decimal ASCII value for 'a'. |
| 60 | + buf[i] = i + 97; |
| 61 | +} |
| 62 | + |
| 63 | +buf.copy(buf, 0, 4, 10); |
| 64 | + |
| 65 | +console.log(buf.toString()); |
| 66 | +// Prints: efghijghijklmnopqrstuvwxyz |
| 67 | +``` |
| 68 | + |
| 69 | +###### Parameters |
| 70 | + |
| 71 | +| Parameter | Type | Description | |
| 72 | +| ------ | ------ | ------ | |
| 73 | +| `target` | `Uint8Array` | A `Buffer` or Uint8Array to copy into. | |
| 74 | +| `targetStart`? | `number` | The offset within `target` at which to begin writing. | |
| 75 | +| `sourceStart`? | `number` | The offset within `buf` from which to begin copying. | |
| 76 | +| `sourceEnd`? | `number` | The offset within `buf` at which to stop copying (not inclusive). | |
| 77 | + |
| 78 | +###### Returns |
| 79 | + |
| 80 | +`number` |
| 81 | + |
| 82 | +The number of bytes copied. |
| 83 | + |
19 | 84 | ##### subarray()
|
20 | 85 |
|
21 | 86 | > **subarray**(`start`?: `number`, `end`?: `number`): [`Buffer`](buffer.md#buffer)
|
@@ -132,6 +197,297 @@ console.log(buf2.toString('hex'));
|
132 | 197 |
|
133 | 198 | `Uint8Array.toString`
|
134 | 199 |
|
| 200 | +##### writeDoubleBE() |
| 201 | + |
| 202 | +> **writeDoubleBE**(`value`: `number`, `offset`?: `number`): `number` |
| 203 | +
|
| 204 | +Writes `value` to `buf` at the specified `offset` as big-endian. The `value` must be a JavaScript number. Behavior is undefined when `value` is anything |
| 205 | +other than a JavaScript number. |
| 206 | + |
| 207 | +```js |
| 208 | +import { Buffer } from 'buffer'; |
| 209 | + |
| 210 | +const buf = Buffer.allocUnsafe(8); |
| 211 | + |
| 212 | +buf.writeDoubleBE(123.456, 0); |
| 213 | + |
| 214 | +console.log(buf); |
| 215 | +// Prints: <Buffer 40 5e dd 2f 1a 9f be 77> |
| 216 | +``` |
| 217 | + |
| 218 | +###### Parameters |
| 219 | + |
| 220 | +| Parameter | Type | Description | |
| 221 | +| ------ | ------ | ------ | |
| 222 | +| `value` | `number` | Number to be written to `buf`. | |
| 223 | +| `offset`? | `number` | Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 8`. | |
| 224 | + |
| 225 | +###### Returns |
| 226 | + |
| 227 | +`number` |
| 228 | + |
| 229 | +`offset` plus the number of bytes written. |
| 230 | + |
| 231 | +##### writeDoubleLE() |
| 232 | + |
| 233 | +> **writeDoubleLE**(`value`: `number`, `offset`?: `number`): `number` |
| 234 | +
|
| 235 | +Writes `value` to `buf` at the specified `offset` as little-endian. The `value` must be a JavaScript number. Behavior is undefined when `value` is anything |
| 236 | +other than a JavaScript number. |
| 237 | + |
| 238 | +```js |
| 239 | +import { Buffer } from 'buffer'; |
| 240 | + |
| 241 | +const buf = Buffer.allocUnsafe(8); |
| 242 | + |
| 243 | +buf.writeDoubleLE(123.456, 0); |
| 244 | + |
| 245 | +console.log(buf); |
| 246 | +// Prints: <Buffer 77 be 9f 1a 2f dd 5e 40> |
| 247 | +``` |
| 248 | + |
| 249 | +###### Parameters |
| 250 | + |
| 251 | +| Parameter | Type | Description | |
| 252 | +| ------ | ------ | ------ | |
| 253 | +| `value` | `number` | Number to be written to `buf`. | |
| 254 | +| `offset`? | `number` | Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 8`. | |
| 255 | + |
| 256 | +###### Returns |
| 257 | + |
| 258 | +`number` |
| 259 | + |
| 260 | +`offset` plus the number of bytes written. |
| 261 | + |
| 262 | +##### writeFloatBE() |
| 263 | + |
| 264 | +> **writeFloatBE**(`value`: `number`, `offset`?: `number`): `number` |
| 265 | +
|
| 266 | +Writes `value` to `buf` at the specified `offset` as big-endian. Behavior is |
| 267 | +undefined when `value` is anything other than a JavaScript number. |
| 268 | + |
| 269 | +```js |
| 270 | +import { Buffer } from 'buffer'; |
| 271 | + |
| 272 | +const buf = Buffer.allocUnsafe(4); |
| 273 | + |
| 274 | +buf.writeFloatBE(0xcafebabe, 0); |
| 275 | + |
| 276 | +console.log(buf); |
| 277 | +// Prints: <Buffer 4f 4a fe bb> |
| 278 | +``` |
| 279 | + |
| 280 | +###### Parameters |
| 281 | + |
| 282 | +| Parameter | Type | Description | |
| 283 | +| ------ | ------ | ------ | |
| 284 | +| `value` | `number` | Number to be written to `buf`. | |
| 285 | +| `offset`? | `number` | Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 4`. | |
| 286 | + |
| 287 | +###### Returns |
| 288 | + |
| 289 | +`number` |
| 290 | + |
| 291 | +`offset` plus the number of bytes written. |
| 292 | + |
| 293 | +##### writeFloatLE() |
| 294 | + |
| 295 | +> **writeFloatLE**(`value`: `number`, `offset`?: `number`): `number` |
| 296 | +
|
| 297 | +Writes `value` to `buf` at the specified `offset` as little-endian. Behavior is |
| 298 | +undefined when `value` is anything other than a JavaScript number. |
| 299 | + |
| 300 | +```js |
| 301 | +import { Buffer } from 'buffer'; |
| 302 | + |
| 303 | +const buf = Buffer.allocUnsafe(4); |
| 304 | + |
| 305 | +buf.writeFloatLE(0xcafebabe, 0); |
| 306 | + |
| 307 | +console.log(buf); |
| 308 | +// Prints: <Buffer bb fe 4a 4f> |
| 309 | +``` |
| 310 | + |
| 311 | +###### Parameters |
| 312 | + |
| 313 | +| Parameter | Type | Description | |
| 314 | +| ------ | ------ | ------ | |
| 315 | +| `value` | `number` | Number to be written to `buf`. | |
| 316 | +| `offset`? | `number` | Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 4`. | |
| 317 | + |
| 318 | +###### Returns |
| 319 | + |
| 320 | +`number` |
| 321 | + |
| 322 | +`offset` plus the number of bytes written. |
| 323 | + |
| 324 | +##### writeInt16BE() |
| 325 | + |
| 326 | +> **writeInt16BE**(`value`: `number`, `offset`?: `number`): `number` |
| 327 | +
|
| 328 | +Writes `value` to `buf` at the specified `offset` as big-endian. The `value` must be a valid signed 16-bit integer. Behavior is undefined when `value` is |
| 329 | +anything other than a signed 16-bit integer. |
| 330 | + |
| 331 | +The `value` is interpreted and written as a two's complement signed integer. |
| 332 | + |
| 333 | +```js |
| 334 | +import { Buffer } from 'buffer'; |
| 335 | + |
| 336 | +const buf = Buffer.allocUnsafe(2); |
| 337 | + |
| 338 | +buf.writeInt16BE(0x0102, 0); |
| 339 | + |
| 340 | +console.log(buf); |
| 341 | +// Prints: <Buffer 01 02> |
| 342 | +``` |
| 343 | + |
| 344 | +###### Parameters |
| 345 | + |
| 346 | +| Parameter | Type | Description | |
| 347 | +| ------ | ------ | ------ | |
| 348 | +| `value` | `number` | Number to be written to `buf`. | |
| 349 | +| `offset`? | `number` | Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 2`. | |
| 350 | + |
| 351 | +###### Returns |
| 352 | + |
| 353 | +`number` |
| 354 | + |
| 355 | +`offset` plus the number of bytes written. |
| 356 | + |
| 357 | +##### writeInt16LE() |
| 358 | + |
| 359 | +> **writeInt16LE**(`value`: `number`, `offset`?: `number`): `number` |
| 360 | +
|
| 361 | +Writes `value` to `buf` at the specified `offset` as little-endian. The `value` must be a valid signed 16-bit integer. Behavior is undefined when `value` is |
| 362 | +anything other than a signed 16-bit integer. |
| 363 | + |
| 364 | +The `value` is interpreted and written as a two's complement signed integer. |
| 365 | + |
| 366 | +```js |
| 367 | +import { Buffer } from 'buffer'; |
| 368 | + |
| 369 | +const buf = Buffer.allocUnsafe(2); |
| 370 | + |
| 371 | +buf.writeInt16LE(0x0304, 0); |
| 372 | + |
| 373 | +console.log(buf); |
| 374 | +// Prints: <Buffer 04 03> |
| 375 | +``` |
| 376 | + |
| 377 | +###### Parameters |
| 378 | + |
| 379 | +| Parameter | Type | Description | |
| 380 | +| ------ | ------ | ------ | |
| 381 | +| `value` | `number` | Number to be written to `buf`. | |
| 382 | +| `offset`? | `number` | Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 2`. | |
| 383 | + |
| 384 | +###### Returns |
| 385 | + |
| 386 | +`number` |
| 387 | + |
| 388 | +`offset` plus the number of bytes written. |
| 389 | + |
| 390 | +##### writeInt32BE() |
| 391 | + |
| 392 | +> **writeInt32BE**(`value`: `number`, `offset`?: `number`): `number` |
| 393 | +
|
| 394 | +Writes `value` to `buf` at the specified `offset` as big-endian. The `value` must be a valid signed 32-bit integer. Behavior is undefined when `value` is |
| 395 | +anything other than a signed 32-bit integer. |
| 396 | + |
| 397 | +The `value` is interpreted and written as a two's complement signed integer. |
| 398 | + |
| 399 | +```js |
| 400 | +import { Buffer } from 'buffer'; |
| 401 | + |
| 402 | +const buf = Buffer.allocUnsafe(4); |
| 403 | + |
| 404 | +buf.writeInt32BE(0x01020304, 0); |
| 405 | + |
| 406 | +console.log(buf); |
| 407 | +// Prints: <Buffer 01 02 03 04> |
| 408 | +``` |
| 409 | + |
| 410 | +###### Parameters |
| 411 | + |
| 412 | +| Parameter | Type | Description | |
| 413 | +| ------ | ------ | ------ | |
| 414 | +| `value` | `number` | - | |
| 415 | +| `offset`? | `number` | Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 4`. | |
| 416 | + |
| 417 | +###### Returns |
| 418 | + |
| 419 | +`number` |
| 420 | + |
| 421 | +`offset` plus the number of bytes written. |
| 422 | + |
| 423 | +##### writeInt32LE() |
| 424 | + |
| 425 | +> **writeInt32LE**(`value`: `number`, `offset`?: `number`): `number` |
| 426 | +
|
| 427 | +Writes `value` to `buf` at the specified `offset` as little-endian. The `value` must be a valid signed 32-bit integer. Behavior is undefined when `value` is |
| 428 | +anything other than a signed 32-bit integer. |
| 429 | + |
| 430 | +The `value` is interpreted and written as a two's complement signed integer. |
| 431 | + |
| 432 | +```js |
| 433 | +import { Buffer } from 'buffer'; |
| 434 | + |
| 435 | +const buf = Buffer.allocUnsafe(4); |
| 436 | + |
| 437 | +buf.writeInt32LE(0x05060708, 0); |
| 438 | + |
| 439 | +console.log(buf); |
| 440 | +// Prints: <Buffer 08 07 06 05> |
| 441 | +``` |
| 442 | + |
| 443 | +###### Parameters |
| 444 | + |
| 445 | +| Parameter | Type | Description | |
| 446 | +| ------ | ------ | ------ | |
| 447 | +| `value` | `number` | Number to be written to `buf`. | |
| 448 | +| `offset`? | `number` | Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 4`. | |
| 449 | + |
| 450 | +###### Returns |
| 451 | + |
| 452 | +`number` |
| 453 | + |
| 454 | +`offset` plus the number of bytes written. |
| 455 | + |
| 456 | +##### writeInt8() |
| 457 | + |
| 458 | +> **writeInt8**(`value`: `number`, `offset`?: `number`): `number` |
| 459 | +
|
| 460 | +Writes `value` to `buf` at the specified `offset`. `value` must be a valid |
| 461 | +signed 8-bit integer. Behavior is undefined when `value` is anything other than |
| 462 | +a signed 8-bit integer. |
| 463 | + |
| 464 | +`value` is interpreted and written as a two's complement signed integer. |
| 465 | + |
| 466 | +```js |
| 467 | +import { Buffer } from 'buffer'; |
| 468 | + |
| 469 | +const buf = Buffer.allocUnsafe(2); |
| 470 | + |
| 471 | +buf.writeInt8(2, 0); |
| 472 | +buf.writeInt8(-2, 1); |
| 473 | + |
| 474 | +console.log(buf); |
| 475 | +// Prints: <Buffer 02 fe> |
| 476 | +``` |
| 477 | + |
| 478 | +###### Parameters |
| 479 | + |
| 480 | +| Parameter | Type | Description | |
| 481 | +| ------ | ------ | ------ | |
| 482 | +| `value` | `number` | Number to be written to `buf`. | |
| 483 | +| `offset`? | `number` | Number of bytes to skip before starting to write. Must satisfy `0 <= offset <= buf.length - 1`. | |
| 484 | + |
| 485 | +###### Returns |
| 486 | + |
| 487 | +`number` |
| 488 | + |
| 489 | +`offset` plus the number of bytes written. |
| 490 | + |
135 | 491 | ***
|
136 | 492 |
|
137 | 493 | ### BufferConstructor
|
|
0 commit comments