(bytes)
| 58 | } |
| 59 | |
| 60 | read(bytes) { |
| 61 | if (bytes > this.length) { // <1> |
| 62 | bytes = this.length; |
| 63 | } |
| 64 | if (bytes === 0) { |
| 65 | return new Uint8Array(0); |
| 66 | } |
| 67 | let readData; |
| 68 | if ( |
| 69 | this.head > this.tail || this.buffer.length - this.tail >= bytes // <2> |
| 70 | ) { |
| 71 | // The data is in a contiguous chunk. |
| 72 | readData = this.buffer.slice(this.tail, bytes) |
| 73 | this.tail += bytes; |
| 74 | } else { // <3> |
| 75 | // Read from the end and the beginning. |
| 76 | readData = new Uint8Array(bytes); |
| 77 | const endBytesToRead = this.buffer.length - this.tail; |
| 78 | readData.set(this.buffer.subarray(this.tail, this.buffer.length)); |
| 79 | readData.set(this.buffer.subarray(0, bytes - endBytesToRead), endBytesToRead); |
| 80 | this.tail = bytes - endBytesToRead; |
| 81 | } |
| 82 | this.length -= bytes; |
| 83 | return readData; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | const Mutex = require('../ch6-mutex/mutex.js'); |
no outgoing calls
no test coverage detected