From c264c87335ff4b3111d43f830dbe37eac1509f2e Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Thu, 22 Jan 2015 14:15:47 -0800 Subject: bytes, strings: add Reader.Size methods As noted on recently on golang-nuts, there's currently no way to know the total size of a strings.Reader or bytes.Reader when using ReadAt on them. Most callers resort to wrapping it in an io.SectionReader to retain that information. The SizeReaderAt abstraction (an io.ReaderAt with a Size() int64 method) has proven useful as a way of expressing a concurrency-safe read-only number of bytes. As one example, see http://talks.golang.org/2013/oscon-dl.slide#49 and the rest of that presentation for its use in dl.google.com. SizeReaderAt is also used in the open source google-api-go-client, and within Google's internal codebase, where it exists in a public package created in 2013 with the package comment: "These may migrate to the standard library after we have enough experience with their feel." I'm still as happy with the SizeReaderAt abstraction and its composabilty as I was in 2013, so I'd like to make these two Readers also be SizeReaderAts. Fixes #9667 Change-Id: Ie6f145ada419dd116280472d8c029f046d5edf70 Reviewed-on: https://go-review.googlesource.com/3199 Reviewed-by: Andrew Gerrand Reviewed-by: Russ Cox Reviewed-by: Rob Pike --- src/bytes/reader.go | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/bytes/reader.go') diff --git a/src/bytes/reader.go b/src/bytes/reader.go index d2d40fa7ca..b89d1548f1 100644 --- a/src/bytes/reader.go +++ b/src/bytes/reader.go @@ -29,6 +29,12 @@ func (r *Reader) Len() int { return int(int64(len(r.s)) - r.i) } +// Size returns the original length of the underlying byte slice. +// Size is the number of bytes available for reading via ReadAt. +// The returned value is always the same and is not affected by calls +// to any other method. +func (r *Reader) Size() int64 { return int64(len(r.s)) } + func (r *Reader) Read(b []byte) (n int, err error) { if len(b) == 0 { return 0, nil -- cgit v1.3-5-g9baa