mirror of https://github.com/v2ray/v2ray-core
comments
parent
66ebeaa0ca
commit
fcc66704fa
|
@ -26,12 +26,14 @@ func (w *BufferToBytesWriter) WriteMultiBuffer(mb MultiBuffer) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadFrom implements io.ReaderFrom.
|
||||||
func (w *BufferToBytesWriter) ReadFrom(reader io.Reader) (int64, error) {
|
func (w *BufferToBytesWriter) ReadFrom(reader io.Reader) (int64, error) {
|
||||||
var sc SizeCounter
|
var sc SizeCounter
|
||||||
err := Copy(NewReader(reader), w, CountSize(&sc))
|
err := Copy(NewReader(reader), w, CountSize(&sc))
|
||||||
return sc.Size, err
|
return sc.Size, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BufferedWriter is a Writer with internal buffer.
|
||||||
type BufferedWriter struct {
|
type BufferedWriter struct {
|
||||||
writer Writer
|
writer Writer
|
||||||
legacyWriter io.Writer
|
legacyWriter io.Writer
|
||||||
|
@ -39,6 +41,7 @@ type BufferedWriter struct {
|
||||||
buffered bool
|
buffered bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewBufferedWriter creates a new BufferedWriter.
|
||||||
func NewBufferedWriter(writer Writer) *BufferedWriter {
|
func NewBufferedWriter(writer Writer) *BufferedWriter {
|
||||||
w := &BufferedWriter{
|
w := &BufferedWriter{
|
||||||
writer: writer,
|
writer: writer,
|
||||||
|
@ -51,6 +54,7 @@ func NewBufferedWriter(writer Writer) *BufferedWriter {
|
||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Write implements io.Writer.
|
||||||
func (w *BufferedWriter) Write(b []byte) (int, error) {
|
func (w *BufferedWriter) Write(b []byte) (int, error) {
|
||||||
if !w.buffered && w.legacyWriter != nil {
|
if !w.buffered && w.legacyWriter != nil {
|
||||||
return w.legacyWriter.Write(b)
|
return w.legacyWriter.Write(b)
|
||||||
|
@ -78,6 +82,7 @@ func (w *BufferedWriter) Write(b []byte) (int, error) {
|
||||||
return totalBytes, nil
|
return totalBytes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteMultiBuffer implements Writer. It takes ownership of the given MultiBuffer.
|
||||||
func (w *BufferedWriter) WriteMultiBuffer(b MultiBuffer) error {
|
func (w *BufferedWriter) WriteMultiBuffer(b MultiBuffer) error {
|
||||||
if !w.buffered {
|
if !w.buffered {
|
||||||
return w.writer.WriteMultiBuffer(b)
|
return w.writer.WriteMultiBuffer(b)
|
||||||
|
@ -99,6 +104,7 @@ func (w *BufferedWriter) WriteMultiBuffer(b MultiBuffer) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Flush flushes buffered content into underlying writer.
|
||||||
func (w *BufferedWriter) Flush() error {
|
func (w *BufferedWriter) Flush() error {
|
||||||
if !w.buffer.IsEmpty() {
|
if !w.buffer.IsEmpty() {
|
||||||
if err := w.writer.WriteMultiBuffer(NewMultiBufferValue(w.buffer)); err != nil {
|
if err := w.writer.WriteMultiBuffer(NewMultiBufferValue(w.buffer)); err != nil {
|
||||||
|
|
Loading…
Reference in New Issue