github.com/mem/u-root@v2.0.1-0.20181004165302-9b18b4636a33+incompatible/pkg/uio/reader.go (about) 1 // Copyright 2018 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package uio 6 7 import ( 8 "io" 9 "io/ioutil" 10 "math" 11 ) 12 13 type inMemReaderAt interface { 14 Bytes() []byte 15 } 16 17 // ReadAll reads everything that r contains. 18 // 19 // Callers *must* not modify bytes in the returned byte slice. 20 // 21 // If r is an in-memory representation, ReadAll will attempt to return a 22 // pointer to those bytes directly. 23 func ReadAll(r io.ReaderAt) ([]byte, error) { 24 if imra, ok := r.(inMemReaderAt); ok { 25 return imra.Bytes(), nil 26 } 27 return ioutil.ReadAll(Reader(r)) 28 } 29 30 // Reader generates a Reader from a ReaderAt. 31 func Reader(r io.ReaderAt) io.Reader { 32 return io.NewSectionReader(r, 0, math.MaxInt64) 33 }