1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 01:57:12 -05:00

write all content

This commit is contained in:
Darien Raymond 2018-07-27 23:35:02 +02:00
parent 8d689fb92d
commit 28ccd4ddd5
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

View File

@ -16,8 +16,22 @@ type BufferToBytesWriter struct {
func (w *BufferToBytesWriter) WriteMultiBuffer(mb MultiBuffer) error {
defer mb.Release()
size := mb.Len()
if size == 0 {
return nil
}
bs := mb.ToNetBuffers()
return common.Error2(bs.WriteTo(w.Writer))
for size > 0 {
n, err := bs.WriteTo(w.Writer)
if err != nil {
return err
}
size -= int32(n)
}
return nil
}
// ReadFrom implements io.ReaderFrom.
@ -105,23 +119,28 @@ func (w *BufferedWriter) WriteMultiBuffer(b MultiBuffer) error {
// Flush flushes buffered content into underlying writer.
func (w *BufferedWriter) Flush() error {
if !w.buffer.IsEmpty() {
b := w.buffer
w.buffer = nil
if w.buffer.IsEmpty() {
return nil
}
if writer, ok := w.writer.(io.Writer); ok {
_, err := writer.Write(b.Bytes())
b.Release()
b := w.buffer
w.buffer = nil
if writer, ok := w.writer.(io.Writer); ok {
defer b.Release()
for !b.IsEmpty() {
n, err := writer.Write(b.Bytes())
if err != nil {
return err
}
} else if err := w.writer.WriteMultiBuffer(NewMultiBufferValue(b)); err != nil {
return err
b.Advance(int32(n))
}
w.buffer = nil
return nil
}
return nil
return w.writer.WriteMultiBuffer(NewMultiBufferValue(b))
}
// SetBuffered sets whether the internal buffer is used. If set to false, Flush() will be called to clear the buffer.