Merge pull request ipfs/go-ipfs-http-client#31 from ipfs/feat/read-at

file: implement ReadAt

This commit was moved from ipfs/go-ipfs-http-client@187d8d16e5
This commit is contained in:
Steven Allen 2019-09-12 11:58:25 -07:00 committed by GitHub
commit c71a840bb8

View File

@ -82,6 +82,25 @@ func (f *apiFile) Read(p []byte) (int, error) {
return n, err
}
func (f *apiFile) ReadAt(p []byte, off int64) (int, error) {
// Always make a new request. This method should be parallel-safe.
resp, err := f.core.Request("cat", f.path.String()).
Option("offset", off).Option("length", len(p)).Send(f.ctx)
if err != nil {
return 0, err
}
if resp.Error != nil {
return 0, resp.Error
}
defer resp.Output.Close()
n, err := io.ReadFull(resp.Output, p)
if err == io.ErrUnexpectedEOF {
err = io.EOF
}
return n, err
}
func (f *apiFile) Seek(offset int64, whence int) (int64, error) {
switch whence {
case io.SeekEnd: