2016-02-01 11:22:29 +00:00
|
|
|
package io
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/v2ray/v2ray-core/common/alloc"
|
|
|
|
)
|
|
|
|
|
2016-02-06 21:28:35 +00:00
|
|
|
// Writer extends io.Writer with alloc.Buffer.
|
2016-02-01 11:22:29 +00:00
|
|
|
type Writer interface {
|
2016-02-06 21:28:35 +00:00
|
|
|
// Write writes an alloc.Buffer into underlying writer.
|
2016-02-01 11:22:29 +00:00
|
|
|
Write(*alloc.Buffer) error
|
|
|
|
}
|
|
|
|
|
2016-02-06 21:28:35 +00:00
|
|
|
// AdaptiveWriter is a Writer that writes alloc.Buffer into underlying writer.
|
2016-02-01 11:22:29 +00:00
|
|
|
type AdaptiveWriter struct {
|
|
|
|
writer io.Writer
|
|
|
|
}
|
|
|
|
|
2016-02-06 21:28:35 +00:00
|
|
|
// NewAdaptiveWriter creates a new AdaptiveWriter.
|
2016-02-01 11:22:29 +00:00
|
|
|
func NewAdaptiveWriter(writer io.Writer) *AdaptiveWriter {
|
|
|
|
return &AdaptiveWriter{
|
|
|
|
writer: writer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-06 21:28:35 +00:00
|
|
|
// Write implements Writer.Write().
|
2016-02-01 11:22:29 +00:00
|
|
|
func (this *AdaptiveWriter) Write(buffer *alloc.Buffer) error {
|
|
|
|
nBytes, err := this.writer.Write(buffer.Value)
|
|
|
|
if nBytes < buffer.Len() {
|
|
|
|
_, err = this.writer.Write(buffer.Value[nBytes:])
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|