Adaptor from ([]byte) to (io.ReaderAt)?

536 views
Skip to first unread message

Mickey Killianey

unread,
Dec 6, 2010, 4:00:00 AM12/6/10
to golang-nuts
I'm looking for a way to get an array of bytes to satisfy the
io.ReaderAt interface, and I can't find a utility wrapper in bytes,
io, or io/ioutil...am I just looking in the wrong spot? Is there a
canonical way to do this?

I know, I know, it's not that many lines of code, but with len(p)
being an int and off being an int64 in the io.ReaderAt's signature...

ReadAt(p []byte, off int64) (n int, err os.Error)

...I'd rather not hack up my own and worry about whether or not I've
checked all the edge cases correctly.

FWIW, I'm doing this because I have bytes that *aren't* coming from a
file that I want to pass to zip.NewReader().

Mick

peterGo

unread,
Dec 6, 2010, 2:26:18 PM12/6/10
to golang-nuts, peterGo
Mick,

Try this patch. If it works, I'll submit a CL.

Peter

readat.diff

Rob 'Commander' Pike

unread,
Dec 6, 2010, 2:36:20 PM12/6/10
to peterGo, golang-nuts
That ReadAt has different semantics from Read, in that Read consumes
data but ReadAt just presents it. That bothers me and goes to the
heart of why byte buffers don't implement Seek. I believe that
without Seek, there is no ReadAt.

-rob

Reply all
Reply to author
Forward
0 new messages