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

release buffer properly

This commit is contained in:
Darien Raymond 2017-04-15 21:50:23 +02:00
parent 47ba054ad5
commit ed7b22622c
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
2 changed files with 18 additions and 21 deletions

View File

@ -1,9 +1,6 @@
package buf
import (
"io"
"net"
)
import "io"
type MultiBufferWriter interface {
WriteMultiBuffer(MultiBuffer) (int, error)
@ -51,19 +48,6 @@ func (mb *MultiBuffer) Read(b []byte) (int, error) {
return totalBytes, nil
}
func (mb MultiBuffer) WriteTo(writer io.Writer) (int, error) {
if mw, ok := writer.(MultiBufferWriter); ok {
return mw.WriteMultiBuffer(mb)
}
bs := make([][]byte, len(mb))
for i, b := range mb {
bs[i] = b.Bytes()
}
nbs := net.Buffers(bs)
nBytes, err := nbs.WriteTo(writer)
return int(nBytes), err
}
func (mb MultiBuffer) Len() int {
size := 0
for _, b := range mb {

View File

@ -1,6 +1,9 @@
package buf
import "io"
import (
"io"
"net"
)
// BufferToBytesWriter is a Writer that writes alloc.Buffer into underlying writer.
type BufferToBytesWriter struct {
@ -8,9 +11,19 @@ type BufferToBytesWriter struct {
}
// Write implements Writer.Write(). Write() takes ownership of the given buffer.
func (v *BufferToBytesWriter) Write(buffer MultiBuffer) error {
_, err := buffer.WriteTo(v.writer)
//buffer.Release()
func (v *BufferToBytesWriter) Write(mb MultiBuffer) error {
if mw, ok := v.writer.(MultiBufferWriter); ok {
_, err := mw.WriteMultiBuffer(mb)
return err
}
defer mb.Release()
bs := make([][]byte, len(mb))
for i, b := range mb {
bs[i] = b.Bytes()
}
nbs := net.Buffers(bs)
_, err := nbs.WriteTo(v.writer)
return err
}