boundedreadwriteseekcloser.go (2189B)
1 package rifs 2 3 import ( 4 "io" 5 6 "github.com/dsoprea/go-logging" 7 ) 8 9 // BoundedReadWriteSeekCloser wraps a RWS that is also a closer with boundaries. 10 // This proxies the RWS methods to the inner BRWS inside. 11 type BoundedReadWriteSeekCloser struct { 12 io.Closer 13 *BoundedReadWriteSeeker 14 } 15 16 // NewBoundedReadWriteSeekCloser returns a new BoundedReadWriteSeekCloser. 17 func NewBoundedReadWriteSeekCloser(rwsc ReadWriteSeekCloser, minimumOffset int64, staticFileSize int64) (brwsc *BoundedReadWriteSeekCloser, err error) { 18 defer func() { 19 if state := recover(); state != nil { 20 err = log.Wrap(state.(error)) 21 } 22 }() 23 24 bs, err := NewBoundedReadWriteSeeker(rwsc, minimumOffset, staticFileSize) 25 log.PanicIf(err) 26 27 brwsc = &BoundedReadWriteSeekCloser{ 28 Closer: rwsc, 29 BoundedReadWriteSeeker: bs, 30 } 31 32 return brwsc, nil 33 } 34 35 // Seek forwards calls to the inner RWS. 36 func (rwsc *BoundedReadWriteSeekCloser) Seek(offset int64, whence int) (newOffset int64, err error) { 37 defer func() { 38 if state := recover(); state != nil { 39 err = log.Wrap(state.(error)) 40 } 41 }() 42 43 newOffset, err = rwsc.BoundedReadWriteSeeker.Seek(offset, whence) 44 log.PanicIf(err) 45 46 return newOffset, nil 47 } 48 49 // Read forwards calls to the inner RWS. 50 func (rwsc *BoundedReadWriteSeekCloser) Read(buffer []byte) (readCount int, err error) { 51 defer func() { 52 if state := recover(); state != nil { 53 err = log.Wrap(state.(error)) 54 } 55 }() 56 57 readCount, err = rwsc.BoundedReadWriteSeeker.Read(buffer) 58 if err != nil { 59 if err == io.EOF { 60 return 0, err 61 } 62 63 log.Panic(err) 64 } 65 66 return readCount, nil 67 } 68 69 // Write forwards calls to the inner RWS. 70 func (rwsc *BoundedReadWriteSeekCloser) Write(buffer []byte) (writtenCount int, err error) { 71 defer func() { 72 if state := recover(); state != nil { 73 err = log.Wrap(state.(error)) 74 } 75 }() 76 77 writtenCount, err = rwsc.BoundedReadWriteSeeker.Write(buffer) 78 log.PanicIf(err) 79 80 return writtenCount, nil 81 } 82 83 // Close forwards calls to the inner RWS. 84 func (rwsc *BoundedReadWriteSeekCloser) Close() (err error) { 85 defer func() { 86 if state := recover(); state != nil { 87 err = log.Wrap(state.(error)) 88 } 89 }() 90 91 err = rwsc.Closer.Close() 92 log.PanicIf(err) 93 94 return nil 95 }