-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #178 from gustavosbarreto/limit_reader
installifdifferent: use limit reader if object has explicit size
- Loading branch information
Showing
6 changed files
with
115 additions
and
5 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
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,35 @@ | ||
package utils | ||
|
||
import ( | ||
"io" | ||
"reflect" | ||
) | ||
|
||
type LimitedReader struct { | ||
R io.ReadSeeker | ||
N int64 | ||
} | ||
|
||
func (l *LimitedReader) Read(p []byte) (n int, err error) { | ||
if l.N <= 0 { | ||
return 0, io.EOF | ||
} | ||
|
||
if int64(len(p)) > l.N { | ||
p = p[0:l.N] | ||
} | ||
|
||
n, err = l.R.Read(p) | ||
l.N -= int64(n) | ||
|
||
return | ||
} | ||
|
||
func (l *LimitedReader) Seek(offset int64, whence int) (int64, error) { | ||
l.N = reflect.ValueOf(l.R).Elem().FieldByName("i").Int() | ||
return l.R.Seek(offset, whence) | ||
} | ||
|
||
func LimitReader(r io.ReadSeeker, n int64) io.ReadSeeker { | ||
return &LimitedReader{R: r, N: n} | ||
} |
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,65 @@ | ||
package utils | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLimitReader(t *testing.T) { | ||
n := int64(3) | ||
rd := bytes.NewReader([]byte("bytes")) | ||
lr := LimitReader(rd, n) | ||
|
||
assert.Equal(t, rd, lr.(*LimitedReader).R) | ||
assert.Equal(t, n, lr.(*LimitedReader).N) | ||
} | ||
|
||
func TestLimitReaderRead(t *testing.T) { | ||
n := int64(3) | ||
rd := bytes.NewReader([]byte("bytes")) | ||
lr := LimitReader(rd, n) | ||
|
||
limitedBytes, err := ioutil.ReadAll(lr) | ||
assert.NoError(t, err) | ||
|
||
bytes := make([]byte, n) | ||
|
||
rd.Seek(0, io.SeekStart) | ||
|
||
_, err = rd.Read(bytes) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, int64(0), lr.(*LimitedReader).N) | ||
assert.Equal(t, bytes, limitedBytes) | ||
} | ||
|
||
func TestLimitReaderSeek(t *testing.T) { | ||
n := int64(3) | ||
skip := int64(1) | ||
rd := bytes.NewReader([]byte("bytes")) | ||
lr := LimitReader(rd, n) | ||
|
||
_, err := ioutil.ReadAll(lr) | ||
assert.NoError(t, err) | ||
|
||
lr.Seek(skip, io.SeekStart) | ||
|
||
assert.Equal(t, n, lr.(*LimitedReader).N) | ||
|
||
limitedBytes, err := ioutil.ReadAll(lr) | ||
assert.NoError(t, err) | ||
|
||
bytes := make([]byte, n) | ||
|
||
rd.Seek(skip, io.SeekStart) | ||
|
||
_, err = rd.Read(bytes) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, int64(0), lr.(*LimitedReader).N) | ||
assert.Equal(t, bytes, limitedBytes) | ||
} |