github.com/shaardie/u-root@v4.0.1-0.20190127173353-f24a1c26aa2e+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 "bytes" 9 "io" 10 "io/ioutil" 11 "math" 12 ) 13 14 type inMemReaderAt interface { 15 Bytes() []byte 16 } 17 18 // ReadAll reads everything that r contains. 19 // 20 // Callers *must* not modify bytes in the returned byte slice. 21 // 22 // If r is an in-memory representation, ReadAll will attempt to return a 23 // pointer to those bytes directly. 24 func ReadAll(r io.ReaderAt) ([]byte, error) { 25 if imra, ok := r.(inMemReaderAt); ok { 26 return imra.Bytes(), nil 27 } 28 return ioutil.ReadAll(Reader(r)) 29 } 30 31 // Reader generates a Reader from a ReaderAt. 32 func Reader(r io.ReaderAt) io.Reader { 33 return io.NewSectionReader(r, 0, math.MaxInt64) 34 } 35 36 // ReaderAtEqual compares the contents of r1 and r2. 37 func ReaderAtEqual(r1, r2 io.ReaderAt) bool { 38 var c, d []byte 39 var err error 40 if r1 != nil { 41 c, err = ReadAll(r1) 42 if err != nil { 43 return false 44 } 45 } 46 47 if r2 != nil { 48 d, err = ReadAll(r2) 49 if err != nil { 50 return false 51 } 52 } 53 return bytes.Equal(c, d) 54 }