| 377 | } |
| 378 | |
| 379 | func (r *websocketReader) drainPending(p []byte) int { |
| 380 | var n int |
| 381 | var max = len(p) |
| 382 | |
| 383 | for i, buf := range r.pending { |
| 384 | if n+len(buf) <= max { |
| 385 | copy(p[n:], buf) |
| 386 | n += len(buf) |
| 387 | } else { |
| 388 | // Is there room left? |
| 389 | if n < max { |
| 390 | // Write the partial and update this slice. |
| 391 | rem := max - n |
| 392 | copy(p[n:], buf[:rem]) |
| 393 | n += rem |
| 394 | r.pending[i] = buf[rem:] |
| 395 | } |
| 396 | // These are the remaining slices that will need to be used at |
| 397 | // the next Read() call. |
| 398 | r.pending = r.pending[i:] |
| 399 | return n |
| 400 | } |
| 401 | } |
| 402 | r.pending = r.pending[:0] |
| 403 | return n |
| 404 | } |
| 405 | |
| 406 | func wsGet(r io.Reader, buf []byte, pos, needed int) ([]byte, int, error) { |
| 407 | avail := len(buf) - pos |