]> Cypherpunks repositories - gostls13.git/commitdiff
add new function io.ReadAll
authorRuss Cox <rsc@golang.org>
Tue, 9 Jun 2009 00:22:28 +0000 (17:22 -0700)
committerRuss Cox <rsc@golang.org>
Tue, 9 Jun 2009 00:22:28 +0000 (17:22 -0700)
R=gri
DELTA=14  (6 added, 4 deleted, 4 changed)
OCL=30072
CL=30074

src/lib/io/utils.go

index 736097946e229f31a6ad993e8bb937264ad06272..a4cbb2d9aa1fbfd8fab69b1229139d4f89a11bbc 100644 (file)
@@ -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);
 }