pull/787/head
Darien Raymond 2017-11-25 21:58:47 +01:00
parent 052d2d8278
commit 41961dbd60
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
2 changed files with 16 additions and 22 deletions

View File

@ -59,20 +59,15 @@ var (
type BufferedReader struct { type BufferedReader struct {
stream Reader stream Reader
legacyReader io.Reader
leftOver MultiBuffer leftOver MultiBuffer
buffered bool buffered bool
} }
func NewBufferedReader(reader Reader) *BufferedReader { func NewBufferedReader(reader Reader) *BufferedReader {
r := &BufferedReader{ return &BufferedReader{
stream: reader, stream: reader,
buffered: true, buffered: true,
} }
if lr, ok := reader.(io.Reader); ok {
r.legacyReader = lr
}
return r
} }
func (r *BufferedReader) SetBuffered(f bool) { func (r *BufferedReader) SetBuffered(f bool) {
@ -99,8 +94,10 @@ func (r *BufferedReader) Read(b []byte) (int, error) {
return nBytes, nil return nBytes, nil
} }
if !r.buffered && r.legacyReader != nil { if !r.buffered {
return r.legacyReader.Read(b) if reader, ok := r.stream.(io.Reader); ok {
return reader.Read(b)
}
} }
mb, err := r.stream.ReadMultiBuffer() mb, err := r.stream.ReadMultiBuffer()

View File

@ -49,22 +49,17 @@ var (
// BufferedWriter is a Writer with internal buffer. // BufferedWriter is a Writer with internal buffer.
type BufferedWriter struct { type BufferedWriter struct {
writer Writer writer Writer
legacyWriter io.Writer
buffer *Buffer buffer *Buffer
buffered bool buffered bool
} }
// NewBufferedWriter creates a new BufferedWriter. // NewBufferedWriter creates a new BufferedWriter.
func NewBufferedWriter(writer Writer) *BufferedWriter { func NewBufferedWriter(writer Writer) *BufferedWriter {
w := &BufferedWriter{ return &BufferedWriter{
writer: writer, writer: writer,
buffer: New(), buffer: New(),
buffered: true, buffered: true,
} }
if lw, ok := writer.(io.Writer); ok {
w.legacyWriter = lw
}
return w
} }
func (w *BufferedWriter) WriteByte(c byte) error { func (w *BufferedWriter) WriteByte(c byte) error {
@ -74,8 +69,10 @@ func (w *BufferedWriter) WriteByte(c byte) error {
// Write implements io.Writer. // 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 {
return w.legacyWriter.Write(b) if writer, ok := w.writer.(io.Writer); ok {
return writer.Write(b)
}
} }
totalBytes := 0 totalBytes := 0