mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-23 00:47:56 +01:00
Have rac favor io.ReaderAt over io.ReadSeeker
This commit is contained in:
1 parent
62211056f8
commit
9394364a47
5 files changed
+228
-96
No files matched your search
+7
-24
@@ -42,24 +42,6 @@ func u32BE(b []byte) uint32 {
|
||||
return (uint32(b[0]) << 24) | (uint32(b[1]) << 16) | (uint32(b[2]) << 8) | (uint32(b[3]))
|
||||
}
|
||||
|
||||
// readAt calls ReadAt if it is available, otherwise it falls back to calling
|
||||
// Seek and then ReadFull. Calling ReadAt is presumably slightly more
|
||||
// efficient, e.g. one syscall instead of two.
|
||||
func readAt(r io.ReadSeeker, p []byte, offset int64) error {
|
||||
if a, ok := r.(io.ReaderAt); ok {
|
||||
n, err := a.ReadAt(p, offset)
|
||||
if (n == len(p)) && (err == io.EOF) {
|
||||
err = nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
if _, err := r.Seek(offset, io.SeekStart); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := io.ReadFull(r, p)
|
||||
return err
|
||||
}
|
||||
|
||||
// suffix32K returns the last 32 KiB of b, or if b is shorter than that, it
|
||||
// returns just b.
|
||||
//
|
||||
@@ -111,9 +93,7 @@ func (r *CodecReader) MakeDecompressor(
|
||||
}
|
||||
|
||||
// MakeReaderContext implements rac.CodecReader.
|
||||
func (r *CodecReader) MakeReaderContext(
|
||||
rs io.ReadSeeker, compressedSize int64, chunk rac.Chunk) (rac.ReaderContext, error) {
|
||||
|
||||
func (r *CodecReader) MakeReaderContext(rs io.ReadSeeker, chunk rac.Chunk) (rac.ReaderContext, error) {
|
||||
// For a description of the RAC+Zlib secondary-data format, see
|
||||
// https://github.com/google/wuffs/blob/master/doc/spec/rac-spec.md#rac--zlib
|
||||
|
||||
@@ -131,12 +111,15 @@ func (r *CodecReader) MakeReaderContext(
|
||||
}
|
||||
|
||||
// Check the cRange size and the tTag.
|
||||
if (cRange.Size() < 6) || (cRange[1] > compressedSize) || (chunk.TTag != 0xFF) {
|
||||
if (cRange.Size() < 6) || (chunk.TTag != 0xFF) {
|
||||
return rac.ReaderContext{}, errInvalidDictionary
|
||||
}
|
||||
|
||||
// Read the dictionary size.
|
||||
if err := readAt(rs, r.buf[:2], cRange[0]); err != nil {
|
||||
if _, err := rs.Seek(cRange[0], io.SeekStart); err != nil {
|
||||
return rac.ReaderContext{}, errInvalidDictionary
|
||||
}
|
||||
if _, err := io.ReadFull(rs, r.buf[:2]); err != nil {
|
||||
return rac.ReaderContext{}, errInvalidDictionary
|
||||
}
|
||||
dictSize := int64(r.buf[0]) | (int64(r.buf[1]) << 8)
|
||||
@@ -158,7 +141,7 @@ func (r *CodecReader) MakeReaderContext(
|
||||
}
|
||||
|
||||
// Read the dictionary and checksum.
|
||||
if err := readAt(rs, buffer, cRange[0]+2); err != nil {
|
||||
if _, err := io.ReadFull(rs, buffer); err != nil {
|
||||
return rac.ReaderContext{}, errInvalidDictionary
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ import (
|
||||
"fmt"
|
||||
"hash/crc32"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/wuffs/lib/rac"
|
||||
@@ -273,3 +274,52 @@ func TestSharedDictionary(t *testing.T) {
|
||||
t.Fatalf("ratio: got %dx, want at least 4x", ratio)
|
||||
}
|
||||
}
|
||||
|
||||
// rsSansReadAt wraps a strings.Reader to have only Read and Seek methods.
|
||||
type rsSansReadAt struct {
|
||||
r *strings.Reader
|
||||
}
|
||||
|
||||
func (r *rsSansReadAt) Read(p []byte) (int, error) { return r.r.Read(p) }
|
||||
func (r *rsSansReadAt) Seek(o int64, w int) (int64, error) { return r.r.Seek(o, w) }
|
||||
|
||||
// rsWithReadAt wraps a strings.Reader to have Read, Seek and ReadAt methods.
|
||||
// Technically, it satisfies the io.ReadSeeker interface, but calling Read or
|
||||
// Seek will panic, other than Seek'ing to the end of the data.
|
||||
type rsWithReadAt struct {
|
||||
r *strings.Reader
|
||||
}
|
||||
|
||||
func (r *rsWithReadAt) Read(p []byte) (int, error) { panic("unimplemented") }
|
||||
func (r *rsWithReadAt) Seek(o int64, w int) (int64, error) {
|
||||
if (o == 0) && (w == io.SeekEnd) {
|
||||
return r.r.Seek(o, w)
|
||||
}
|
||||
panic("unimplemented")
|
||||
}
|
||||
func (r *rsWithReadAt) ReadAt(p []byte, o int64) (int, error) { return r.r.ReadAt(p, o) }
|
||||
|
||||
// testReadSeeker tests that decoding from rs works, regardless of whether rs
|
||||
// implements the optional ReadAt method. If rs does implement io.ReaderAt then
|
||||
// its Read method should never be called.
|
||||
func testReadSeeker(t *testing.T, rs io.ReadSeeker) {
|
||||
buf := &bytes.Buffer{}
|
||||
r := &rac.Reader{
|
||||
ReadSeeker: rs,
|
||||
CodecReaders: []rac.CodecReader{&CodecReader{}},
|
||||
}
|
||||
if _, err := io.Copy(buf, r); err != nil {
|
||||
t.Fatalf("io.Copy: %v", err)
|
||||
}
|
||||
if got, want := buf.String(), decodedSheep; got != want {
|
||||
t.Fatalf("got:\n%s\nwant:\n%s", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadSeekerSansReadAt(t *testing.T) {
|
||||
testReadSeeker(t, &rsSansReadAt{strings.NewReader(encodedSheep)})
|
||||
}
|
||||
|
||||
func TestReadSeekerWithReadAt(t *testing.T) {
|
||||
testReadSeeker(t, &rsWithReadAt{strings.NewReader(encodedSheep)})
|
||||
}
|
||||
Reference in new issue
Block a user