diff options
| author | Russ Cox <rsc@golang.org> | 2009-06-08 17:22:28 -0700 | 
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2009-06-08 17:22:28 -0700 | 
| commit | 2ff6c48c8398d20ccac09ab2995cbdafe2cf4f0c (patch) | |
| tree | 3d16cb366a79ce9327369c37a24b1b1bd8c7b01e | |
| parent | 2b024049ccd16176470b581d435493f25ca7bb64 (diff) | |
| download | golang-2ff6c48c8398d20ccac09ab2995cbdafe2cf4f0c.tar.gz | |
add new function io.ReadAll
R=gri
DELTA=14  (6 added, 4 deleted, 4 changed)
OCL=30072
CL=30074
| -rw-r--r-- | src/lib/io/utils.go | 16 | 
1 files changed, 9 insertions, 7 deletions
| diff --git a/src/lib/io/utils.go b/src/lib/io/utils.go index 736097946..a4cbb2d9a 100644 --- a/src/lib/io/utils.go +++ b/src/lib/io/utils.go @@ -11,17 +11,19 @@ import (  	"os";  ) +// ReadAll reads from r until an error or EOF and returns the data it read. +func ReadAll(r Reader) ([]byte, os.Error) { +	var buf ByteBuffer; +	n, err := io.Copy(r, &buf); +	return buf.Data(), err; +} -// ReadFile reads the file named by filename and returns -// its contents if successful. -// +// ReadFile reads the file named by filename and returns the contents.  func ReadFile(filename string) ([]byte, os.Error) {  	f, err := os.Open(filename, os.O_RDONLY, 0);  	if err != nil {  		return nil, err;  	} -	var b io.ByteBuffer; -	_, err := io.Copy(f, &b); -	f.Close(); -	return b.Data(), err; +	defer f.Close(); +	return ReadAll(f);  } | 
