2016-12-09 07:17:34 -05:00
|
|
|
package buf
|
|
|
|
|
2017-04-16 16:30:29 -04: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-17 16:35:20 -04:00
|
|
|
func (w *BufferToBytesWriter) Write(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-04-17 16:35:20 -04:00
|
|
|
_, err := bs.WriteTo(w.writer)
|
2017-04-15 15:07:23 -04:00
|
|
|
return err
|
2016-12-09 07:17:34 -05:00
|
|
|
}
|
|
|
|
|
2017-04-19 15:27:21 -04:00
|
|
|
type writerAdapter struct {
|
|
|
|
writer MultiBufferWriter
|
|
|
|
}
|
|
|
|
|
2017-04-21 09:36:05 -04:00
|
|
|
// Write implements buf.MultiBufferWriter.
|
2017-04-19 15:27:21 -04:00
|
|
|
func (w *writerAdapter) Write(mb MultiBuffer) error {
|
2017-04-23 07:41:52 -04:00
|
|
|
return w.writer.WriteMultiBuffer(mb)
|
2017-04-19 15:27:21 -04:00
|
|
|
}
|
|
|
|
|
2017-04-20 05:00:15 -04:00
|
|
|
type mergingWriter struct {
|
|
|
|
writer io.Writer
|
|
|
|
buffer []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *mergingWriter) Write(mb MultiBuffer) error {
|
|
|
|
defer mb.Release()
|
|
|
|
|
|
|
|
for !mb.IsEmpty() {
|
|
|
|
nBytes, _ := mb.Read(w.buffer)
|
|
|
|
if _, err := w.writer.Write(w.buffer[:nBytes]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-21 08:51:09 -04:00
|
|
|
type seqWriter struct {
|
|
|
|
writer io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *seqWriter) Write(mb MultiBuffer) error {
|
|
|
|
defer mb.Release()
|
|
|
|
|
|
|
|
for _, b := range mb {
|
|
|
|
if b.IsEmpty() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, err := w.writer.Write(b.Bytes()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-23 07:41:52 -04:00
|
|
|
var (
|
|
|
|
_ MultiBufferWriter = (*bytesToBufferWriter)(nil)
|
|
|
|
)
|
|
|
|
|
2017-02-20 04:33:35 -05:00
|
|
|
type bytesToBufferWriter struct {
|
2016-12-09 07:17:34 -05:00
|
|
|
writer Writer
|
|
|
|
}
|
|
|
|
|
2017-04-21 09:36:05 -04:00
|
|
|
// Write implements io.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-23 07:41:52 -04:00
|
|
|
func (w *bytesToBufferWriter) WriteMultiBuffer(mb MultiBuffer) error {
|
|
|
|
return w.writer.Write(mb)
|
2016-12-09 07:17:34 -05:00
|
|
|
}
|
2017-04-17 16:35:20 -04:00
|
|
|
|
|
|
|
func (w *bytesToBufferWriter) ReadFrom(reader io.Reader) (int64, error) {
|
|
|
|
mbReader := NewReader(reader)
|
|
|
|
totalBytes := int64(0)
|
|
|
|
eof := false
|
|
|
|
for !eof {
|
|
|
|
mb, err := mbReader.Read()
|
|
|
|
if err == io.EOF {
|
|
|
|
eof = true
|
|
|
|
} else if err != nil {
|
|
|
|
return totalBytes, err
|
|
|
|
}
|
|
|
|
totalBytes += int64(mb.Len())
|
|
|
|
if err := w.writer.Write(mb); err != nil {
|
|
|
|
return totalBytes, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return totalBytes, nil
|
|
|
|
}
|
2017-04-27 16:20:29 -04:00
|
|
|
|
|
|
|
type noOpWriter struct{}
|
|
|
|
|
|
|
|
func (noOpWriter) Write(b MultiBuffer) error {
|
|
|
|
b.Release()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
Discard Writer = noOpWriter{}
|
|
|
|
)
|