-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wsflate: avoid double buffering when using flate.Reader (#137)
This commit makes wsflate.suffixedReader to optionally implement io.ByteReader. That is, if source object is buffered (bytes.Reader, bufio.Reader etc.) and implements io.ByteReader, but suffixedReader not, then flate.NewReader() will allocate new bufio.Reader for it. This commit fixes it and adds runtime checks on suffixedReader source to hide ReadByte() method if source is a pure io.Reader. Fixes #130
- Loading branch information
Showing
3 changed files
with
80 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,37 @@ | ||
package wsflate | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"testing" | ||
) | ||
|
||
func TestSuffixedReaderIface(t *testing.T) { | ||
for _, test := range []struct { | ||
src io.Reader | ||
exp bool | ||
}{ | ||
{ | ||
src: bytes.NewReader(nil), | ||
exp: true, | ||
}, | ||
{ | ||
src: io.TeeReader(nil, nil), | ||
exp: false, | ||
}, | ||
} { | ||
t.Run(fmt.Sprintf("%T", test.src), func(t *testing.T) { | ||
isByteReader := func(r io.Reader) bool { | ||
_, ok := r.(io.ByteReader) | ||
return ok | ||
} | ||
s := &suffixedReader{ | ||
r: test.src, | ||
} | ||
if act, exp := isByteReader(s.iface()), test.exp; act != exp { | ||
t.Fatalf("unexpected io.ByteReader: %t; want %t", act, exp) | ||
} | ||
}) | ||
} | ||
} |