| 232 | } |
| 233 | |
| 234 | private readChunk(method: string, buffer: Uint8Array, explicit: bigint | undefined) { |
| 235 | return Effect.suspend(() => { |
| 236 | if (explicit !== undefined && explicit < BigInt(0)) { |
| 237 | return Effect.fail(PlatformError.badArgument({ |
| 238 | module: "FileSystem", |
| 239 | method, |
| 240 | description: "Cannot read before the start of the file" |
| 241 | })) |
| 242 | } |
| 243 | const position = explicit ?? this.position |
| 244 | return Effect.map( |
| 245 | tryPromise( |
| 246 | method, |
| 247 | undefined, |
| 248 | (signal) => |
| 249 | this.native(signal, async () => { |
| 250 | if (this.nativePosition !== position) { |
| 251 | this.file.seekSync(position, Deno.SeekMode.Start) |
| 252 | } |
| 253 | this.nativePosition = undefined |
| 254 | const bytesRead = (await this.file.read(buffer)) ?? 0 |
| 255 | this.nativePosition = position + BigInt(bytesRead) |
| 256 | return bytesRead |
| 257 | }) |
| 258 | ), |
| 259 | (bytesRead) => { |
| 260 | if (explicit === undefined) { |
| 261 | this.position = position + BigInt(bytesRead) |
| 262 | } |
| 263 | return bytesRead |
| 264 | } |
| 265 | ) |
| 266 | }) |
| 267 | } |
| 268 | |
| 269 | read(buffer: Uint8Array, options?: FileSystem.File.ReadOptions) { |
| 270 | return this.readChunk("read", buffer, options?.position) |