v2ray-core/common/buf/buffered_writer.go

93 lines
2.1 KiB
Go
Raw Normal View History

2017-02-15 21:51:01 +00:00
package buf
2016-02-26 22:45:33 +00:00
import (
"io"
2016-12-09 11:08:25 +00:00
2016-12-04 08:10:47 +00:00
"v2ray.com/core/common/errors"
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
}
}
2016-12-27 22:09:08 +00:00
// ReadFrom implements io.ReaderFrom.ReadFrom().
2016-11-27 20:39:09 +00:00
func (v *BufferedWriter) ReadFrom(reader io.Reader) (int64, error) {
2016-06-01 19:17:44 +00:00
totalBytes := int64(0)
for {
2016-12-09 11:08:25 +00:00
oriSize := v.buffer.Len()
2017-02-15 21:51:01 +00:00
err := v.buffer.AppendSupplier(ReadFrom(reader))
2016-12-09 11:08:25 +00:00
totalBytes += int64(v.buffer.Len() - oriSize)
2016-06-01 19:17:44 +00:00
if err != nil {
2016-12-04 08:10:47 +00:00
if errors.Cause(err) == io.EOF {
2016-06-01 19:17:44 +00:00
return totalBytes, nil
}
return totalBytes, err
}
if err := v.Flush(); err != nil {
return totalBytes, err
}
2016-06-01 19:17:44 +00:00
}
}
2016-11-27 20:39:09 +00:00
func (v *BufferedWriter) Write(b []byte) (int, error) {
2016-12-27 20:41:44 +00:00
if !v.buffered || v.buffer == nil {
2016-11-27 20:39:09 +00:00
return v.writer.Write(b)
2016-02-26 22:45:33 +00:00
}
2016-12-04 20:34:22 +00:00
nBytes, err := v.buffer.Write(b)
if err != nil {
return 0, err
}
2016-11-27 20:39:09 +00:00
if v.buffer.IsFull() {
err := v.Flush()
2016-12-04 20:34:22 +00:00
if err != nil {
return 0, err
}
if nBytes < len(b) {
if _, err := v.writer.Write(b[nBytes:]); err != nil {
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.
2016-11-27 20:39:09 +00:00
func (v *BufferedWriter) Flush() error {
defer v.buffer.Clear()
for !v.buffer.IsEmpty() {
2016-12-05 14:19:14 +00:00
nBytes, err := v.writer.Write(v.buffer.Bytes())
2016-05-13 00:20:07 +00:00
if err != nil {
return err
}
2016-11-27 20:39:09 +00:00
v.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.
func (v *BufferedWriter) IsBuffered() bool {
2016-12-27 20:41:44 +00:00
return v.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-01-06 10:40:35 +00:00
func (v *BufferedWriter) SetBuffered(cached bool) error {
2016-12-27 20:41:44 +00:00
v.buffered = cached
2016-11-27 20:39:09 +00:00
if !cached && !v.buffer.IsEmpty() {
2017-01-06 10:40:35 +00:00
return v.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
}