From c93b9e81c03168ca69def94ba231d2ecc6672ce9 Mon Sep 17 00:00:00 2001 From: unclejack Date: Tue, 19 Aug 2014 19:58:07 +0300 Subject: [PATCH] pkg/ioutils: add NewBufReaderWithDrainbufAndBuffer Docker-DCO-1.1-Signed-off-by: Cristian Staretu (github: unclejack) --- pkg/ioutils/readers.go | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/pkg/ioutils/readers.go b/pkg/ioutils/readers.go index 8488034bd8..f059bb1666 100644 --- a/pkg/ioutils/readers.go +++ b/pkg/ioutils/readers.go @@ -24,16 +24,29 @@ func NewReadCloserWrapper(r io.Reader, closer func() error) io.ReadCloser { type bufReader struct { sync.Mutex - buf *bytes.Buffer - reader io.Reader - err error - wait sync.Cond + buf *bytes.Buffer + reader io.Reader + err error + wait sync.Cond + drainBuf []byte } func NewBufReader(r io.Reader) *bufReader { reader := &bufReader{ - buf: &bytes.Buffer{}, - reader: r, + buf: &bytes.Buffer{}, + drainBuf: make([]byte, 1024), + reader: r, + } + reader.wait.L = &reader.Mutex + go reader.drain() + return reader +} + +func NewBufReaderWithDrainbufAndBuffer(r io.Reader, drainBuffer []byte, buffer *bytes.Buffer) *bufReader { + reader := &bufReader{ + buf: buffer, + drainBuf: drainBuffer, + reader: r, } reader.wait.L = &reader.Mutex go reader.drain() @@ -41,14 +54,13 @@ func NewBufReader(r io.Reader) *bufReader { } func (r *bufReader) drain() { - buf := make([]byte, 1024) for { - n, err := r.reader.Read(buf) + n, err := r.reader.Read(r.drainBuf) r.Lock() if err != nil { r.err = err } else { - r.buf.Write(buf[0:n]) + r.buf.Write(r.drainBuf[0:n]) } r.wait.Signal() r.Unlock()