Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open large binary file in kaitai struct for javascript? #8

Open
Meigyoku-Thmn opened this issue May 22, 2018 · 4 comments
Open

Open large binary file in kaitai struct for javascript? #8

Meigyoku-Thmn opened this issue May 22, 2018 · 4 comments

Comments

@Meigyoku-Thmn
Copy link

The current javascript implementation requires reading a whole file into memory (ArrayBuffer). This is not good if I want to read a large file, for example a very big zip file. Can you add an option to use file descriptor (returned by fs.openSync) instead of ArrayBuffer?

@GreyCat
Copy link
Member

GreyCat commented May 22, 2018

That's actually a viable idea, which would hopefully require only adjustments to runtime. Any JavaScript volunteers?

@cherue
Copy link
Contributor

cherue commented Mar 15, 2020

This is even possible in browsers using <input type="file">:

const file = input.files[0]
const slice = file.slice(offset, offset + size)

slice.arrayBuffer().then(
    arrayBuffer => {
        const data = new Uint8Array(arrayBuffer)
    }
)

@GreyCat
Copy link
Member

GreyCat commented Mar 15, 2020

Wow, that's even better :)

I wonder what the overhead of using slice() + arrayBuffer() + new Uint8Array for every single read will be significant vs one ArrayBuffer + Uint8Array for whole file?

@cherue
Copy link
Contributor

cherue commented Mar 15, 2020

Yeah that'll probably be an issue, especially for small files.

Maybe something like Python's from_bytes and from_file could work, if it all goes through the KaitaiStream constructor it wouldn't even need compiler changes.

  • ArrayBuffer would be the current implementation
  • Blob (File's ancestor) would be reading from a file in the browser
  • string (path) and number (file descriptor) would read from a file in Node

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants