| 1 | package wrapioscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/ |
| 2 | |
| 3 | import "ioscip-go gomod github.com/golang/go/src go1.13 io/" |
| 4 | |
| 5 | // DoerAt is a common interface for wrappers WriteAt or ReadAt functions |
| 6 | type DoerAtscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/DoerAt# interface { |
| 7 | DoAtscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/DoerAt#DoAt.([]byte, int64) (int, error) |
| 8 | } |
| 9 | |
| 10 | // DoAtFunc is implemented by ReadAt/WriteAt |
| 11 | type DoAtFuncscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/DoAtFunc# func([]byte, int64) (int, error) |
| 12 | |
| 13 | type wrapperscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper# struct { |
| 14 | offscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#off. int64 |
| 15 | wrapAtscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#wrapAt. int64 |
| 16 | doatscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#doat. DoAtFuncscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/DoAtFunc# |
| 17 | } |
| 18 | |
| 19 | func (w *wrapperscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#) Offsetscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#Offset().() int64 { |
| 20 | return w.offscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#off. |
| 21 | } |
| 22 | |
| 23 | func (w *wrapperscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#) Seekscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#Seek().(offset int64, whence int) (int64, error) { |
| 24 | switch whence { |
| 25 | case 0: |
| 26 | w.offscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#off. = offset |
| 27 | case 1: |
| 28 | w.offscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#off. += offset |
| 29 | case 2: |
| 30 | w.offscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#off. = (w.wrapAtscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#wrapAt. + offset) |
| 31 | } |
| 32 | w.offscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#off. %= w.wrapAtscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#wrapAt. |
| 33 | return w.offscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#off., nil |
| 34 | } |
| 35 | |
| 36 | func (w *wrapperscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#) DoAtscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#DoAt().(p []byte, off int64) (n int, err error) { |
| 37 | return w.doatscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#doat.(p, off) |
| 38 | } |
| 39 | |
| 40 | // WrapWriter wraps writes around a section of data. |
| 41 | type WrapWriterscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/WrapWriter# struct { |
| 42 | *wrapperscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/WrapWriter#wrapper. |
| 43 | } |
| 44 | |
| 45 | // NewWrapWriter creates a WrapWriter starting at offset off, and wrapping at offset wrapAt. |
| 46 | func NewWrapWriterscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/NewWrapWriter().(w ioscip-go gomod github.com/golang/go/src go1.13 io/.WriterAtscip-go gomod github.com/golang/go/src go1.13 io/WriterAt#, off int64, wrapAt int64) *WrapWriterscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/WrapWriter# { |
| 47 | return &WrapWriterscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/WrapWriter#{ |
| 48 | &wrapperscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#{ |
| 49 | doatscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#doat.: w.WriteAtscip-go gomod github.com/golang/go/src go1.13 io/WriterAt#WriteAt()., |
| 50 | offscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#off.: (off % wrapAt), |
| 51 | wrapAtscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#wrapAt.: wrapAt, |
| 52 | }, |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // Write writes p starting at the current offset, wrapping when it reaches the end. |
| 57 | // The current offset is shifted forward by the amount written. |
| 58 | func (w *WrapWriterscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/WrapWriter#) Writescip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/WrapWriter#Write().(p []byte) (n int, err error) { |
| 59 | n, err = Wrapscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/Wrap().(w, p, w.offscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#off., w.wrapAtscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#wrapAt.) |
| 60 | w.offscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#off. = (w.offscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#off. + int64(n)) % w.wrapAtscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#wrapAt. |
| 61 | return n, err |
| 62 | } |
| 63 | |
| 64 | // WriteAt writes p starting at offset off, wrapping when it reaches the end. |
| 65 | func (w *WrapWriterscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/WrapWriter#) WriteAtscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/WrapWriter#WriteAt().(p []byte, off int64) (n int, err error) { |
| 66 | return Wrapscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/Wrap().(w, p, off, w.wrapAtscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#wrapAt.) |
| 67 | } |
| 68 | |
| 69 | // WrapReader wraps reads around a section of data. |
| 70 | type WrapReaderscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/WrapReader# struct { |
| 71 | *wrapperscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/WrapReader#wrapper. |
| 72 | } |
| 73 | |
| 74 | // NewWrapReader creates a WrapReader starting at offset off, and wrapping at offset wrapAt. |
| 75 | func NewWrapReaderscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/NewWrapReader().(r ioscip-go gomod github.com/golang/go/src go1.13 io/.ReaderAtscip-go gomod github.com/golang/go/src go1.13 io/ReaderAt#, off int64, wrapAt int64) *WrapReaderscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/WrapReader# { |
| 76 | return &WrapReaderscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/WrapReader#{ |
| 77 | &wrapperscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#{ |
| 78 | doatscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#doat.: r.ReadAtscip-go gomod github.com/golang/go/src go1.13 io/ReaderAt#ReadAt()., |
| 79 | offscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#off.: (off % wrapAt), |
| 80 | wrapAtscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#wrapAt.: wrapAt, |
| 81 | }, |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // Read reads into p starting at the current offset, wrapping if it reaches the end. |
| 86 | // The current offset is shifted forward by the amount read. |
| 87 | func (r *WrapReaderscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/WrapReader#) Readscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/WrapReader#Read().(p []byte) (n int, err error) { |
| 88 | n, err = Wrapscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/Wrap().(r, p, r.offscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#off., r.wrapAtscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#wrapAt.) |
| 89 | r.offscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#off. = (r.offscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#off. + int64(n)) % r.wrapAtscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#wrapAt. |
| 90 | return n, err |
| 91 | } |
| 92 | |
| 93 | // ReadAt reads into p starting at the current offset, wrapping when it reaches the end. |
| 94 | func (r *WrapReaderscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/WrapReader#) ReadAtscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/WrapReader#ReadAt().(p []byte, off int64) (n int, err error) { |
| 95 | return Wrapscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/Wrap().(r, p, off, r.wrapAtscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/wrapper#wrapAt.) |
| 96 | } |
| 97 | |
| 98 | // maxConsecutiveEmptyActions determines how many consecutive empty reads/writes can occur before giving up |
| 99 | const maxConsecutiveEmptyActionsscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/maxConsecutiveEmptyActions. = 100 |
| 100 | |
| 101 | // Wrap causes an action on an array of bytes (like read/write) to be done from an offset off, |
| 102 | // wrapping at offset wrapAt. |
| 103 | func Wrapscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/Wrap().(w DoerAtscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/DoerAt#, p []byte, off int64, wrapAt int64) (n int, err error) { |
| 104 | var m, fails int |
| 105 | |
| 106 | off %= wrapAt |
| 107 | |
| 108 | for len(p) > 0 { |
| 109 | |
| 110 | if off+int64(len(p)) < wrapAt { |
| 111 | m, err = w.DoAtscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/DoerAt#DoAt.(p, off) |
| 112 | } else { |
| 113 | space := wrapAt - off |
| 114 | m, err = w.DoAtscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/DoerAt#DoAt.(p[:space], off) |
| 115 | } |
| 116 | |
| 117 | if err != nil && err != ioscip-go gomod github.com/golang/go/src go1.13 io/.EOFscip-go gomod github.com/golang/go/src go1.13 io/EOF. { |
| 118 | return n + m, err |
| 119 | } |
| 120 | |
| 121 | switch m { |
| 122 | case 0: |
| 123 | fails++ |
| 124 | default: |
| 125 | fails = 0 |
| 126 | } |
| 127 | |
| 128 | if fails > maxConsecutiveEmptyActionsscip-go gomod github.com/djherbis/buffer . `github.com/djherbis/buffer/wrapio`/maxConsecutiveEmptyActions. { |
| 129 | return n + m, ioscip-go gomod github.com/golang/go/src go1.13 io/.ErrNoProgressscip-go gomod github.com/golang/go/src go1.13 io/ErrNoProgress. |
| 130 | } |
| 131 | |
| 132 | n += m |
| 133 | p = p[m:] |
| 134 | off += int64(m) |
| 135 | off %= wrapAt |
| 136 | } |
| 137 | |
| 138 | return n, err |
| 139 | } |