v2ray-core/common/buf/buffered_writer.go

74 lines
1.8 KiB
Go
Raw Normal View History

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-05-23 22:54:30 +00:00
func NewBufferedWriter(writer io.Writer) *BufferedWriter {
return NewBufferedWriterSize(writer, 1024)
}
2017-10-22 13:03:20 +00:00
// NewBufferedWriterSize creates a BufferedWriter with specified buffer size.
2017-05-23 22:54:30 +00:00
func NewBufferedWriterSize(writer io.Writer, size uint32) *BufferedWriter {
2016-02-26 22:45:33 +00:00
return &BufferedWriter{
2017-05-23 22:54:30 +00:00
writer: writer,
buffer: NewLocal(int(size)),
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-05-23 22:54:30 +00:00
bytesWritten := 0
for bytesWritten < len(b) {
nBytes, err := w.buffer.Write(b[bytesWritten:])
if err != nil {
return bytesWritten, err
2016-12-04 20:34:22 +00:00
}
2017-05-23 22:54:30 +00:00
bytesWritten += nBytes
if w.buffer.IsFull() {
if err := w.Flush(); err != nil {
return bytesWritten, err
2016-12-04 20:34:22 +00:00
}
}
2016-02-26 22:45:33 +00:00
}
2017-05-23 22:54:30 +00:00
return bytesWritten, 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
}