generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Accommodate
index.Generate
taking io.ReadSeeker
Accommodate to the `index.Generate` signature change, done in #136. Insead of implementng a new reader that converts `io.ReaderAt` to `io.ReadSeaker`, make `internalio.OffsetReader` partially implement `Seek`. The "partial" refers to inability to satisfy `Seek` calls with `io.SeekEnd` whence since the point of `internalio.OffsetReader` is that it needs not to know the total size of a file, i.e. cannot know its end. Instead, it panics if `Seek` is called with whence `io.SeekEnd`. None of this matterns much since it is all placed under internal APIs. This commit was moved from ipld/go-car@c0023ed
- Loading branch information
Showing
5 changed files
with
103 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package io | ||
|
||
import "io" | ||
|
||
var ( | ||
_ io.ReaderAt = (*OffsetReadSeeker)(nil) | ||
_ io.ReadSeeker = (*OffsetReadSeeker)(nil) | ||
) | ||
|
||
// OffsetReadSeeker implements Read, and ReadAt on a section | ||
// of an underlying io.ReaderAt. | ||
// The main difference between io.SectionReader and OffsetReadSeeker is that | ||
// NewOffsetReadSeeker does not require the user to know the number of readable bytes. | ||
// | ||
// It also partially implements Seek, where the implementation panics if io.SeekEnd is passed. | ||
// This is because, OffsetReadSeeker does not know the end of the file therefore cannot seek relative | ||
// to it. | ||
type OffsetReadSeeker struct { | ||
r io.ReaderAt | ||
base int64 | ||
off int64 | ||
} | ||
|
||
// NewOffsetReadSeeker returns an OffsetReadSeeker that reads from r | ||
// starting offset offset off and stops with io.EOF when r reaches its end. | ||
// The Seek function will panic if whence io.SeekEnd is passed. | ||
func NewOffsetReadSeeker(r io.ReaderAt, off int64) *OffsetReadSeeker { | ||
return &OffsetReadSeeker{r, off, off} | ||
} | ||
|
||
func (o *OffsetReadSeeker) Read(p []byte) (n int, err error) { | ||
n, err = o.r.ReadAt(p, o.off) | ||
o.off += int64(n) | ||
return | ||
} | ||
|
||
func (o *OffsetReadSeeker) ReadAt(p []byte, off int64) (n int, err error) { | ||
if off < 0 { | ||
return 0, io.EOF | ||
} | ||
off += o.base | ||
return o.r.ReadAt(p, off) | ||
} | ||
|
||
func (o *OffsetReadSeeker) ReadByte() (byte, error) { | ||
b := []byte{0} | ||
_, err := o.Read(b) | ||
return b[0], err | ||
} | ||
|
||
func (o *OffsetReadSeeker) Offset() int64 { | ||
return o.off | ||
} | ||
|
||
func (o *OffsetReadSeeker) Seek(offset int64, whence int) (int64, error) { | ||
switch whence { | ||
case io.SeekStart: | ||
o.off = offset | ||
case io.SeekCurrent: | ||
o.off += offset | ||
case io.SeekEnd: | ||
panic("unsupported whence: SeekEnd") | ||
} | ||
return o.off, nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters