Deno: want: add file.readAt() and file.writeAt()

Created on 15 Dec 2019  路  7Comments  路  Source: denoland/deno

Offset operations can currently be performed with seek, but some concise APIs are missing

These APIs are more semantic and easier to understand

Golang also provides a similar API

import (
    "fmt"
    "os"
)

func main() {
    f, _ := os.OpenFile("1.go", os.O_RDWR, os.ModePerm)
    f.WriteAt([]byte("widuu"), 10) // write at offset = 10
    b := make([]byte, 20)
    d, _ := f.ReadAt(b, 10)    // read from offset = 10
    fmt.Println(string(b[:d])) //widuudhellowordhello
}

file.readAt(bytes: Uint8Array, offset: number)

const file = Deno.open("./file.txt")

const bytes = new Uint8Array(10)

file.readAt(bytes, 20); // Read 10 bytes of data with an offset of 20

mock api

async function readAt(bytes: Uint8Array, offset: number) {
  await this.seek(offset, 0)
  await this.read(b)
  await this.seek(0, 0) // reset offset
  return b
}

file.writeAt(bytes: Uint8Array, offset: number)

same as above

Why

Should have the API to quickly read/write some data from file

Or maybe extend the Deno.read(b: Uint8Array) API to Deno.read(b: Uint8Array, offset?: number) will be better?

cli public API suggestion

Most helpful comment

@caspervonb @fvkramer i have something started but i'm also having a kid like with in 2 weeks.... so if you get this done just toss it out there.

All 7 comments

Worth noting glibc posix implements offset-based pread, pwrite simply as seek then write, so IMO we don't really need to extend Deno cli API on Deno.read for it.

NVM, adding to Deno.read as an optional argument looks good to me (taking advantage of atomicity of pread/pwrite)

Am in need of this for implementing pread/pwrite in https://deno.land/x/wasi.

Adding an optional offset is the way to go IMHO.

This issue has been open for a while - I'll work on it.

I'm jumping on this one.

Is anyone actively working on this?

Hey, I'm not. I assumed the person who posted after me was taking it. It doesn't look like anyone is working on it

@caspervonb @fvkramer i have something started but i'm also having a kid like with in 2 weeks.... so if you get this done just toss it out there.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zugende picture zugende  路  3Comments

justjavac picture justjavac  路  3Comments

CruxCv picture CruxCv  路  3Comments

davidbarratt picture davidbarratt  路  3Comments

ry picture ry  路  3Comments