| 613 | } |
| 614 | |
| 615 | #flushBufferSync() { |
| 616 | if (this.#destroyed) { |
| 617 | throw new ERR_INVALID_STATE('Utf8Stream is destroyed'); |
| 618 | } |
| 619 | |
| 620 | if (this.#fd < 0) { |
| 621 | throw new ERR_INVALID_STATE('Invalid file descriptor'); |
| 622 | } |
| 623 | |
| 624 | if (!this.#writing && this.#writingBuf.length > 0) { |
| 625 | this.#bufs.unshift([this.#writingBuf]); |
| 626 | this.#writingBuf = kEmptyBuffer; |
| 627 | } |
| 628 | |
| 629 | let buf = kEmptyBuffer; |
| 630 | while (this.#bufs.length || buf.length) { |
| 631 | if (buf.length <= 0) { |
| 632 | buf = mergeBuf(this.#bufs[0], this.#lens[0]); |
| 633 | } |
| 634 | try { |
| 635 | const n = this.#fs.writeSync(this.#fd, buf); |
| 636 | buf = buf.subarray(n); |
| 637 | this.#len = MathMax(this.#len - n, 0); |
| 638 | if (buf.length <= 0) { |
| 639 | this.#bufs.shift(); |
| 640 | this.#lens.shift(); |
| 641 | } |
| 642 | } catch (err) { |
| 643 | const shouldRetry = err.code === 'EAGAIN' || err.code === 'EBUSY'; |
| 644 | if (shouldRetry && !this.#retryEAGAIN(err, buf.length, this.#len - buf.length)) { |
| 645 | throw err; |
| 646 | } |
| 647 | |
| 648 | sleep(BUSY_WRITE_TIMEOUT); |
| 649 | } |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | #flushSyncUtf8() { |
| 654 | if (this.#destroyed) { |