2008-09-12 17:42:53 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2009-03-06 04:43:44 -07:00
|
|
|
// 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.
|
2008-09-12 17:42:53 -06:00
|
|
|
package io
|
2008-11-11 18:28:36 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os";
|
|
|
|
)
|
|
|
|
|
2009-03-06 04:43:44 -07:00
|
|
|
// ErrEOF is the error returned by Readn and Copyn when they encounter EOF.
|
2009-01-20 15:40:40 -07:00
|
|
|
var ErrEOF = os.NewError("EOF")
|
2008-09-12 17:42:53 -06:00
|
|
|
|
2009-03-06 04:43:44 -07:00
|
|
|
// Read is the interface that wraps the basic Read method.
|
2009-01-20 15:40:40 -07:00
|
|
|
type Read interface {
|
2009-04-17 01:08:24 -06:00
|
|
|
Read(p []byte) (n int, err os.Error);
|
2008-09-12 17:42:53 -06:00
|
|
|
}
|
|
|
|
|
2009-03-06 04:43:44 -07:00
|
|
|
// Write is the interface that wraps the basic Write method.
|
2009-01-20 15:40:40 -07:00
|
|
|
type Write interface {
|
2009-04-17 01:08:24 -06:00
|
|
|
Write(p []byte) (n int, err os.Error);
|
2008-09-12 17:42:53 -06:00
|
|
|
}
|
|
|
|
|
2009-03-06 04:43:44 -07:00
|
|
|
// Close is the interface that wraps the basic Close method.
|
2009-02-16 17:32:30 -07:00
|
|
|
type Close interface {
|
2009-04-17 01:08:24 -06:00
|
|
|
Close() os.Error;
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
|
|
|
|
2009-03-06 04:43:44 -07:00
|
|
|
// ReadWrite is the interface that groups the basic Read and Write methods.
|
2009-01-20 15:40:40 -07:00
|
|
|
type ReadWrite interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Read;
|
|
|
|
Write;
|
2008-09-17 14:49:23 -06:00
|
|
|
}
|
|
|
|
|
2009-03-06 04:43:44 -07:00
|
|
|
// ReadClose is the interface that groups the basic Read and Close methods.
|
2009-02-16 17:32:30 -07:00
|
|
|
type ReadClose interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Read;
|
|
|
|
Close;
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
|
|
|
|
2009-03-06 04:43:44 -07:00
|
|
|
// WriteClose is the interface that groups the basic Write and Close methods.
|
2009-02-16 17:32:30 -07:00
|
|
|
type WriteClose interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Write;
|
|
|
|
Close;
|
2008-10-20 13:37:07 -06:00
|
|
|
}
|
|
|
|
|
2009-03-06 04:43:44 -07:00
|
|
|
// ReadWriteClose is the interface that groups the basic Read, Write and Close methods.
|
2009-02-16 17:32:30 -07:00
|
|
|
type ReadWriteClose interface {
|
2009-02-16 17:37:49 -07:00
|
|
|
Read;
|
|
|
|
Write;
|
|
|
|
Close;
|
2009-02-03 15:16:22 -07:00
|
|
|
}
|
|
|
|
|
2009-02-06 18:54:26 -07:00
|
|
|
// Convert a string to an array of bytes for easy marshaling.
|
|
|
|
func StringBytes(s string) []byte {
|
|
|
|
b := make([]byte, len(s));
|
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
b[i] = s[i];
|
2008-09-12 17:42:53 -06:00
|
|
|
}
|
2009-02-06 18:54:26 -07:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2009-03-06 04:43:44 -07:00
|
|
|
// WriteString writes the contents of the string s to w, which accepts an array of bytes.
|
2009-04-17 01:08:24 -06:00
|
|
|
func WriteString(w Write, s string) (n int, err os.Error) {
|
2009-02-06 18:54:26 -07:00
|
|
|
return w.Write(StringBytes(s))
|
2008-09-12 17:42:53 -06:00
|
|
|
}
|
2008-11-11 18:28:36 -07:00
|
|
|
|
2009-03-06 04:43:44 -07:00
|
|
|
// Readn reads r until the buffer buf is full, or until EOF or error.
|
2009-04-17 01:08:24 -06:00
|
|
|
func Readn(r Read, buf []byte) (n int, err os.Error) {
|
2008-11-11 18:28:36 -07:00
|
|
|
n = 0;
|
|
|
|
for n < len(buf) {
|
2009-03-02 17:12:04 -07:00
|
|
|
nn, e := r.Read(buf[n:len(buf)]);
|
2008-11-11 18:28:36 -07:00
|
|
|
if nn > 0 {
|
|
|
|
n += nn
|
|
|
|
}
|
|
|
|
if e != nil {
|
|
|
|
return n, e
|
|
|
|
}
|
|
|
|
if nn <= 0 {
|
|
|
|
return n, ErrEOF // no error but insufficient data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert something that implements Read into something
|
|
|
|
// whose Reads are always Readn
|
2009-02-16 17:32:30 -07:00
|
|
|
type fullRead struct {
|
2009-03-02 17:12:04 -07:00
|
|
|
r Read;
|
2008-11-11 18:28:36 -07:00
|
|
|
}
|
|
|
|
|
2009-04-17 01:08:24 -06:00
|
|
|
func (fr *fullRead) Read(p []byte) (n int, err os.Error) {
|
2009-03-02 17:12:04 -07:00
|
|
|
n, err = Readn(fr.r, p);
|
2008-11-11 18:28:36 -07:00
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
2009-03-06 04:43:44 -07:00
|
|
|
// MakeFullReader takes r, an implementation of Read, and returns an object
|
|
|
|
// that still implements Read but always calls Readn underneath.
|
2009-03-02 17:12:04 -07:00
|
|
|
func MakeFullReader(r Read) Read {
|
|
|
|
if fr, ok := r.(*fullRead); ok {
|
2009-02-16 17:32:30 -07:00
|
|
|
// already a fullRead
|
2009-03-02 17:12:04 -07:00
|
|
|
return r
|
2008-11-11 18:28:36 -07:00
|
|
|
}
|
2009-03-03 09:39:12 -07:00
|
|
|
return &fullRead{r}
|
2008-11-11 18:28:36 -07:00
|
|
|
}
|
2008-11-18 19:08:05 -07:00
|
|
|
|
2009-03-06 04:43:44 -07:00
|
|
|
// 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.
|
2009-04-17 01:08:24 -06:00
|
|
|
func Copyn(src Read, dst Write, n int64) (written int64, err os.Error) {
|
2009-01-06 16:19:02 -07:00
|
|
|
buf := make([]byte, 32*1024);
|
2008-11-18 19:45:51 -07:00
|
|
|
for written < n {
|
2008-11-19 11:20:52 -07:00
|
|
|
l := len(buf);
|
|
|
|
if d := n - written; d < int64(l) {
|
|
|
|
l = int(d);
|
2008-11-18 19:08:05 -07:00
|
|
|
}
|
|
|
|
nr, er := src.Read(buf[0 : l]);
|
|
|
|
if nr > 0 {
|
|
|
|
nw, ew := dst.Write(buf[0 : nr]);
|
2008-11-18 19:45:51 -07:00
|
|
|
if nw > 0 {
|
|
|
|
written += int64(nw);
|
|
|
|
}
|
|
|
|
if ew != nil {
|
2008-11-18 19:08:05 -07:00
|
|
|
err = ew;
|
|
|
|
break;
|
|
|
|
}
|
2008-11-18 19:45:51 -07:00
|
|
|
if nr != nw {
|
|
|
|
err = os.EIO;
|
|
|
|
break;
|
|
|
|
}
|
2008-11-18 19:08:05 -07:00
|
|
|
}
|
|
|
|
if er != nil {
|
|
|
|
err = er;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if nr == 0 {
|
2008-11-18 19:45:51 -07:00
|
|
|
err = ErrEOF;
|
2008-11-18 19:08:05 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-11-18 19:45:51 -07:00
|
|
|
return written, err
|
2008-11-18 19:08:05 -07:00
|
|
|
}
|
2008-11-18 19:45:51 -07:00
|
|
|
|
2009-03-06 04:43:44 -07:00
|
|
|
// Copy copies from src to dst until EOF is reached.
|
|
|
|
// It returns the number of bytes copied and the error, if any.
|
2009-04-17 01:08:24 -06:00
|
|
|
func Copy(src Read, dst Write) (written int64, err os.Error) {
|
2009-01-06 16:19:02 -07:00
|
|
|
buf := make([]byte, 32*1024);
|
2008-11-18 19:45:51 -07:00
|
|
|
for {
|
|
|
|
nr, er := src.Read(buf);
|
|
|
|
if nr > 0 {
|
|
|
|
nw, ew := dst.Write(buf[0:nr]);
|
|
|
|
if nw > 0 {
|
|
|
|
written += int64(nw);
|
|
|
|
}
|
|
|
|
if ew != nil {
|
|
|
|
err = ew;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if nr != nw {
|
|
|
|
err = os.EIO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if er != nil {
|
|
|
|
err = er;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if nr == 0 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return written, err
|
|
|
|
}
|