Skip to content

Commit 23aca2f

Browse files
committed
buffer: create buffer.copy doc
1 parent fa63474 commit 23aca2f

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

doc/api/buffer.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,97 @@ socket.on('readable', () => {
919919

920920
A `TypeError` will be thrown if `size` is not a number.
921921

922+
### Static method: `Buffer.copy(source, target, targetStart[, sourceStart[, sourceEnd]])`
923+
924+
<!-- YAML
925+
added: REPLACEME
926+
-->
927+
928+
* `source` {Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer} The source
929+
to copy data from.
930+
* `target` {Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer} The target
931+
to copy data to.
932+
* `targetStart` {integer} The offset within `target` at which to begin writing.
933+
**Default:** `0`.
934+
* `sourceStart` {integer} The offset within `source` from which to begin copying.
935+
**Default:** `0`.
936+
* `sourceEnd` {integer} The offset within `source` at which to stop copying
937+
(exclusive). **Default:** `source.byteLength`.
938+
* Returns: {integer} The number of bytes copied.
939+
940+
Copies data from `source` to `target`. This is a method that can copy data between different types of binary data structures, including `Buffer`, `TypedArray`, `DataView`, `ArrayBuffer`, and `SharedArrayBuffer` instances.
941+
942+
```mjs
943+
import { Buffer } from 'node:buffer';
944+
945+
const src = Buffer.from([1, 2, 3, 4]);
946+
const dst = Buffer.alloc(4);
947+
948+
const bytesCopied = Buffer.copy(src, dst, 0);
949+
console.log(bytesCopied); // 4
950+
console.log(dst); // <Buffer 01 02 03 04>
951+
```
952+
953+
```cjs
954+
const { Buffer } = require('node:buffer');
955+
956+
const src = Buffer.from([1, 2, 3, 4]);
957+
const dst = Buffer.alloc(4);
958+
959+
const bytesCopied = Buffer.copy(src, dst, 0);
960+
console.log(bytesCopied); // 4
961+
console.log(dst); // <Buffer 01 02 03 04>
962+
```
963+
964+
The method can also copy between different types:
965+
966+
```mjs
967+
import { Buffer } from 'node:buffer';
968+
969+
// Copy from ArrayBuffer to Buffer
970+
const ab = new Uint8Array([5, 6, 7, 8]).buffer;
971+
const buf = Buffer.alloc(4);
972+
973+
Buffer.copy(ab, buf, 0);
974+
console.log(buf); // <Buffer 05 06 07 08>
975+
976+
// Copy from Buffer to DataView
977+
const src = Buffer.from([1, 2, 3]);
978+
const targetAB = new ArrayBuffer(5);
979+
const dv = new DataView(targetAB);
980+
981+
Buffer.copy(src, dv, 2);
982+
console.log(new Uint8Array(targetAB)); // Uint8Array(5) [ 0, 0, 1, 2, 3 ]
983+
```
984+
985+
```cjs
986+
const { Buffer } = require('node:buffer');
987+
988+
// Copy from ArrayBuffer to Buffer
989+
const ab = new Uint8Array([5, 6, 7, 8]).buffer;
990+
const buf = Buffer.alloc(4);
991+
992+
Buffer.copy(ab, buf, 0);
993+
console.log(buf); // <Buffer 05 06 07 08>
994+
995+
// Copy from Buffer to DataView
996+
const src = Buffer.from([1, 2, 3]);
997+
const targetAB = new ArrayBuffer(5);
998+
const dv = new DataView(targetAB);
999+
1000+
Buffer.copy(src, dv, 2);
1001+
console.log(new Uint8Array(targetAB)); // Uint8Array(5) [ 0, 0, 1, 2, 3 ]
1002+
```
1003+
1004+
If `targetStart` is negative or beyond the length of `target`, a [`RangeError`][]
1005+
is thrown. If `sourceStart` is negative or beyond the length of `source`, a
1006+
[`RangeError`][] is thrown. If `sourceEnd` is negative, a [`RangeError`][] is
1007+
thrown. Values that exceed the respective buffer lengths are clamped to the
1008+
appropriate limits.
1009+
1010+
If `sourceStart` is greater than or equal to `sourceEnd`, zero bytes are copied
1011+
and the method returns `0`.
1012+
9221013
### Static method: `Buffer.byteLength(string[, encoding])`
9231014

9241015
<!-- YAML

0 commit comments

Comments
 (0)