Efficiently read part of a file by seek offsets
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 38/100
Research direction
No repository file or test is named. Start by locating the existing file-reading entry point, then compare its behavior with the proposed java.nio SeekableByteChannel approach and clarify whether start and end refer to characters or bytes. Done means the requested file slice is read without loading the entire file.
Written by the indexing model from the issue text.
Description
It would be nice to read a slice of a file, based on the start & end char indices.
chatgpt attempt, seems to work:
Yes, there is a more efficient way to handle large files by reading only the necessary parts of the file instead of slurping the entire content. You can accomplish this using Java's
java.nio.fileAPI. Here is a way to do this using interop:
(defn lazy-substring-of-file [filename start end]
(let [path (java.nio.file.Paths/get filename (into-array String []))
options (into-array java.nio.file.OpenOption [java.nio.file.StandardOpenOption/READ])
fc (.newByteChannel java.nio.file.Files path options)
bb (java.nio.ByteBuffer/allocate (- end start))]
(.position fc start)
(.read fc bb)
(.close fc)
(String. (.array bb) "UTF-8")))
In this function:
java.nio.file.Paths/getis used to get ajava.nio.file.Pathobject from the filename.java.nio.file.Files/newByteChannelis used to create a newjava.nio.channels.SeekableByteChannelto the file.java.nio.ByteBuffer/allocateis used to create aByteBufferof the right size..positionis used to set the read position of the byte channel..readis used to read the right amount of bytes from the file into theByteBuffer.String. (.array bb) "UTF-8"is used to create a new string from theByteBuffer.This function avoids reading the whole file into memory by only reading the necessary bytes. It works best when
startandendare relatively small compared to the size of the file.
- Dominant language
- Clojure
- Stars
- 221
- Forks
- 52
- PR merge metrics
- No merged PRs in 30d
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from babashka/fs
-
Difficulty 2/5 1-3 hours Newbie friendliness 48/100
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
-
Difficulty 5/5 Over a week Newbie friendliness 30/100
-
`copy-tree`, when following links, does not copy contents of symbolicly linked sub-dirPossibly taken @shivampwm2020 claimed this 51 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 35/100
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
nextjournal/clerk#830 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
IOP tokens release blocker team qa team qa autotests WG adoption
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
penpot/penpot#13540 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
izhangzhihao/intellij-rainbow-brackets#2903 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 67/100