83 lines
1.2 KiB
Go
83 lines
1.2 KiB
Go
|
package ioutils
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"io"
|
||
|
"sync"
|
||
|
)
|
||
|
|
||
|
type readCloserWrapper struct {
|
||
|
io.Reader
|
||
|
closer func() error
|
||
|
}
|
||
|
|
||
|
func (r *readCloserWrapper) Close() error {
|
||
|
return r.closer()
|
||
|
}
|
||
|
|
||
|
func NewReadCloserWrapper(r io.Reader, closer func() error) io.ReadCloser {
|
||
|
return &readCloserWrapper{
|
||
|
Reader: r,
|
||
|
closer: closer,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
type bufReader struct {
|
||
|
sync.Mutex
|
||
|
buf *bytes.Buffer
|
||
|
reader io.Reader
|
||
|
err error
|
||
|
wait sync.Cond
|
||
|
}
|
||
|
|
||
|
func NewBufReader(r io.Reader) *bufReader {
|
||
|
reader := &bufReader{
|
||
|
buf: &bytes.Buffer{},
|
||
|
reader: r,
|
||
|
}
|
||
|
reader.wait.L = &reader.Mutex
|
||
|
go reader.drain()
|
||
|
return reader
|
||
|
}
|
||
|
|
||
|
func (r *bufReader) drain() {
|
||
|
buf := make([]byte, 1024)
|
||
|
for {
|
||
|
n, err := r.reader.Read(buf)
|
||
|
r.Lock()
|
||
|
if err != nil {
|
||
|
r.err = err
|
||
|
} else {
|
||
|
r.buf.Write(buf[0:n])
|
||
|
}
|
||
|
r.wait.Signal()
|
||
|
r.Unlock()
|
||
|
if err != nil {
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (r *bufReader) Read(p []byte) (n int, err error) {
|
||
|
r.Lock()
|
||
|
defer r.Unlock()
|
||
|
for {
|
||
|
n, err = r.buf.Read(p)
|
||
|
if n > 0 {
|
||
|
return n, err
|
||
|
}
|
||
|
if r.err != nil {
|
||
|
return 0, r.err
|
||
|
}
|
||
|
r.wait.Wait()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (r *bufReader) Close() error {
|
||
|
closer, ok := r.reader.(io.ReadCloser)
|
||
|
if !ok {
|
||
|
return nil
|
||
|
}
|
||
|
return closer.Close()
|
||
|
}
|