Set arbitrary position on new Reader type

How can I read an arbitrary number of bytes on an arbitrary position with the new std.Io.Reader type?

I’m trying to parse a file format but it uses a lot of byte-wise offsets :frowning:

Well, std.Io.Reader is specifically made for streaming, so seeking is not directly possible, but you can discard a certain number of bytes. If you need to read from a specific location of a file, create a new reader, discardAll(offset) and then read from that position.

Any other alternatives for this? Other than just taking the File object, of course?

For parsing a file like that, I’d use File directly. Io.Reader is really for steaming.

You may be looking for std.Io.Reader.fixed().
When the main reader encounters a byte-offset and length, as long as you hold the file’s data slice in memory, you can construct a new reader that only reads a slice of the file at the given byte-offset with the given length.

while the interface doesnt support seeking, only advancing the stream.

The File.Reader implementation does support seeking, see seekTo and seekBy.

That being said, if you are able to just use the interface you should, as it makes your code much more portable.

Perhaps in the future there might be Seekable versions of the interface.

3 Likes

Yeah, I kind of really didn’t want to depend exclusively on the File.Reader implementation because I abstract over a lot of different ways of reading files, I might need to create my own thing, then…? Is there no abstraction on top of that, which I could use?

nope, but I expect one to be added eventually, can’t say when though. Probably after Andrew is satisfied with the state of the existing reader/writer.

1 Like

thank you for the insight! I think i’ll just use the file reader for now, to hell with it lol

i consider this closed, for now.