2016-02-26 22:45:33 +00:00
|
|
|
package io
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2016-05-20 11:56:19 +00:00
|
|
|
"sync"
|
2016-02-26 22:45:33 +00:00
|
|
|
|
|
|
|
"github.com/v2ray/v2ray-core/common/alloc"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BufferedWriter struct {
|
2016-05-20 11:56:19 +00:00
|
|
|
sync.Mutex
|
2016-02-26 22:45:33 +00:00
|
|
|
writer io.Writer
|
|
|
|
buffer *alloc.Buffer
|
|
|
|
cached bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBufferedWriter(rawWriter io.Writer) *BufferedWriter {
|
|
|
|
return &BufferedWriter{
|
|
|
|
writer: rawWriter,
|
|
|
|
buffer: alloc.NewBuffer().Clear(),
|
|
|
|
cached: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-01 19:17:44 +00:00
|
|
|
func (this *BufferedWriter) ReadFrom(reader io.Reader) (int64, error) {
|
|
|
|
this.Lock()
|
|
|
|
defer this.Unlock()
|
|
|
|
|
|
|
|
if this.writer == nil {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
totalBytes := int64(0)
|
|
|
|
for {
|
|
|
|
nBytes, err := this.buffer.FillFrom(reader)
|
2016-06-01 20:09:12 +00:00
|
|
|
totalBytes += int64(nBytes)
|
2016-06-01 19:17:44 +00:00
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
return totalBytes, nil
|
|
|
|
}
|
|
|
|
return totalBytes, err
|
|
|
|
}
|
|
|
|
this.FlushWithoutLock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-26 22:45:33 +00:00
|
|
|
func (this *BufferedWriter) Write(b []byte) (int, error) {
|
2016-05-20 11:56:19 +00:00
|
|
|
this.Lock()
|
|
|
|
defer this.Unlock()
|
|
|
|
|
|
|
|
if this.writer == nil {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
|
2016-02-26 22:45:33 +00:00
|
|
|
if !this.cached {
|
|
|
|
return this.writer.Write(b)
|
|
|
|
}
|
|
|
|
nBytes, _ := this.buffer.Write(b)
|
|
|
|
if this.buffer.IsFull() {
|
2016-06-01 19:17:44 +00:00
|
|
|
this.FlushWithoutLock()
|
2016-02-26 22:45:33 +00:00
|
|
|
}
|
|
|
|
return nBytes, nil
|
|
|
|
}
|
|
|
|
|
2016-02-27 10:02:42 +00:00
|
|
|
func (this *BufferedWriter) Flush() error {
|
2016-05-20 11:56:19 +00:00
|
|
|
this.Lock()
|
|
|
|
defer this.Unlock()
|
2016-06-01 19:17:44 +00:00
|
|
|
|
2016-05-20 11:56:19 +00:00
|
|
|
if this.writer == nil {
|
|
|
|
return io.EOF
|
|
|
|
}
|
|
|
|
|
2016-06-01 19:17:44 +00:00
|
|
|
return this.FlushWithoutLock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *BufferedWriter) FlushWithoutLock() error {
|
2016-05-13 00:20:07 +00:00
|
|
|
defer this.buffer.Clear()
|
|
|
|
for !this.buffer.IsEmpty() {
|
|
|
|
nBytes, err := this.writer.Write(this.buffer.Value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-26 22:45:33 +00:00
|
|
|
this.buffer.SliceFrom(nBytes)
|
|
|
|
}
|
2016-05-13 00:20:07 +00:00
|
|
|
return nil
|
2016-02-26 22:45:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *BufferedWriter) Cached() bool {
|
|
|
|
return this.cached
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *BufferedWriter) SetCached(cached bool) {
|
|
|
|
this.cached = cached
|
|
|
|
if !cached && !this.buffer.IsEmpty() {
|
2016-02-27 10:02:42 +00:00
|
|
|
this.Flush()
|
2016-02-26 22:45:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *BufferedWriter) Release() {
|
2016-05-22 17:41:48 +00:00
|
|
|
this.Flush()
|
|
|
|
|
2016-05-20 11:56:19 +00:00
|
|
|
this.Lock()
|
|
|
|
defer this.Unlock()
|
|
|
|
|
2016-02-26 22:45:33 +00:00
|
|
|
this.buffer.Release()
|
2016-03-10 15:56:19 +00:00
|
|
|
this.buffer = nil
|
|
|
|
this.writer = nil
|
2016-02-26 22:45:33 +00:00
|
|
|
}
|