2017-02-15 21:51:01 +00:00
|
|
|
package buf
|
2016-02-26 22:45:33 +00:00
|
|
|
|
2017-04-17 20:38:33 +00:00
|
|
|
import "io"
|
2016-02-26 22:45:33 +00:00
|
|
|
|
2016-12-27 20:41:44 +00:00
|
|
|
// BufferedWriter is an io.Writer with internal buffer. It writes to underlying writer when buffer is full or on demand.
|
|
|
|
// This type is not thread safe.
|
2016-02-26 22:45:33 +00:00
|
|
|
type BufferedWriter struct {
|
2016-12-27 20:41:44 +00:00
|
|
|
writer io.Writer
|
2017-02-15 21:51:01 +00:00
|
|
|
buffer *Buffer
|
2016-12-27 20:41:44 +00:00
|
|
|
buffered bool
|
2016-02-26 22:45:33 +00:00
|
|
|
}
|
|
|
|
|
2017-02-20 09:33:35 +00:00
|
|
|
// NewBufferedWriter creates a new BufferedWriter.
|
2017-02-15 21:51:01 +00:00
|
|
|
func NewBufferedWriter(rawWriter io.Writer) *BufferedWriter {
|
2016-02-26 22:45:33 +00:00
|
|
|
return &BufferedWriter{
|
2016-12-27 20:41:44 +00:00
|
|
|
writer: rawWriter,
|
2017-02-15 21:51:01 +00:00
|
|
|
buffer: NewLocal(1024),
|
2016-12-27 20:41:44 +00:00
|
|
|
buffered: true,
|
2016-02-26 22:45:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-21 13:36:05 +00:00
|
|
|
// Write implements io.Writer.
|
2017-04-23 17:16:56 +00:00
|
|
|
func (w *BufferedWriter) Write(b []byte) (int, error) {
|
|
|
|
if !w.buffered || w.buffer == nil {
|
|
|
|
return w.writer.Write(b)
|
2016-02-26 22:45:33 +00:00
|
|
|
}
|
2017-04-23 17:16:56 +00:00
|
|
|
nBytes, err := w.buffer.Write(b)
|
2016-12-04 20:34:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2017-04-23 17:16:56 +00:00
|
|
|
if w.buffer.IsFull() {
|
|
|
|
if err := w.Flush(); err != nil {
|
2016-12-04 20:34:22 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if nBytes < len(b) {
|
2017-04-23 17:16:56 +00:00
|
|
|
if _, err := w.writer.Write(b[nBytes:]); err != nil {
|
2016-12-04 20:34:22 +00:00
|
|
|
return nBytes, err
|
|
|
|
}
|
|
|
|
}
|
2016-02-26 22:45:33 +00:00
|
|
|
}
|
2016-12-04 20:34:22 +00:00
|
|
|
return len(b), nil
|
2016-02-26 22:45:33 +00:00
|
|
|
}
|
|
|
|
|
2016-12-27 22:09:08 +00:00
|
|
|
// Flush writes all buffered content into underlying writer, if any.
|
2017-04-23 17:16:56 +00:00
|
|
|
func (w *BufferedWriter) Flush() error {
|
|
|
|
defer w.buffer.Clear()
|
|
|
|
for !w.buffer.IsEmpty() {
|
|
|
|
nBytes, err := w.writer.Write(w.buffer.Bytes())
|
2016-05-13 00:20:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-23 17:16:56 +00:00
|
|
|
w.buffer.SliceFrom(nBytes)
|
2016-02-26 22:45:33 +00:00
|
|
|
}
|
2016-05-13 00:20:07 +00:00
|
|
|
return nil
|
2016-02-26 22:45:33 +00:00
|
|
|
}
|
|
|
|
|
2016-12-27 22:09:08 +00:00
|
|
|
// IsBuffered returns true if this BufferedWriter holds a buffer.
|
2017-04-23 17:16:56 +00:00
|
|
|
func (w *BufferedWriter) IsBuffered() bool {
|
|
|
|
return w.buffered
|
2016-02-26 22:45:33 +00:00
|
|
|
}
|
|
|
|
|
2016-12-27 22:09:08 +00:00
|
|
|
// SetBuffered controls whether the BufferedWriter holds a buffer for writing. If not buffered, any write() calls into underlying writer directly.
|
2017-04-23 17:16:56 +00:00
|
|
|
func (w *BufferedWriter) SetBuffered(cached bool) error {
|
|
|
|
w.buffered = cached
|
|
|
|
if !cached && !w.buffer.IsEmpty() {
|
|
|
|
return w.Flush()
|
2016-02-26 22:45:33 +00:00
|
|
|
}
|
2017-01-06 10:40:35 +00:00
|
|
|
return nil
|
2016-02-26 22:45:33 +00:00
|
|
|
}
|