Make decompressor conditional on reader interface

This commit is contained in:
kortschak 2014-08-08 21:27:17 +09:30
parent 088e73a163
commit 844927ff1f
2 changed files with 33 additions and 17 deletions

View file

@ -217,14 +217,13 @@ const (
b2zipMagic = "BZh" b2zipMagic = "BZh"
) )
type readAtReader interface { func decompressor(r io.Reader) (io.Reader, error) {
io.Reader ra, ok := r.(io.ReaderAt)
io.ReaderAt if !ok {
} return r, nil
}
func decompressor(r readAtReader) (io.Reader, error) {
var buf [3]byte var buf [3]byte
_, err := r.ReadAt(buf[:], 0) _, err := ra.ReadAt(buf[:], 0)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -18,6 +18,8 @@ import (
"bytes" "bytes"
"compress/bzip2" "compress/bzip2"
"compress/gzip" "compress/gzip"
"io"
"strings"
"sync" "sync"
"testing" "testing"
"time" "time"
@ -419,62 +421,77 @@ func BenchmarkKeanuBullockOther(b *testing.B) {
runBench(8, b) runBench(8, b)
} }
// reader is a test helper to filter non-io.Reader methods from the contained io.Reader.
type reader struct {
r io.Reader
}
func (r reader) Read(p []byte) (int, error) {
return r.r.Read(p)
}
var testDecompressor = []struct { var testDecompressor = []struct {
message string message string
input []byte input io.Reader
expect []byte expect []byte
err error err error
readErr error readErr error
}{ }{
{ {
message: "text input", message: "text input",
input: []byte("cayley data\n"), input: strings.NewReader("cayley data\n"),
err: nil, err: nil,
expect: []byte("cayley data\n"), expect: []byte("cayley data\n"),
readErr: nil, readErr: nil,
}, },
{ {
message: "gzip input", message: "gzip input",
input: []byte{ input: bytes.NewReader([]byte{
0x1f, 0x8b, 0x08, 0x00, 0x5c, 0xbc, 0xcd, 0x53, 0x00, 0x03, 0x4b, 0x4e, 0xac, 0xcc, 0x49, 0xad, 0x1f, 0x8b, 0x08, 0x00, 0x5c, 0xbc, 0xcd, 0x53, 0x00, 0x03, 0x4b, 0x4e, 0xac, 0xcc, 0x49, 0xad,
0x54, 0x48, 0x49, 0x2c, 0x49, 0xe4, 0x02, 0x00, 0x03, 0xe1, 0xfc, 0xc3, 0x0c, 0x00, 0x00, 0x00, 0x54, 0x48, 0x49, 0x2c, 0x49, 0xe4, 0x02, 0x00, 0x03, 0xe1, 0xfc, 0xc3, 0x0c, 0x00, 0x00, 0x00,
}, }),
err: nil, err: nil,
expect: []byte("cayley data\n"), expect: []byte("cayley data\n"),
readErr: nil, readErr: nil,
}, },
{ {
message: "bzip2 input", message: "bzip2 input",
input: []byte{ input: bytes.NewReader([]byte{
0x42, 0x5a, 0x68, 0x39, 0x31, 0x41, 0x59, 0x26, 0x53, 0x59, 0xb5, 0x4b, 0xe3, 0xc4, 0x00, 0x00, 0x42, 0x5a, 0x68, 0x39, 0x31, 0x41, 0x59, 0x26, 0x53, 0x59, 0xb5, 0x4b, 0xe3, 0xc4, 0x00, 0x00,
0x02, 0xd1, 0x80, 0x00, 0x10, 0x40, 0x00, 0x2e, 0x04, 0x04, 0x20, 0x20, 0x00, 0x31, 0x06, 0x4c, 0x02, 0xd1, 0x80, 0x00, 0x10, 0x40, 0x00, 0x2e, 0x04, 0x04, 0x20, 0x20, 0x00, 0x31, 0x06, 0x4c,
0x41, 0x4c, 0x1e, 0xa7, 0xa9, 0x2a, 0x18, 0x26, 0xb1, 0xc2, 0xee, 0x48, 0xa7, 0x0a, 0x12, 0x16, 0x41, 0x4c, 0x1e, 0xa7, 0xa9, 0x2a, 0x18, 0x26, 0xb1, 0xc2, 0xee, 0x48, 0xa7, 0x0a, 0x12, 0x16,
0xa9, 0x7c, 0x78, 0x80, 0xa9, 0x7c, 0x78, 0x80,
}, }),
err: nil, err: nil,
expect: []byte("cayley data\n"), expect: []byte("cayley data\n"),
readErr: nil, readErr: nil,
}, },
{ {
message: "bad gzip input", message: "bad gzip input",
input: []byte{0x1f, 0x8b, 'c', 'a', 'y', 'l', 'e', 'y', ' ', 'd', 'a', 't', 'a', '\n'}, input: strings.NewReader("\x1f\x8bcayley data\n"),
err: gzip.ErrHeader, err: gzip.ErrHeader,
expect: nil, expect: nil,
readErr: nil, readErr: nil,
}, },
{ {
message: "bad bzip2 input", message: "bad bzip2 input",
input: []byte{0x42, 0x5a, 0x68, 'c', 'a', 'y', 'l', 'e', 'y', ' ', 'd', 'a', 't', 'a', '\n'}, input: strings.NewReader("\x42\x5a\x68cayley data\n"),
err: nil, err: nil,
expect: nil, expect: nil,
readErr: bzip2.StructuralError("invalid compression level"), readErr: bzip2.StructuralError("invalid compression level"),
}, },
{
message: "gzip input without ReadAt",
input: reader{strings.NewReader("\x1f\x8bcayley data\n")},
err: nil,
expect: []byte("\x1f\x8bcayley data\n"),
readErr: nil,
},
} }
func TestDecompressor(t *testing.T) { func TestDecompressor(t *testing.T) {
for _, test := range testDecompressor { for _, test := range testDecompressor {
buf := bytes.NewReader(test.input) r, err := decompressor(test.input)
r, err := decompressor(buf)
if err != test.err { if err != test.err {
t.Fatalf("Unexpected error for %s, got:%v expect:%v", test.message, err, test.err) t.Fatalf("Unexpected error for %s, got:%v expect:%v", test.message, err, test.err)
} }