Skip to content

Commit

Permalink
bufiox
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Feb 25, 2024
1 parent e366ec8 commit bba26cc
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
21 changes: 18 additions & 3 deletions bufiox/bufio.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2020 Qiniu Limited (qiniu.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package bufiox

import (
Expand All @@ -10,13 +26,13 @@ import (

// -----------------------------------------------------------------------------

type nilReaderImpl int
type nilReaderImpl struct{}

func (p nilReaderImpl) Read(b []byte) (n int, err error) {
return 0, io.EOF
}

var nilReader io.Reader = nilReaderImpl(0)
var nilReader io.Reader = nilReaderImpl{}

// -----------------------------------------------------------------------------

Expand Down Expand Up @@ -82,7 +98,6 @@ var ErrSeekUnsupported = errors.New("bufio: the underlying reader doesn't suppor
// 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 Seek(b *bufio.Reader, offset int64, whence int) (int64, error) {
r := getUnderlyingReader(b)
if seeker, ok := r.(io.Seeker); ok {
Expand Down
27 changes: 24 additions & 3 deletions bufiox/bufio_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2020 Qiniu Limited (qiniu.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package bufiox

import (
Expand All @@ -13,12 +29,17 @@ import (

func TestReaderSize(t *testing.T) {
b1 := NewReaderBuffer(nil)
size1 := unsafe.Sizeof(*b1)
b2 := bufio.NewReader(os.Stdin)
size2 := unsafe.Sizeof(*b2)
if size1 != size2 {
_ = b2
if unsafe.Sizeof(*b1) != unsafe.Sizeof(*b2) {
t.Fatal("TestReaderSize: sizeof(bufiox.Reader) != sizeof(bufio.Reader)")
}
if Buffer(b1) != nil {
t.Fatal("Buffer not nil")
}
if !IsReaderBuffer(b1) {
t.Fatal("not IsReaderBuffer?")
}
b, err := ReadAll(b1)
if err != nil || b != nil {
t.Fatal("ReadAll failed:", err, b)
Expand Down
18 changes: 16 additions & 2 deletions bufiox/seek.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2020 Qiniu Limited (qiniu.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package bufiox

import (
Expand All @@ -24,7 +40,6 @@ func NewReader(rd io.ReadSeeker) *Reader {
// 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 {
b, ok := rd.(*Reader)
if ok && b.Size() >= size {
Expand All @@ -39,7 +54,6 @@ func NewReaderSize(rd io.ReadSeeker, size int) *Reader {
// 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 (r *Reader) Seek(offset int64, whence int) (int64, error) {
return Seek(&r.Reader, offset, whence)
}
Expand Down

0 comments on commit bba26cc

Please sign in to comment.