2016-12-09 07:17:34 -05:00
|
|
|
package bufio
|
2016-02-26 17:45:33 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2016-12-09 06:08:25 -05:00
|
|
|
|
2016-12-11 20:14:48 -05:00
|
|
|
"v2ray.com/core/common"
|
2016-12-09 05:35:27 -05:00
|
|
|
"v2ray.com/core/common/buf"
|
2016-12-04 03:10:47 -05:00
|
|
|
"v2ray.com/core/common/errors"
|
2016-02-26 17:45:33 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type BufferedWriter struct {
|
|
|
|
writer io.Writer
|
2016-12-09 05:35:27 -05:00
|
|
|
buffer *buf.Buffer
|
2016-02-26 17:45:33 -05:00
|
|
|
cached bool
|
|
|
|
}
|
|
|
|
|
2016-12-09 07:17:34 -05:00
|
|
|
func NewWriter(rawWriter io.Writer) *BufferedWriter {
|
2016-02-26 17:45:33 -05:00
|
|
|
return &BufferedWriter{
|
|
|
|
writer: rawWriter,
|
2016-12-09 06:08:25 -05:00
|
|
|
buffer: buf.NewSmall(),
|
2016-02-26 17:45:33 -05:00
|
|
|
cached: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *BufferedWriter) ReadFrom(reader io.Reader) (int64, error) {
|
2016-06-01 15:17:44 -04:00
|
|
|
totalBytes := int64(0)
|
|
|
|
for {
|
2016-12-09 06:08:25 -05:00
|
|
|
oriSize := v.buffer.Len()
|
|
|
|
err := v.buffer.AppendSupplier(buf.ReadFrom(reader))
|
|
|
|
totalBytes += int64(v.buffer.Len() - oriSize)
|
2016-06-01 15:17:44 -04:00
|
|
|
if err != nil {
|
2016-12-04 03:10:47 -05:00
|
|
|
if errors.Cause(err) == io.EOF {
|
2016-06-01 15:17:44 -04:00
|
|
|
return totalBytes, nil
|
|
|
|
}
|
|
|
|
return totalBytes, err
|
|
|
|
}
|
2016-12-26 19:01:47 -05:00
|
|
|
if err := v.Flush(); err != nil {
|
|
|
|
return totalBytes, err
|
|
|
|
}
|
2016-06-01 15:17:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *BufferedWriter) Write(b []byte) (int, error) {
|
2016-12-26 19:01:47 -05:00
|
|
|
if !v.cached || v.buffer == nil {
|
2016-11-27 15:39:09 -05:00
|
|
|
return v.writer.Write(b)
|
2016-02-26 17:45:33 -05:00
|
|
|
}
|
2016-12-04 15:34:22 -05:00
|
|
|
nBytes, err := v.buffer.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2016-11-27 15:39:09 -05:00
|
|
|
if v.buffer.IsFull() {
|
2016-12-26 19:01:47 -05:00
|
|
|
err := v.Flush()
|
2016-12-04 15:34:22 -05: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 17:45:33 -05:00
|
|
|
}
|
2016-12-04 15:34:22 -05:00
|
|
|
return len(b), nil
|
2016-02-26 17:45:33 -05:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *BufferedWriter) Flush() error {
|
|
|
|
defer v.buffer.Clear()
|
|
|
|
for !v.buffer.IsEmpty() {
|
2016-12-05 09:19:14 -05:00
|
|
|
nBytes, err := v.writer.Write(v.buffer.Bytes())
|
2016-05-12 20:20:07 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-27 15:39:09 -05:00
|
|
|
v.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-11-27 15:39:09 -05:00
|
|
|
func (v *BufferedWriter) Cached() bool {
|
|
|
|
return v.cached
|
2016-02-26 17:45:33 -05:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *BufferedWriter) SetCached(cached bool) {
|
|
|
|
v.cached = cached
|
|
|
|
if !cached && !v.buffer.IsEmpty() {
|
|
|
|
v.Flush()
|
2016-02-26 17:45:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *BufferedWriter) Release() {
|
|
|
|
v.Flush()
|
2016-05-22 13:41:48 -04:00
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
v.buffer.Release()
|
|
|
|
v.buffer = nil
|
2016-12-11 20:14:48 -05:00
|
|
|
|
|
|
|
if releasable, ok := v.writer.(common.Releasable); ok {
|
|
|
|
releasable.Release()
|
|
|
|
}
|
2016-02-26 17:45:33 -05:00
|
|
|
}
|