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