mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
document io
R=rsc DELTA=44 (30 added, 4 deleted, 10 changed) OCL=25819 CL=25835
This commit is contained in:
parent
5b4fa1ad22
commit
7bb335c7de
3 changed files with 39 additions and 13 deletions
|
|
@ -4,15 +4,13 @@
|
||||||
|
|
||||||
package io
|
package io
|
||||||
|
|
||||||
// Byte buffer for marshaling nested messages.
|
// Simple byte buffer for marshaling data.
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io";
|
"io";
|
||||||
"os";
|
"os";
|
||||||
)
|
)
|
||||||
|
|
||||||
// A simple implementation of the io.Read and io.Write interfaces.
|
|
||||||
// A newly allocated ByteBuffer is ready to use.
|
|
||||||
|
|
||||||
// TODO(r): Do better memory management.
|
// TODO(r): Do better memory management.
|
||||||
|
|
||||||
|
|
@ -24,6 +22,9 @@ func bytecopy(dst []byte, doff int, src []byte, soff int, count int) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A ByteBuffer is a simple implementation of the io.Read and io.Write interfaces
|
||||||
|
// connected to a buffer of bytes.
|
||||||
|
// The zero value for ByteBuffer is an empty buffer ready to use.
|
||||||
type ByteBuffer struct {
|
type ByteBuffer struct {
|
||||||
buf []byte;
|
buf []byte;
|
||||||
off int; // Read from here
|
off int; // Read from here
|
||||||
|
|
@ -31,11 +32,14 @@ type ByteBuffer struct {
|
||||||
cap int;
|
cap int;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reset resets the buffer so it has no content.
|
||||||
func (b *ByteBuffer) Reset() {
|
func (b *ByteBuffer) Reset() {
|
||||||
b.off = 0;
|
b.off = 0;
|
||||||
b.len = 0;
|
b.len = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Write appends the contents of p to the buffer. The return
|
||||||
|
// value is the length of p; err is always nil.
|
||||||
func (b *ByteBuffer) Write(p []byte) (n int, err *os.Error) {
|
func (b *ByteBuffer) Write(p []byte) (n int, err *os.Error) {
|
||||||
plen := len(p);
|
plen := len(p);
|
||||||
if len(b.buf) == 0 {
|
if len(b.buf) == 0 {
|
||||||
|
|
@ -54,6 +58,8 @@ func (b *ByteBuffer) Write(p []byte) (n int, err *os.Error) {
|
||||||
return plen, nil;
|
return plen, nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Read reads the next len(p) bytes from the buffer or until the buffer
|
||||||
|
// is drained. The return value is the number of bytes read; err is always nil.
|
||||||
func (b *ByteBuffer) Read(p []byte) (n int, err *os.Error) {
|
func (b *ByteBuffer) Read(p []byte) (n int, err *os.Error) {
|
||||||
plen := len(p);
|
plen := len(p);
|
||||||
if len(b.buf) == 0 {
|
if len(b.buf) == 0 {
|
||||||
|
|
@ -71,22 +77,23 @@ func (b *ByteBuffer) Read(p []byte) (n int, err *os.Error) {
|
||||||
return plen, nil;
|
return plen, nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Len returns the length of the underlying buffer.
|
||||||
func (b *ByteBuffer) Len() int {
|
func (b *ByteBuffer) Len() int {
|
||||||
return b.len
|
return b.len
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Off returns the location within the buffer of the next byte to be read.
|
||||||
func (b *ByteBuffer) Off() int {
|
func (b *ByteBuffer) Off() int {
|
||||||
return b.off
|
return b.off
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Data returns the contents of the unread portion of the buffer.
|
||||||
func (b *ByteBuffer) Data() []byte {
|
func (b *ByteBuffer) Data() []byte {
|
||||||
return b.buf[b.off:b.len]
|
return b.buf[b.off:b.len]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *ByteBuffer) AllData() []byte {
|
// NewByteBufferFromArray creates and initializes a new ByteBuffer
|
||||||
return b.buf[0:b.len]
|
// with buf as its initial contents.
|
||||||
}
|
|
||||||
|
|
||||||
func NewByteBufferFromArray(buf []byte) *ByteBuffer {
|
func NewByteBufferFromArray(buf []byte) *ByteBuffer {
|
||||||
b := new(ByteBuffer);
|
b := new(ByteBuffer);
|
||||||
b.buf = buf;
|
b.buf = buf;
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,11 @@
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// 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
|
package io
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
@ -9,35 +14,43 @@ import (
|
||||||
"syscall";
|
"syscall";
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ErrEOF is the error returned by Readn and Copyn when they encounter EOF.
|
||||||
var ErrEOF = os.NewError("EOF")
|
var ErrEOF = os.NewError("EOF")
|
||||||
|
|
||||||
|
// Read is the interface that wraps the basic Read method.
|
||||||
type Read interface {
|
type Read interface {
|
||||||
Read(p []byte) (n int, err *os.Error);
|
Read(p []byte) (n int, err *os.Error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Write is the interface that wraps the basic Write method.
|
||||||
type Write interface {
|
type Write interface {
|
||||||
Write(p []byte) (n int, err *os.Error);
|
Write(p []byte) (n int, err *os.Error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close is the interface that wraps the basic Close method.
|
||||||
type Close interface {
|
type Close interface {
|
||||||
Close() *os.Error;
|
Close() *os.Error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadWrite is the interface that groups the basic Read and Write methods.
|
||||||
type ReadWrite interface {
|
type ReadWrite interface {
|
||||||
Read;
|
Read;
|
||||||
Write;
|
Write;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadClose is the interface that groups the basic Read and Close methods.
|
||||||
type ReadClose interface {
|
type ReadClose interface {
|
||||||
Read;
|
Read;
|
||||||
Close;
|
Close;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteClose is the interface that groups the basic Write and Close methods.
|
||||||
type WriteClose interface {
|
type WriteClose interface {
|
||||||
Write;
|
Write;
|
||||||
Close;
|
Close;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadWriteClose is the interface that groups the basic Read, Write and Close methods.
|
||||||
type ReadWriteClose interface {
|
type ReadWriteClose interface {
|
||||||
Read;
|
Read;
|
||||||
Write;
|
Write;
|
||||||
|
|
@ -53,11 +66,12 @@ func StringBytes(s string) []byte {
|
||||||
return b;
|
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) {
|
func WriteString(w Write, s string) (n int, err *os.Error) {
|
||||||
return w.Write(StringBytes(s))
|
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) {
|
func Readn(r Read, buf []byte) (n int, err *os.Error) {
|
||||||
n = 0;
|
n = 0;
|
||||||
for n < len(buf) {
|
for n < len(buf) {
|
||||||
|
|
@ -86,6 +100,8 @@ func (fr *fullRead) Read(p []byte) (n int, err *os.Error) {
|
||||||
return n, err
|
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 {
|
func MakeFullReader(r Read) Read {
|
||||||
if fr, ok := r.(*fullRead); ok {
|
if fr, ok := r.(*fullRead); ok {
|
||||||
// already a fullRead
|
// already a fullRead
|
||||||
|
|
@ -94,8 +110,8 @@ func MakeFullReader(r Read) Read {
|
||||||
return &fullRead{r}
|
return &fullRead{r}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copies n bytes (or until EOF is reached) from src to dst.
|
// Copy n copies n bytes (or until EOF is reached) from src to dst.
|
||||||
// Returns the number of bytes copied and the error, if any.
|
// 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) {
|
func Copyn(src Read, dst Write, n int64) (written int64, err *os.Error) {
|
||||||
buf := make([]byte, 32*1024);
|
buf := make([]byte, 32*1024);
|
||||||
for written < n {
|
for written < n {
|
||||||
|
|
@ -130,8 +146,8 @@ func Copyn(src Read, dst Write, n int64) (written int64, err *os.Error) {
|
||||||
return written, err
|
return written, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copies from src to dst until EOF is reached.
|
// Copy copies from src to dst until EOF is reached.
|
||||||
// Returns the number of bytes copied and the error, if any.
|
// It returns the number of bytes copied and the error, if any.
|
||||||
func Copy(src Read, dst Write) (written int64, err *os.Error) {
|
func Copy(src Read, dst Write) (written int64, err *os.Error) {
|
||||||
buf := make([]byte, 32*1024);
|
buf := make([]byte, 32*1024);
|
||||||
for {
|
for {
|
||||||
|
|
|
||||||
|
|
@ -169,7 +169,10 @@ func (w *pipeWrite) finish() {
|
||||||
w.Close();
|
w.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a synchronous in-memory pipe.
|
// Pipe creates a synchronous in-memory pipe.
|
||||||
|
// Used to connect code expecting an io.Read
|
||||||
|
// with code expecting an io.Write.
|
||||||
|
//
|
||||||
// Reads on one end are matched by writes on the other.
|
// Reads on one end are matched by writes on the other.
|
||||||
// Writes don't complete until all the data has been
|
// Writes don't complete until all the data has been
|
||||||
// written or the read end is closed. Reads return
|
// written or the read end is closed. Reads return
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue