| 1 | package wrapio |
| 2 | |
| 3 | import ( |
| 4 | "encoding/gob" |
| 5 | "io" |
| 6 | |
| 7 | "github.com/djherbis/buffer/limio" |
| 8 | ) |
| 9 | |
| 10 | // ReadWriterAt implements io.ReaderAt and io.WriterAt |
| 11 | type ReadWriterAt interface { |
| 12 | io.ReaderAt |
| 13 | io.WriterAt |
| 14 | } |
| 15 | |
| 16 | // Wrapper implements a io.ReadWriter and ReadWriterAt such that |
| 17 | // when reading/writing goes past N bytes, it "wraps" back to the beginning. |
| 18 | type Wrapper struct { |
| 19 | // N is the offset at which to "wrap" back to the start |
| 20 | N int64 |
| 21 | // L is the length of the data written |
| 22 | L int64 |
| 23 | // O is our offset in the data |
| 24 | O int64 |
| 25 | rwa ReadWriterAt |
| 26 | } |
| 27 | |
| 28 | // NewWrapper creates a Wrapper based on ReadWriterAt rwa. |
| 29 | // L is the current length, O is the current offset, and N is offset at which we "wrap". |
| 30 | func NewWrapper(rwa ReadWriterAt, L, O, N int64) *Wrapper { |
| 31 | return &Wrapper{ |
| 32 | L: L, |
| 33 | O: O, |
| 34 | N: N, |
| 35 | rwa: rwa, |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | // Len returns the # of bytes in the Wrapper |
| 40 | func (wpr *Wrapper) Len() int64 { |
| 41 | return wpr.L |
| 42 | } |
| 43 | |
| 44 | // Cap returns the "wrap" offset (max # of bytes) |
| 45 | func (wpr *Wrapper) Cap() int64 { |
| 46 | return wpr.N |
| 47 | } |
| 48 | |
| 49 | // Reset seeks to the start (0 offset), and sets the length to 0. |
| 50 | func (wpr *Wrapper) Reset() { |
| 51 | wpr.O = 0 |
| 52 | wpr.L = 0 |
| 53 | } |
| 54 | |
| 55 | // SetReadWriterAt lets you switch the underlying Read/WriterAt |
| 56 | func (wpr *Wrapper) SetReadWriterAt(rwa ReadWriterAt) { |
| 57 | wpr.rwa = rwa |
| 58 | } |
| 59 | |
| 60 | // Read reads from the current offset into p, wrapping at Cap() |
| 61 | func (wpr *Wrapper) Read(p []byte) (n int, err error) { |
| 62 | n, err = wpr.ReadAt(p, 0) |
| 63 | wpr.L -= int64(n) |
| 64 | wpr.O += int64(n) |
| 65 | wpr.O %= wpr.N |
| 66 | return n, err |
| 67 | } |
| 68 | |
| 69 | // ReadAt reads from the current offset+off into p, wrapping at Cap() |
| 70 | func (wpr *Wrapper) ReadAt(p []byte, off int64) (n int, err error) { |
| 71 | wrap := NewWrapReader(wpr.rwa, wpr.O+off, wpr.N) |
| 72 | r := io.LimitReader(wrap, wpr.L-off) |
| 73 | return r.Read(p) |
| 74 | } |
| 75 | |
| 76 | // Write writes p to the end of the Wrapper (at Len()), wrapping at Cap() |
| 77 | func (wpr *Wrapper) Write(p []byte) (n int, err error) { |
| 78 | return wpr.WriteAt(p, wpr.L) |
| 79 | } |
| 80 | |
| 81 | // WriteAt writes p at the current offset+off, wrapping at Cap() |
| 82 | func (wpr *Wrapper) WriteAt(p []byte, off int64) (n int, err error) { |
| 83 | wrap := NewWrapWriter(wpr.rwa, wpr.O+off, wpr.N) |
| 84 | w := limio.LimitWriter(wrap, wpr.N-off) |
| 85 | n, err = w.Write(p) |
| 86 | if wpr.L < off+int64(n) { |
| 87 | wpr.L = int64(n) + off |
| 88 | } |
| 89 | return n, err |
| 90 | } |
| 91 | |
| 92 | func init() { |
| 93 | gob.Register(&Wrapper{}) |
| 94 | } |