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

216 lines
4.2 KiB
Go
Raw Normal View History

2016-12-09 07:17:34 -05:00
package buf
2017-11-03 20:33:35 -04:00
import (
"io"
"v2ray.com/core/common/errors"
)
2016-12-09 07:17:34 -05:00
var (
_ io.ReaderFrom = (*BufferToBytesWriter)(nil)
_ io.Writer = (*BufferToBytesWriter)(nil)
_ Writer = (*BufferToBytesWriter)(nil)
)
2016-12-09 07:17:34 -05:00
// BufferToBytesWriter is a Writer that writes alloc.Buffer into underlying writer.
type BufferToBytesWriter struct {
2017-11-09 16:33:15 -05:00
io.Writer
}
func NewBufferToBytesWriter(writer io.Writer) *BufferToBytesWriter {
return &BufferToBytesWriter{
Writer: writer,
}
2016-12-09 07:17:34 -05:00
}
2017-11-09 16:33:15 -05:00
// WriteMultiBuffer implements Writer. This method takes ownership of the given buffer.
func (w *BufferToBytesWriter) WriteMultiBuffer(mb MultiBuffer) error {
2017-04-15 15:50:23 -04:00
defer mb.Release()
2017-04-16 16:30:29 -04:00
bs := mb.ToNetBuffers()
2017-11-09 16:33:15 -05:00
_, err := bs.WriteTo(w)
2017-04-15 15:07:23 -04:00
return err
2016-12-09 07:17:34 -05:00
}
2017-11-21 10:55:22 -05:00
// ReadFrom implements io.ReaderFrom.
2017-11-09 16:33:15 -05:00
func (w *BufferToBytesWriter) ReadFrom(reader io.Reader) (int64, error) {
var sc SizeCounter
err := Copy(NewReader(reader), w, CountSize(&sc))
return sc.Size, err
2017-04-19 15:27:21 -04:00
}
var (
_ io.ReaderFrom = (*BufferedWriter)(nil)
_ io.Writer = (*BufferedWriter)(nil)
_ Writer = (*BufferedWriter)(nil)
_ io.ByteWriter = (*BufferedWriter)(nil)
)
2017-11-21 10:55:22 -05:00
// BufferedWriter is a Writer with internal buffer.
2017-11-09 16:33:15 -05:00
type BufferedWriter struct {
writer Writer
legacyWriter io.Writer
buffer *Buffer
buffered bool
2017-04-19 15:27:21 -04:00
}
2017-11-21 10:55:22 -05:00
// NewBufferedWriter creates a new BufferedWriter.
2017-11-09 16:33:15 -05:00
func NewBufferedWriter(writer Writer) *BufferedWriter {
w := &BufferedWriter{
writer: writer,
buffer: New(),
buffered: true,
}
if lw, ok := writer.(io.Writer); ok {
w.legacyWriter = lw
}
return w
2017-04-20 05:00:15 -04:00
}
func (w *BufferedWriter) WriteByte(c byte) error {
_, err := w.Write([]byte{c})
return err
}
2017-11-21 10:55:22 -05:00
// Write implements io.Writer.
2017-11-09 16:33:15 -05:00
func (w *BufferedWriter) Write(b []byte) (int, error) {
if !w.buffered && w.legacyWriter != nil {
return w.legacyWriter.Write(b)
}
2017-04-20 05:00:15 -04:00
2017-11-09 16:33:15 -05:00
totalBytes := 0
for len(b) > 0 {
2017-11-17 04:18:12 -05:00
if w.buffer == nil {
w.buffer = New()
}
2017-11-09 16:33:15 -05:00
nBytes, err := w.buffer.Write(b)
totalBytes += nBytes
if err != nil {
return totalBytes, err
}
2017-11-15 17:38:52 -05:00
if !w.buffered || w.buffer.IsFull() {
2017-11-09 16:33:15 -05:00
if err := w.Flush(); err != nil {
return totalBytes, err
}
2017-04-20 05:00:15 -04:00
}
2017-11-09 16:33:15 -05:00
b = b[nBytes:]
2017-04-20 05:00:15 -04:00
}
2017-11-15 17:38:52 -05:00
2017-11-09 16:33:15 -05:00
return totalBytes, nil
2017-04-20 05:00:15 -04:00
}
2017-11-21 10:55:22 -05:00
// WriteMultiBuffer implements Writer. It takes ownership of the given MultiBuffer.
2017-11-09 16:33:15 -05:00
func (w *BufferedWriter) WriteMultiBuffer(b MultiBuffer) error {
if !w.buffered {
return w.writer.WriteMultiBuffer(b)
}
2017-04-21 08:51:09 -04:00
2017-11-09 16:33:15 -05:00
defer b.Release()
2017-04-21 08:51:09 -04:00
2017-11-09 16:33:15 -05:00
for !b.IsEmpty() {
if err := w.buffer.AppendSupplier(ReadFrom(&b)); err != nil {
2017-04-21 08:51:09 -04:00
return err
}
2017-11-09 16:33:15 -05:00
if w.buffer.IsFull() {
if err := w.Flush(); err != nil {
return err
}
}
2017-04-21 08:51:09 -04:00
}
return nil
}
2017-11-21 10:55:22 -05:00
// Flush flushes buffered content into underlying writer.
2017-11-09 16:33:15 -05:00
func (w *BufferedWriter) Flush() error {
if !w.buffer.IsEmpty() {
if err := w.writer.WriteMultiBuffer(NewMultiBufferValue(w.buffer)); err != nil {
return err
}
2017-04-23 07:41:52 -04:00
2017-11-09 16:33:15 -05:00
if w.buffered {
w.buffer = New()
} else {
w.buffer = nil
}
}
return nil
2016-12-09 07:17:34 -05:00
}
2017-11-09 16:33:15 -05:00
func (w *BufferedWriter) SetBuffered(f bool) error {
w.buffered = f
if !f {
return w.Flush()
2016-12-09 07:17:34 -05:00
}
2017-11-09 16:33:15 -05:00
return nil
2017-04-15 15:07:23 -04:00
}
2016-12-09 07:17:34 -05:00
2017-11-15 05:23:25 -05:00
// ReadFrom implements io.ReaderFrom.
2017-11-09 16:33:15 -05:00
func (w *BufferedWriter) ReadFrom(reader io.Reader) (int64, error) {
if err := w.SetBuffered(false); err != nil {
return 0, err
2017-11-09 16:33:15 -05:00
}
var sc SizeCounter
2017-11-09 16:33:15 -05:00
err := Copy(NewReader(reader), w, CountSize(&sc))
return sc.Size, err
2016-12-09 07:17:34 -05:00
}
2017-04-17 16:35:20 -04:00
2017-11-09 16:33:15 -05:00
type seqWriter struct {
writer io.Writer
}
func (w *seqWriter) WriteMultiBuffer(mb MultiBuffer) error {
defer mb.Release()
for _, b := range mb {
if b.IsEmpty() {
continue
2017-04-17 16:35:20 -04:00
}
2017-11-09 16:33:15 -05:00
if _, err := w.writer.Write(b.Bytes()); err != nil {
return err
2017-04-17 16:35:20 -04:00
}
}
2017-11-09 16:33:15 -05:00
return nil
2017-04-17 16:35:20 -04:00
}
2017-04-27 16:20:29 -04:00
type noOpWriter struct{}
2017-11-09 16:33:15 -05:00
func (noOpWriter) WriteMultiBuffer(b MultiBuffer) error {
2017-04-27 16:20:29 -04:00
b.Release()
return nil
}
2017-11-03 20:33:35 -04:00
type noOpBytesWriter struct{}
func (noOpBytesWriter) Write(b []byte) (int, error) {
return len(b), nil
}
func (noOpBytesWriter) ReadFrom(reader io.Reader) (int64, error) {
b := New()
defer b.Release()
totalBytes := int64(0)
for {
err := b.Reset(ReadFrom(reader))
totalBytes += int64(b.Len())
if err != nil {
if errors.Cause(err) == io.EOF {
return totalBytes, nil
}
return totalBytes, err
}
}
}
2017-04-27 16:20:29 -04:00
var (
2017-11-08 12:14:44 -05:00
// Discard is a Writer that swallows all contents written in.
Discard Writer = noOpWriter{}
// DiscardBytes is an io.Writer that swallows all contents written in.
2017-11-03 20:33:35 -04:00
DiscardBytes io.Writer = noOpBytesWriter{}
2017-04-27 16:20:29 -04:00
)