1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 10:08:15 -05:00
This commit is contained in:
Darien Raymond 2016-12-27 23:09:08 +01:00
parent c347e50c28
commit 22fa151391
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
2 changed files with 7 additions and 3 deletions

View File

@ -25,6 +25,7 @@ func NewWriter(rawWriter io.Writer) *BufferedWriter {
}
}
// ReadFrom implements io.ReaderFrom.ReadFrom().
func (v *BufferedWriter) ReadFrom(reader io.Reader) (int64, error) {
totalBytes := int64(0)
for {
@ -65,6 +66,7 @@ func (v *BufferedWriter) Write(b []byte) (int, error) {
return len(b), nil
}
// Flush writes all buffered content into underlying writer, if any.
func (v *BufferedWriter) Flush() error {
defer v.buffer.Clear()
for !v.buffer.IsEmpty() {
@ -77,10 +79,12 @@ func (v *BufferedWriter) Flush() error {
return nil
}
func (v *BufferedWriter) Buffered() bool {
// IsBuffered returns true if this BufferedWriter holds a buffer.
func (v *BufferedWriter) IsBuffered() bool {
return v.buffered
}
// SetBuffered controls whether the BufferedWriter holds a buffer for writing. If not buffered, any write() calls into underlying writer directly.
func (v *BufferedWriter) SetBuffered(cached bool) {
v.buffered = cached
if !cached && !v.buffer.IsEmpty() {

View File

@ -15,7 +15,7 @@ func TestBufferedWriter(t *testing.T) {
content := buf.New()
writer := NewWriter(content)
assert.Bool(writer.Buffered()).IsTrue()
assert.Bool(writer.IsBuffered()).IsTrue()
payload := make([]byte, 16)
@ -35,7 +35,7 @@ func TestBufferedWriterLargePayload(t *testing.T) {
content := buf.NewLocal(128 * 1024)
writer := NewWriter(content)
assert.Bool(writer.Buffered()).IsTrue()
assert.Bool(writer.IsBuffered()).IsTrue()
payload := make([]byte, 64*1024)
rand.Read(payload)