mirror of
https://github.com/GrandpaNutz/fafda.git
synced 2026-03-11 22:15:35 +00:00
32 lines
496 B
Go
32 lines
496 B
Go
package partedio
|
|
|
|
import (
|
|
"io"
|
|
"sync"
|
|
)
|
|
|
|
type SyncReader struct {
|
|
reader io.Reader
|
|
mu sync.Mutex
|
|
}
|
|
|
|
func NewSyncReader(r io.Reader) io.Reader {
|
|
return &SyncReader{r, sync.Mutex{}}
|
|
}
|
|
|
|
func (br *SyncReader) Read(p []byte) (int, error) {
|
|
br.mu.Lock()
|
|
defer br.mu.Unlock()
|
|
|
|
currReadIdx := 0
|
|
// Loop until p is full
|
|
for currReadIdx < len(p) {
|
|
n, err := br.reader.Read(p[currReadIdx:])
|
|
currReadIdx += n
|
|
if err != nil {
|
|
return currReadIdx, err
|
|
}
|
|
}
|
|
|
|
return currReadIdx, nil
|
|
}
|