1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-07 02:38:10 -05:00
v2fly/common/buf/writer.go

38 lines
840 B
Go
Raw Normal View History

2016-12-09 07:17:34 -05:00
package buf
2016-12-27 14:53:29 -05:00
import "io"
2016-12-09 07:17:34 -05:00
// BufferToBytesWriter is a Writer that writes alloc.Buffer into underlying writer.
type BufferToBytesWriter struct {
writer io.Writer
}
// Write implements Writer.Write(). Write() takes ownership of the given buffer.
2017-04-15 15:07:23 -04:00
func (v *BufferToBytesWriter) Write(buffer MultiBuffer) error {
_, err := buffer.WriteTo(v.writer)
//buffer.Release()
return err
2016-12-09 07:17:34 -05:00
}
2017-02-20 04:33:35 -05:00
type bytesToBufferWriter struct {
2016-12-09 07:17:34 -05:00
writer Writer
}
2017-04-15 15:07:23 -04:00
func (w *bytesToBufferWriter) Write(payload []byte) (int, error) {
mb := NewMultiBuffer()
for p := payload; len(p) > 0; {
b := New()
nBytes, _ := b.Write(p)
p = p[nBytes:]
mb.Append(b)
}
if err := w.writer.Write(mb); err != nil {
return 0, err
2016-12-09 07:17:34 -05:00
}
2017-04-15 15:07:23 -04:00
return len(payload), nil
}
2016-12-09 07:17:34 -05:00
2017-04-15 15:07:23 -04:00
func (w *bytesToBufferWriter) WriteMulteBuffer(mb MultiBuffer) (int, error) {
return mb.Len(), w.writer.Write(mb)
2016-12-09 07:17:34 -05:00
}