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 (
|
2009-06-04 16:28:09 -06:00
|
|
|
"bytes";
|
2008-11-11 18:28:36 -07:00
|
|
|
"os";
|
|
|
|
)
|
|
|
|
|
2009-05-18 12:47:35 -06:00
|
|
|
// Error represents an unexpected I/O behavior.
|
2009-05-08 15:40:20 -06:00
|
|
|
type Error struct {
|
|
|
|
os.ErrorString
|
|
|
|
}
|
2009-05-18 12:47:35 -06:00
|
|
|
|
|
|
|
// ErrEOF means that data was expected, but a read got EOF instead.
|
2009-05-08 15:40:20 -06:00
|
|
|
var ErrEOF os.Error = &Error{"EOF"}
|
2008-09-12 17:42:53 -06:00
|
|
|
|
2009-05-18 12:47:35 -06:00
|
|
|
// ErrShortWrite means that a write accepted fewer bytes than requested
|
|
|
|
// but failed to return an explicit error.
|
|
|
|
var ErrShortWrite os.Error = &Error{"short write"}
|
|
|
|
|
|
|
|
|
2009-05-08 12:22:57 -06:00
|
|
|
// Reader is the interface that wraps the basic Read method.
|
2009-05-19 15:01:03 -06:00
|
|
|
// An implementation of Read is allowed to use all of p for
|
|
|
|
// scratch space during the call, even if it eventually returns
|
|
|
|
// n < len(p).
|
2009-05-08 12:22:57 -06:00
|
|
|
type Reader 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-05-08 12:22:57 -06:00
|
|
|
// Writer is the interface that wraps the basic Write method.
|
|
|
|
type Writer 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-05-08 12:22:57 -06:00
|
|
|
// Closer is the interface that wraps the basic Close method.
|
|
|
|
type Closer 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-05-08 12:22:57 -06:00
|
|
|
type ReadWriter interface {
|
|
|
|
Reader;
|
|
|
|
Writer;
|
2008-09-17 14:49:23 -06:00
|
|
|
}
|
|
|
|
|
2009-05-08 12:22:57 -06:00
|
|
|
// ReadCloser is the interface that groups the basic Read and Close methods.
|
|
|
|
type ReadCloser interface {
|
|
|
|
Reader;
|
|
|
|
Closer;
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
|
|
|
|
2009-05-08 12:22:57 -06:00
|
|
|
// WriteCloser is the interface that groups the basic Write and Close methods.
|
|
|
|
type WriteCloser interface {
|
|
|
|
Writer;
|
|
|
|
Closer;
|
2008-10-20 13:37:07 -06:00
|
|
|
}
|
|
|
|
|
2009-05-08 12:22:57 -06:00
|
|
|
// ReadWriteCloser is the interface that groups the basic Read, Write and Close methods.
|
|
|
|
type ReadWriteCloser interface {
|
|
|
|
Reader;
|
|
|
|
Writer;
|
|
|
|
Closer;
|
2009-02-03 15:16:22 -07:00
|
|
|
}
|
|
|
|
|
2009-05-22 23:43:57 -06:00
|
|
|
// Convert a string to an array of bytes for easy marshaling.
|
2009-02-06 18:54:26 -07:00
|
|
|
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-05-08 12:22:57 -06:00
|
|
|
func WriteString(w Writer, 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-05-19 15:01:03 -06:00
|
|
|
// ReadAtLeast reads r into buf until at least min bytes have been read,
|
|
|
|
// or until EOF or error.
|
|
|
|
func ReadAtLeast(r Reader, buf []byte, min int) (n int, err os.Error) {
|
2008-11-11 18:28:36 -07:00
|
|
|
n = 0;
|
2009-05-19 15:01:03 -06:00
|
|
|
for n < min {
|
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
|
|
|
|
}
|
|
|
|
|
2009-05-19 15:01:03 -06:00
|
|
|
// FullRead reads r until the buffer buf is full, or until EOF or error.
|
|
|
|
func FullRead(r Reader, buf []byte) (n int, err os.Error) {
|
|
|
|
// TODO(rsc): 6g bug prevents obvious return
|
|
|
|
n, err = ReadAtLeast(r, buf, len(buf));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-11 18:28:36 -07:00
|
|
|
// Convert something that implements Read into something
|
2009-04-19 22:02:29 -06:00
|
|
|
// whose Reads are always FullReads
|
2009-02-16 17:32:30 -07:00
|
|
|
type fullRead struct {
|
2009-05-08 12:22:57 -06:00
|
|
|
r Reader;
|
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-04-19 22:02:29 -06:00
|
|
|
n, err = FullRead(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
|
2009-04-19 22:02:29 -06:00
|
|
|
// that still implements Read but always calls FullRead underneath.
|
2009-05-08 12:22:57 -06:00
|
|
|
func MakeFullReader(r Reader) Reader {
|
2009-03-02 17:12:04 -07:00
|
|
|
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-05-08 12:22:57 -06:00
|
|
|
func Copyn(src Reader, dst Writer, 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-05-08 12:22:57 -06:00
|
|
|
func Copy(src Reader, dst Writer) (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
|
|
|
|
}
|
2009-05-18 14:31:56 -06:00
|
|
|
|
|
|
|
// A ByteReader satisfies Reads by consuming data from a slice of bytes.
|
|
|
|
// Clients can call NewByteReader to create one or wrap pointers
|
|
|
|
// to their own slices: r := ByteReader{&data}.
|
|
|
|
type ByteReader struct {
|
|
|
|
Data *[]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r ByteReader) Read(p []byte) (int, os.Error) {
|
|
|
|
n := len(p);
|
|
|
|
b := r.Data;
|
|
|
|
if n > len(b) {
|
|
|
|
n = len(b);
|
|
|
|
}
|
2009-06-04 16:28:09 -06:00
|
|
|
bytes.Copy(p, b[0:n]);
|
2009-05-18 14:31:56 -06:00
|
|
|
*b = b[n:len(b)];
|
|
|
|
return n, nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewByteReader returns a new ByteReader reading from data.
|
|
|
|
func NewByteReader(data []byte) ByteReader {
|
|
|
|
return ByteReader{ &data };
|
|
|
|
}
|
|
|
|
|