mirror of
https://github.com/v2fly/v2ray-core.git
synced 2026-01-03 15:55:20 -05:00
Massive fixes
This commit is contained in:
@@ -69,12 +69,14 @@ func (b *Buffer) Bytes() []byte {
|
||||
|
||||
// Slice cuts the buffer at the given position.
|
||||
func (b *Buffer) Slice(from, to int) *Buffer {
|
||||
b.offset += from
|
||||
b.Value = b.Value[from:to]
|
||||
return b
|
||||
}
|
||||
|
||||
// SliceFrom cuts the buffer at the given position.
|
||||
func (b *Buffer) SliceFrom(from int) *Buffer {
|
||||
b.offset += from
|
||||
b.Value = b.Value[from:]
|
||||
return b
|
||||
}
|
||||
@@ -121,9 +123,10 @@ func (b *Buffer) Read(data []byte) (int, error) {
|
||||
}
|
||||
nBytes := copy(data, b.Value)
|
||||
if nBytes == b.Len() {
|
||||
b.Value = b.Value[:0]
|
||||
b.Clear()
|
||||
} else {
|
||||
b.Value = b.Value[nBytes:]
|
||||
b.offset += nBytes
|
||||
}
|
||||
return nBytes, nil
|
||||
}
|
||||
@@ -132,7 +135,9 @@ func (b *Buffer) FillFrom(reader io.Reader) (int, error) {
|
||||
begin := b.Len()
|
||||
b.Value = b.Value[:cap(b.Value)]
|
||||
nBytes, err := reader.Read(b.Value[begin:])
|
||||
b.Value = b.Value[:begin+nBytes]
|
||||
if err == nil {
|
||||
b.Value = b.Value[:begin+nBytes]
|
||||
}
|
||||
return nBytes, err
|
||||
}
|
||||
|
||||
|
||||
@@ -35,16 +35,15 @@ func (this *BufferedWriter) Write(b []byte) (int, error) {
|
||||
}
|
||||
|
||||
func (this *BufferedWriter) Flush() error {
|
||||
nBytes, err := this.writer.Write(this.buffer.Value)
|
||||
this.buffer.SliceFrom(nBytes)
|
||||
if !this.buffer.IsEmpty() {
|
||||
nBytes, err = this.writer.Write(this.buffer.Value)
|
||||
defer this.buffer.Clear()
|
||||
for !this.buffer.IsEmpty() {
|
||||
nBytes, err := this.writer.Write(this.buffer.Value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
this.buffer.SliceFrom(nBytes)
|
||||
}
|
||||
if this.buffer.IsEmpty() {
|
||||
this.buffer.Clear()
|
||||
}
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *BufferedWriter) Cached() bool {
|
||||
@@ -59,6 +58,7 @@ func (this *BufferedWriter) SetCached(cached bool) {
|
||||
}
|
||||
|
||||
func (this *BufferedWriter) Release() {
|
||||
this.Flush()
|
||||
this.buffer.Release()
|
||||
this.buffer = nil
|
||||
this.writer = nil
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
package io
|
||||
|
||||
import (
|
||||
"github.com/v2ray/v2ray-core/common/log"
|
||||
)
|
||||
|
||||
func Pipe(reader Reader, writer Writer) error {
|
||||
for {
|
||||
buffer, err := reader.Read()
|
||||
if err != nil {
|
||||
log.Debug("IO: Pipe exits as ", err)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -14,6 +19,7 @@ func Pipe(reader Reader, writer Writer) error {
|
||||
|
||||
err = writer.Write(buffer)
|
||||
if err != nil {
|
||||
log.Debug("IO: Pipe exits as ", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user