summaryrefslogtreecommitdiff
path: root/src/lib/io/io.go
diff options
context:
space:
mode:
authorRob Pike <r@golang.org>2009-03-06 03:43:44 -0800
committerRob Pike <r@golang.org>2009-03-06 03:43:44 -0800
commit249703293ae456513a4bc45d2e0738be2e97fb14 (patch)
tree32be5f41a524519e1afc6f5cb35a863ab06974d7 /src/lib/io/io.go
parent5c5e8dd06dbdfa82f35a2e5fcc98933f645131ac (diff)
downloadgolang-249703293ae456513a4bc45d2e0738be2e97fb14.tar.gz
document io
R=rsc DELTA=44 (30 added, 4 deleted, 10 changed) OCL=25819 CL=25835
Diffstat (limited to 'src/lib/io/io.go')
-rw-r--r--src/lib/io/io.go26
1 files changed, 21 insertions, 5 deletions
diff --git a/src/lib/io/io.go b/src/lib/io/io.go
index e5f02eb70..37acab4c7 100644
--- a/src/lib/io/io.go
+++ b/src/lib/io/io.go
@@ -2,6 +2,11 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
+// This package provides basic interfaces to I/O primitives.
+// Its primary job is to wrap existing implementations of such primitives,
+// such as those in package os, into shared public interfaces that
+// abstract the functionality.
+// It also provides buffering primitives and some other basic operations.
package io
import (
@@ -9,35 +14,43 @@ import (
"syscall";
)
+// ErrEOF is the error returned by Readn and Copyn when they encounter EOF.
var ErrEOF = os.NewError("EOF")
+// Read is the interface that wraps the basic Read method.
type Read interface {
Read(p []byte) (n int, err *os.Error);
}
+// Write is the interface that wraps the basic Write method.
type Write interface {
Write(p []byte) (n int, err *os.Error);
}
+// Close is the interface that wraps the basic Close method.
type Close interface {
Close() *os.Error;
}
+// ReadWrite is the interface that groups the basic Read and Write methods.
type ReadWrite interface {
Read;
Write;
}
+// ReadClose is the interface that groups the basic Read and Close methods.
type ReadClose interface {
Read;
Close;
}
+// WriteClose is the interface that groups the basic Write and Close methods.
type WriteClose interface {
Write;
Close;
}
+// ReadWriteClose is the interface that groups the basic Read, Write and Close methods.
type ReadWriteClose interface {
Read;
Write;
@@ -53,11 +66,12 @@ func StringBytes(s string) []byte {
return b;
}
+// WriteString writes the contents of the string s to w, which accepts an array of bytes.
func WriteString(w Write, s string) (n int, err *os.Error) {
return w.Write(StringBytes(s))
}
-// Read until buffer is full, EOF, or error
+// Readn reads r until the buffer buf is full, or until EOF or error.
func Readn(r Read, buf []byte) (n int, err *os.Error) {
n = 0;
for n < len(buf) {
@@ -86,6 +100,8 @@ func (fr *fullRead) Read(p []byte) (n int, err *os.Error) {
return n, err
}
+// MakeFullReader takes r, an implementation of Read, and returns an object
+// that still implements Read but always calls Readn underneath.
func MakeFullReader(r Read) Read {
if fr, ok := r.(*fullRead); ok {
// already a fullRead
@@ -94,8 +110,8 @@ func MakeFullReader(r Read) Read {
return &fullRead{r}
}
-// Copies n bytes (or until EOF is reached) from src to dst.
-// Returns the number of bytes copied and the error, if any.
+// Copy n copies n bytes (or until EOF is reached) from src to dst.
+// It returns the number of bytes copied and the error, if any.
func Copyn(src Read, dst Write, n int64) (written int64, err *os.Error) {
buf := make([]byte, 32*1024);
for written < n {
@@ -130,8 +146,8 @@ func Copyn(src Read, dst Write, n int64) (written int64, err *os.Error) {
return written, err
}
-// Copies from src to dst until EOF is reached.
-// Returns the number of bytes copied and the error, if any.
+// Copy copies from src to dst until EOF is reached.
+// It returns the number of bytes copied and the error, if any.
func Copy(src Read, dst Write) (written int64, err *os.Error) {
buf := make([]byte, 32*1024);
for {