github.com/coreos/mantle@v0.13.0/lang/reader/reader.go (about)

     1  // Copyright 2016 CoreOS, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package reader
    16  
    17  import (
    18  	"io"
    19  )
    20  
    21  // AtReader converts an io.ReaderAt into an io.Reader
    22  func AtReader(ra io.ReaderAt) io.Reader {
    23  	if rd, ok := ra.(io.Reader); ok {
    24  		return rd
    25  	}
    26  	return &atReader{ReaderAt: ra}
    27  }
    28  
    29  type atReader struct {
    30  	io.ReaderAt
    31  	off int64
    32  }
    33  
    34  func (r *atReader) Read(p []byte) (n int, err error) {
    35  	n, err = r.ReadAt(p, r.off)
    36  	r.off += int64(n)
    37  	return
    38  }