mirror of https://github.com/v2ray/v2ray-core
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
122 lines
2.0 KiB
122 lines
2.0 KiB
8 years ago
|
package bufio
|
||
9 years ago
|
|
||
|
import (
|
||
|
"io"
|
||
9 years ago
|
"sync"
|
||
8 years ago
|
|
||
8 years ago
|
"v2ray.com/core/common/buf"
|
||
8 years ago
|
"v2ray.com/core/common/errors"
|
||
9 years ago
|
)
|
||
|
|
||
|
type BufferedWriter struct {
|
||
9 years ago
|
sync.Mutex
|
||
9 years ago
|
writer io.Writer
|
||
8 years ago
|
buffer *buf.Buffer
|
||
9 years ago
|
cached bool
|
||
|
}
|
||
|
|
||
8 years ago
|
func NewWriter(rawWriter io.Writer) *BufferedWriter {
|
||
9 years ago
|
return &BufferedWriter{
|
||
|
writer: rawWriter,
|
||
8 years ago
|
buffer: buf.NewSmall(),
|
||
9 years ago
|
cached: true,
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
func (v *BufferedWriter) ReadFrom(reader io.Reader) (int64, error) {
|
||
|
v.Lock()
|
||
|
defer v.Unlock()
|
||
9 years ago
|
|
||
8 years ago
|
if v.writer == nil {
|
||
8 years ago
|
return 0, io.ErrClosedPipe
|
||
9 years ago
|
}
|
||
|
|
||
|
totalBytes := int64(0)
|
||
|
for {
|
||
8 years ago
|
oriSize := v.buffer.Len()
|
||
|
err := v.buffer.AppendSupplier(buf.ReadFrom(reader))
|
||
|
totalBytes += int64(v.buffer.Len() - oriSize)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
if errors.Cause(err) == io.EOF {
|
||
9 years ago
|
return totalBytes, nil
|
||
|
}
|
||
|
return totalBytes, err
|
||
|
}
|
||
8 years ago
|
v.FlushWithoutLock()
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
func (v *BufferedWriter) Write(b []byte) (int, error) {
|
||
|
v.Lock()
|
||
|
defer v.Unlock()
|
||
9 years ago
|
|
||
8 years ago
|
if v.writer == nil {
|
||
8 years ago
|
return 0, io.ErrClosedPipe
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
if !v.cached {
|
||
|
return v.writer.Write(b)
|
||
9 years ago
|
}
|
||
8 years ago
|
nBytes, err := v.buffer.Write(b)
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
8 years ago
|
if v.buffer.IsFull() {
|
||
8 years ago
|
err := v.FlushWithoutLock()
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
if nBytes < len(b) {
|
||
|
if _, err := v.writer.Write(b[nBytes:]); err != nil {
|
||
|
return nBytes, err
|
||
|
}
|
||
|
}
|
||
9 years ago
|
}
|
||
8 years ago
|
return len(b), nil
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func (v *BufferedWriter) Flush() error {
|
||
|
v.Lock()
|
||
|
defer v.Unlock()
|
||
9 years ago
|
|
||
8 years ago
|
if v.writer == nil {
|
||
8 years ago
|
return io.ErrClosedPipe
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
return v.FlushWithoutLock()
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func (v *BufferedWriter) FlushWithoutLock() error {
|
||
|
defer v.buffer.Clear()
|
||
|
for !v.buffer.IsEmpty() {
|
||
8 years ago
|
nBytes, err := v.writer.Write(v.buffer.Bytes())
|
||
9 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
8 years ago
|
v.buffer.SliceFrom(nBytes)
|
||
9 years ago
|
}
|
||
9 years ago
|
return nil
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func (v *BufferedWriter) Cached() bool {
|
||
|
return v.cached
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func (v *BufferedWriter) SetCached(cached bool) {
|
||
|
v.cached = cached
|
||
|
if !cached && !v.buffer.IsEmpty() {
|
||
|
v.Flush()
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
func (v *BufferedWriter) Release() {
|
||
|
v.Flush()
|
||
9 years ago
|
|
||
8 years ago
|
v.Lock()
|
||
|
defer v.Unlock()
|
||
9 years ago
|
|
||
8 years ago
|
v.buffer.Release()
|
||
|
v.buffer = nil
|
||
|
v.writer = nil
|
||
9 years ago
|
}
|