-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bufiox.NewReader/NewReaderSize: support Seek
- Loading branch information
Showing
3 changed files
with
145 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package bufiox | ||
|
||
import ( | ||
"bufio" | ||
"io" | ||
"os" | ||
"strings" | ||
"testing" | ||
"unsafe" | ||
) | ||
|
||
// ------------------------------------------------------------------------------------- | ||
|
||
func TestReaderSize(t *testing.T) { | ||
b1 := NewReaderBuffer(nil) | ||
size1 := unsafe.Sizeof(*b1) | ||
b2 := bufio.NewReader(os.Stdin) | ||
size2 := unsafe.Sizeof(*b2) | ||
if size1 != size2 { | ||
t.Fatal("TestReaderSize: sizeof(bufiox.Reader) != sizeof(bufio.Reader)") | ||
} | ||
b, err := ReadAll(b1) | ||
if err != nil || b != nil { | ||
t.Fatal("ReadAll failed:", err, b) | ||
} | ||
} | ||
|
||
func TestGetUnderlyingReaderAndSeek(t *testing.T) { | ||
r := strings.NewReader("Hello, china!!!") | ||
b := bufio.NewReader(r) | ||
if getUnderlyingReader(b) != r { | ||
t.Fatal("getUnderlyingReader failed") | ||
} | ||
b.ReadByte() | ||
r1 := getUnderlyingReader(b) | ||
b1 := bufio.NewReader(r1) | ||
if _, err1 := b1.ReadByte(); err1 != io.EOF { | ||
t.Fatal("bufio.NewReader cache?") | ||
} | ||
newoff, err := Seek(b, 7, io.SeekStart) | ||
if err != nil || newoff != 7 { | ||
t.Fatal("Seek failed:", err, newoff) | ||
} | ||
china, err := b.ReadString('!') | ||
if err != nil { | ||
t.Fatal("ReadString failed:", err) | ||
} | ||
if china != "china!" { | ||
t.Fatal("Seek failed:", china) | ||
} | ||
} | ||
|
||
// ------------------------------------------------------------------------------------- | ||
|
||
func TestSeeker(t *testing.T) { | ||
r := strings.NewReader("Hello, china!!!") | ||
b := NewReader(r) | ||
var rdseeker io.ReadSeeker = b | ||
rdseeker.Seek(7, io.SeekStart) | ||
china, err := b.ReadString('!') | ||
if err != nil { | ||
t.Fatal("ReadString failed:", err) | ||
} | ||
if china != "china!" { | ||
t.Fatal("Seek failed:", china) | ||
} | ||
b.Seek(0, io.SeekStart) | ||
|
||
b2 := NewReaderSize(r, 64) | ||
data, err := ReadAll(&b2.Reader) | ||
if err != nil || string(data) != "Hello, china!!!" { | ||
t.Fatal("ReadAll failed:", err, data) | ||
} | ||
} | ||
|
||
// ------------------------------------------------------------------------------------- |
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package bufiox | ||
|
||
import ( | ||
"bufio" | ||
"io" | ||
) | ||
|
||
// ----------------------------------------------------------------------------- | ||
|
||
// Reader class. | ||
type Reader struct { | ||
bufio.Reader | ||
} | ||
|
||
// NewReader returns a new Reader whose buffer has the default size. | ||
func NewReader(rd io.ReadSeeker) *Reader { | ||
r := bufio.NewReader(rd) | ||
return &Reader{Reader: *r} | ||
} | ||
|
||
// NewReaderSize returns a new Reader whose buffer has at least the specified | ||
// size. If the argument io.Reader is already a Reader with large enough size, | ||
// it returns the underlying Reader. | ||
// | ||
func NewReaderSize(rd io.ReadSeeker, size int) *Reader { | ||
r := bufio.NewReaderSize(rd, size) | ||
return &Reader{Reader: *r} | ||
} | ||
|
||
// Seek sets the offset for the next Read or Write to offset, interpreted | ||
// according to whence: SeekStart means relative to the start of the file, | ||
// SeekCurrent means relative to the current offset, and SeekEnd means | ||
// relative to the end. Seek returns the new offset relative to the start | ||
// of the file and an error, if any. | ||
// | ||
func (p *Reader) Seek(offset int64, whence int) (int64, error) { | ||
return Seek(&p.Reader, offset, whence) | ||
} | ||
|
||
// ----------------------------------------------------------------------------- |