* Class: Buffer * ============= * * The Buffer constructor returns instances of `Uint8Array` that are augmented * with function properties for all the node `Buffer` API functions. We use * `Uint8Array` so that square bracket notation works as expected -- it returns * a single octet. * * By a
(subject, encoding)
| 503 | * prototype. |
| 504 | */ |
| 505 | function Buffer (subject, encoding) { |
| 506 | var self = this |
| 507 | if (!(self instanceof Buffer)) return new Buffer(subject, encoding) |
| 508 | |
| 509 | var type = typeof subject |
| 510 | var length |
| 511 | |
| 512 | if (type === 'number') { |
| 513 | length = +subject |
| 514 | } else if (type === 'string') { |
| 515 | length = Buffer.byteLength(subject, encoding) |
| 516 | } else if (type === 'object' && subject !== null) { |
| 517 | // assume object is array-like |
| 518 | if (subject.type === 'Buffer' && isArray(subject.data)) subject = subject.data |
| 519 | length = +subject.length |
| 520 | } else { |
| 521 | throw new TypeError('must start with number, buffer, array or string') |
| 522 | } |
| 523 | |
| 524 | if (length > kMaxLength) { |
| 525 | throw new RangeError('Attempt to allocate Buffer larger than maximum size: 0x' + |
| 526 | kMaxLength.toString(16) + ' bytes') |
| 527 | } |
| 528 | |
| 529 | if (length < 0) length = 0 |
| 530 | else length >>>= 0 // coerce to uint32 |
| 531 | |
| 532 | if (Buffer.TYPED_ARRAY_SUPPORT) { |
| 533 | // Preferred: Return an augmented `Uint8Array` instance for best performance |
| 534 | self = Buffer._augment(new Uint8Array(length)) // eslint-disable-line consistent-this |
| 535 | } else { |
| 536 | // Fallback: Return THIS instance of Buffer (created by `new`) |
| 537 | self.length = length |
| 538 | self._isBuffer = true |
| 539 | } |
| 540 | |
| 541 | var i |
| 542 | if (Buffer.TYPED_ARRAY_SUPPORT && typeof subject.byteLength === 'number') { |
| 543 | // Speed optimization -- use set if we're copying from a typed array |
| 544 | self._set(subject) |
| 545 | } else if (isArrayish(subject)) { |
| 546 | // Treat array-ish objects as a byte array |
| 547 | if (Buffer.isBuffer(subject)) { |
| 548 | for (i = 0; i < length; i++) { |
| 549 | self[i] = subject.readUInt8(i) |
| 550 | } |
| 551 | } else { |
| 552 | for (i = 0; i < length; i++) { |
| 553 | self[i] = ((subject[i] % 256) + 256) % 256 |
| 554 | } |
| 555 | } |
| 556 | } else if (type === 'string') { |
| 557 | self.write(subject, 0, encoding) |
| 558 | } else if (type === 'number' && !Buffer.TYPED_ARRAY_SUPPORT) { |
| 559 | for (i = 0; i < length; i++) { |
| 560 | self[i] = 0 |
| 561 | } |
| 562 | } |
no test coverage detected