| 1 | package buffer |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/gob" |
| 6 | "io" |
| 7 | "math" |
| 8 | ) |
| 9 | |
| 10 | type chain struct { |
| 11 | Buf BufferAt |
| 12 | Next BufferAt |
| 13 | } |
| 14 | |
| 15 | type nopBufferAt struct { |
| 16 | Buffer |
| 17 | } |
| 18 | |
| 19 | func (buf *nopBufferAt) ReadAt(p []byte, off int64) (int, error) { |
| 20 | panic("ReadAt not implemented") |
| 21 | } |
| 22 | |
| 23 | func (buf *nopBufferAt) WriteAt(p []byte, off int64) (int, error) { |
| 24 | panic("WriteAt not implemented") |
| 25 | } |
| 26 | |
| 27 | // toBufferAt converts a Buffer to a BufferAt with nop ReadAt and WriteAt funcs |
| 28 | func toBufferAt(buf Buffer) BufferAt { |
| 29 | return &nopBufferAt{Buffer: buf} |
| 30 | } |
| 31 | |
| 32 | // NewMultiAt returns a BufferAt which is the logical concatenation of the passed BufferAts. |
| 33 | // The data in the buffers is shifted such that there is no non-empty buffer following |
| 34 | // a non-full buffer, this process is also run after every Read. |
| 35 | // If no buffers are passed, the returned Buffer is nil. |
| 36 | func NewMultiAt(buffers ...BufferAt) BufferAt { |
| 37 | if len(buffers) == 0 { |
| 38 | return nil |
| 39 | } else if len(buffers) == 1 { |
| 40 | return buffers[0] |
| 41 | } |
| 42 | |
| 43 | buf := &chain{ |
| 44 | Buf: buffers[0], |
| 45 | Next: NewMultiAt(buffers[1:]...), |
| 46 | } |
| 47 | |
| 48 | buf.Defrag() |
| 49 | |
| 50 | return buf |
| 51 | } |
| 52 | |
| 53 | // NewMulti returns a Buffer which is the logical concatenation of the passed buffers. |
| 54 | // The data in the buffers is shifted such that there is no non-empty buffer following |
| 55 | // a non-full buffer, this process is also run after every Read. |
| 56 | // If no buffers are passed, the returned Buffer is nil. |
| 57 | func NewMulti(buffers ...Buffer) Buffer { |
| 58 | bufAt := make([]BufferAt, len(buffers)) |
| 59 | for i, buf := range buffers { |
| 60 | bufAt[i] = toBufferAt(buf) |
| 61 | } |
| 62 | return NewMultiAt(bufAt...) |
| 63 | } |
| 64 | |
| 65 | func (buf *chain) Reset() { |
| 66 | buf.Next.Reset() |
| 67 | buf.Buf.Reset() |
| 68 | } |
| 69 | |
| 70 | func (buf *chain) Cap() (n int64) { |
| 71 | Next := buf.Next.Cap() |
| 72 | if buf.Buf.Cap() > math.MaxInt64-Next { |
| 73 | return math.MaxInt64 |
| 74 | } |
| 75 | return buf.Buf.Cap() + Next |
| 76 | } |
| 77 | |
| 78 | func (buf *chain) Len() (n int64) { |
| 79 | Next := buf.Next.Len() |
| 80 | if buf.Buf.Len() > math.MaxInt64-Next { |
| 81 | return math.MaxInt64 |
| 82 | } |
| 83 | return buf.Buf.Len() + Next |
| 84 | } |
| 85 | |
| 86 | func (buf *chain) Defrag() { |
| 87 | for !Full(buf.Buf) && !Empty(buf.Next) { |
| 88 | r := io.LimitReader(buf.Next, Gap(buf.Buf)) |
| 89 | if _, err := io.Copy(buf.Buf, r); err != nil && err != io.EOF { |
| 90 | return |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func (buf *chain) Read(p []byte) (n int, err error) { |
| 96 | n, err = buf.Buf.Read(p) |
| 97 | if len(p[n:]) > 0 && (err == nil || err == io.EOF) { |
| 98 | m, err := buf.Next.Read(p[n:]) |
| 99 | n += m |
| 100 | if err != nil { |
| 101 | return n, err |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | buf.Defrag() |
| 106 | |
| 107 | return n, err |
| 108 | } |
| 109 | |
| 110 | func (buf *chain) ReadAt(p []byte, off int64) (n int, err error) { |
| 111 | if buf.Buf.Len() < off { |
| 112 | return buf.Next.ReadAt(p, off-buf.Buf.Len()) |
| 113 | } |
| 114 | |
| 115 | n, err = buf.Buf.ReadAt(p, off) |
| 116 | if len(p[n:]) > 0 && (err == nil || err == io.EOF) { |
| 117 | var m int |
| 118 | m, err = buf.Next.ReadAt(p[n:], 0) |
| 119 | n += m |
| 120 | } |
| 121 | return n, err |
| 122 | } |
| 123 | |
| 124 | func (buf *chain) Write(p []byte) (n int, err error) { |
| 125 | if n, err = buf.Buf.Write(p); err == io.ErrShortWrite { |
| 126 | err = nil |
| 127 | } |
| 128 | p = p[n:] |
| 129 | if len(p) > 0 && err == nil { |
| 130 | m, err := buf.Next.Write(p) |
| 131 | n += m |
| 132 | if err != nil { |
| 133 | return n, err |
| 134 | } |
| 135 | } |
| 136 | return n, err |
| 137 | } |
| 138 | |
| 139 | func (buf *chain) WriteAt(p []byte, off int64) (n int, err error) { |
| 140 | switch { |
| 141 | case buf.Buf.Cap() <= off: // past the end |
| 142 | return buf.Next.WriteAt(p, off-buf.Buf.Cap()) |
| 143 | |
| 144 | case buf.Buf.Cap() >= off+int64(len(p)): // fits in |
| 145 | return buf.Buf.WriteAt(p, off) |
| 146 | |
| 147 | default: // partial fit |
| 148 | n, err = buf.Buf.WriteAt(p, off) |
| 149 | if len(p[n:]) > 0 && (err == nil || err == io.ErrShortWrite) { |
| 150 | var m int |
| 151 | m, err = buf.Next.WriteAt(p[n:], 0) |
| 152 | n += m |
| 153 | } |
| 154 | return n, err |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | func init() { |
| 159 | gob.Register(&chain{}) |
| 160 | gob.Register(&nopBufferAt{}) |
| 161 | } |
| 162 | |
| 163 | func (buf *chain) MarshalBinary() ([]byte, error) { |
| 164 | b := bytes.NewBuffer(nil) |
| 165 | enc := gob.NewEncoder(b) |
| 166 | if err := enc.Encode(&buf.Buf); err != nil { |
| 167 | return nil, err |
| 168 | } |
| 169 | if err := enc.Encode(&buf.Next); err != nil { |
| 170 | return nil, err |
| 171 | } |
| 172 | return b.Bytes(), nil |
| 173 | } |
| 174 | |
| 175 | func (buf *chain) UnmarshalBinary(data []byte) error { |
| 176 | b := bytes.NewBuffer(data) |
| 177 | dec := gob.NewDecoder(b) |
| 178 | if err := dec.Decode(&buf.Buf); err != nil { |
| 179 | return err |
| 180 | } |
| 181 | if err := dec.Decode(&buf.Next); err != nil { |
| 182 | return err |
| 183 | } |
| 184 | return nil |
| 185 | } |