Write appends p to the buffer, keeping only the last size bytes. The return value n is the length of p;
(p []byte)
| 25 | // Write appends p to the buffer, keeping only the last size bytes. |
| 26 | // The return value n is the length of p; |
| 27 | func (t *TruncBuffer) Write(p []byte) (int, error) { |
| 28 | t.mu.Lock() |
| 29 | defer t.mu.Unlock() |
| 30 | |
| 31 | // If input is larger than the buffer, only keep the tail. |
| 32 | if len(p) >= t.size { |
| 33 | copy(t.buf, p[len(p)-t.size:]) |
| 34 | t.head = 0 |
| 35 | t.full = true |
| 36 | return len(p), nil |
| 37 | } |
| 38 | |
| 39 | for _, b := range p { |
| 40 | t.buf[t.head] = b |
| 41 | t.head++ |
| 42 | if t.head == t.size { |
| 43 | t.head = 0 |
| 44 | t.full = true |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | return len(p), nil |
| 49 | } |
| 50 | |
| 51 | // Bytes returns a copy of the current buffer contents in order. |
| 52 | func (t *TruncBuffer) Bytes() []byte { |
no outgoing calls