git-lfs/git/pkt_line_reader.go

60 lines
1.2 KiB
Go
Raw Normal View History

2016-10-27 23:09:02 +00:00
package git
import (
"io"
2016-11-15 17:01:18 +00:00
"github.com/git-lfs/git-lfs/tools"
2016-10-27 23:09:02 +00:00
)
2016-11-09 01:46:32 +00:00
type pktlineReader struct {
2016-11-08 20:45:38 +00:00
pl *pktline
2016-10-27 23:09:02 +00:00
buf []byte
}
2016-11-09 01:46:32 +00:00
var _ io.Reader = new(pktlineReader)
2016-10-27 23:09:02 +00:00
2016-11-09 01:46:32 +00:00
func (r *pktlineReader) Read(p []byte) (int, error) {
2016-10-27 23:09:02 +00:00
var n int
if len(r.buf) > 0 {
// If there is data in the buffer, shift as much out of it and
// into the given "p" as we can.
n = tools.MinInt(len(p), len(r.buf))
2016-10-27 23:09:02 +00:00
copy(p, r.buf[:n])
r.buf = r.buf[n:]
}
// Loop and grab as many packets as we can in a given "run", until we
// have either, a) overfilled the given buffer "p", or we have started
// to internally buffer in "r.buf".
for len(r.buf) == 0 {
2016-11-08 20:45:38 +00:00
chunk, err := r.pl.readPacket()
2016-10-27 23:09:02 +00:00
if err != nil {
return n, err
}
if len(chunk) == 0 {
// If we got an empty chunk, then we know that we have
// reached the end of processing for this particular
// packet, so let's terminate.
return n, io.EOF
}
// Figure out how much of the packet we can read into "p".
nn := tools.MinInt(len(chunk), len(p[n:]))
2016-10-27 23:09:02 +00:00
// Move that amount into "p", from where we left off.
copy(p[n:], chunk[:nn])
// And move the rest into the buffer.
r.buf = append(r.buf, chunk[nn:]...)
// Mark that we have read "nn" bytes into "p"
n += nn
}
return n, nil
}