...

Source file src/oras.land/oras-go/pkg/content/readerat.go

Documentation: oras.land/oras-go/pkg/content

     1  /*
     2  Copyright The ORAS Authors.
     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 content
    16  
    17  import (
    18  	"io"
    19  
    20  	"github.com/containerd/containerd/content"
    21  )
    22  
    23  // ensure interface
    24  var (
    25  	_ content.ReaderAt = sizeReaderAt{}
    26  )
    27  
    28  type readAtCloser interface {
    29  	io.ReaderAt
    30  	io.Closer
    31  }
    32  
    33  type sizeReaderAt struct {
    34  	readAtCloser
    35  	size int64
    36  }
    37  
    38  func (ra sizeReaderAt) Size() int64 {
    39  	return ra.size
    40  }
    41  
    42  func NopCloserAt(r io.ReaderAt) nopCloserAt {
    43  	return nopCloserAt{r}
    44  }
    45  
    46  type nopCloserAt struct {
    47  	io.ReaderAt
    48  }
    49  
    50  func (n nopCloserAt) Close() error {
    51  	return nil
    52  }
    53  
    54  // readerAtWrapper wraps a ReaderAt to give a Reader
    55  type ReaderAtWrapper struct {
    56  	offset   int64
    57  	readerAt io.ReaderAt
    58  }
    59  
    60  func (r *ReaderAtWrapper) Read(p []byte) (n int, err error) {
    61  	n, err = r.readerAt.ReadAt(p, r.offset)
    62  	r.offset += int64(n)
    63  	return
    64  }
    65  
    66  func NewReaderAtWrapper(readerAt io.ReaderAt) *ReaderAtWrapper {
    67  	return &ReaderAtWrapper{readerAt: readerAt}
    68  }
    69  

View as plain text