diff options
| author | Russ Cox <rsc@golang.org> | 2009-05-22 22:43:57 -0700 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2009-05-22 22:43:57 -0700 |
| commit | ca2fe5d8bd77b1a159baddde66f8f6ca5e731be2 (patch) | |
| tree | b6132390c4156a7249f1ec0bb7a3b13d7f27e6b1 /src/lib/io | |
| parent | 2a4dcfffc9d0618dd63b04db50816d82e4db5dc7 (diff) | |
| download | go-ca2fe5d8bd77b1a159baddde66f8f6ca5e731be2.tar.xz | |
Automated g4 rollback of changelist 29302.
*** Reason for rollback ***
too many files included
*** Original change description ***
simplifying grammar: delete LBASETYPE and LACONST
R=ken
OCL=29303
CL=29303
Diffstat (limited to 'src/lib/io')
| -rw-r--r-- | src/lib/io/io.go | 14 | ||||
| -rw-r--r-- | src/lib/io/pipe.go | 86 |
2 files changed, 27 insertions, 73 deletions
diff --git a/src/lib/io/io.go b/src/lib/io/io.go index 68ef8c4803..c120d8d443 100644 --- a/src/lib/io/io.go +++ b/src/lib/io/io.go @@ -69,19 +69,7 @@ type ReadWriteCloser interface { Closer; } -// ReadByter is the interface that wraps the basic ReadByte method. -// Implementations of ReadByte typically use buffered I/O. -type ReadByter interface { - ReadByte() (byte, os.Error); -} - -// WriteByter is the interface that wraps the basic WriteByte method. -// Implementations of WriteByte typically use buffered I/O. -type WriteByter interface { - WriteByte(byte) os.Error; -} - -// StringBytes converts a string to an array of bytes for easy marshaling. +// 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++ { diff --git a/src/lib/io/pipe.go b/src/lib/io/pipe.go index f91bf34567..5f9e7a488c 100644 --- a/src/lib/io/pipe.go +++ b/src/lib/io/pipe.go @@ -21,9 +21,7 @@ type pipeReturn struct { // Shared pipe structure. type pipe struct { rclosed bool; // Read end closed? - rerr os.Error; // Error supplied to CloseReader wclosed bool; // Write end closed? - werr os.Error; // Error supplied to CloseWriter wpend []byte; // Written data waiting to be read. wtot int; // Bytes consumed so far in current write. cr chan []byte; // Write sends data here... @@ -41,7 +39,7 @@ func (p *pipe) Read(data []byte) (n int, err os.Error) { p.wpend = <-p.cr; } if p.wpend == nil { - return 0, p.werr; + return 0, nil; } p.wtot = 0; } @@ -72,7 +70,7 @@ func (p *pipe) Write(data []byte) (n int, err os.Error) { return 0, os.EINVAL; } if p.rclosed { - return 0, p.rerr; + return 0, os.EPIPE; } // Send data to reader. @@ -83,34 +81,29 @@ func (p *pipe) Write(data []byte) (n int, err os.Error) { return res.n, res.err; } -func (p *pipe) CloseReader(rerr os.Error) os.Error { +func (p *pipe) CloseReader() os.Error { if p == nil || p.rclosed { return os.EINVAL; } // Stop any future writes. p.rclosed = true; - if rerr == nil { - rerr = os.EPIPE; - } - p.rerr = rerr; // Stop the current write. if !p.wclosed { - p.cw <- pipeReturn{p.wtot, rerr}; + p.cw <- pipeReturn{p.wtot, os.EPIPE}; } return nil; } -func (p *pipe) CloseWriter(werr os.Error) os.Error { +func (p *pipe) CloseWriter() os.Error { if p == nil || p.wclosed { return os.EINVAL; } // Stop any future reads. p.wclosed = true; - p.werr = werr; // Stop the current read. if !p.rclosed { @@ -128,97 +121,70 @@ func (p *pipe) CloseWriter(werr os.Error) os.Error { // 2. Clients cannot use interface conversions on the // read end to find the Write method, and vice versa. -// A PipeReader is the read half of a pipe. -type PipeReader struct { +// Read half of pipe. +type pipeRead struct { lock sync.Mutex; p *pipe; } -// Read implements the standard Read interface: -// it reads data from the pipe, blocking until a writer -// arrives or the write end is closed. -// If the write end is closed with an error, that error is -// returned as err; otherwise err is nil. -func (r *PipeReader) Read(data []byte) (n int, err os.Error) { +func (r *pipeRead) Read(data []byte) (n int, err os.Error) { r.lock.Lock(); defer r.lock.Unlock(); return r.p.Read(data); } -// Close closes the reader; subsequent writes to the -// write half of the pipe will return the error os.EPIPE. -func (r *PipeReader) Close() os.Error { - r.lock.Lock(); - defer r.lock.Unlock(); - - return r.p.CloseReader(nil); -} - -// CloseWithError closes the reader; subsequent writes -// to the write half of the pipe will return the error rerr. -func (r *PipeReader) CloseWithError(rerr os.Error) os.Error { +func (r *pipeRead) Close() os.Error { r.lock.Lock(); defer r.lock.Unlock(); - return r.p.CloseReader(rerr); + return r.p.CloseReader(); } -func (r *PipeReader) finish() { +func (r *pipeRead) finish() { r.Close(); } // Write half of pipe. -type PipeWriter struct { +type pipeWrite struct { lock sync.Mutex; p *pipe; } -// Write implements the standard Write interface: -// it writes data to the pipe, blocking until readers -// have consumed all the data or the read end is closed. -// If the read end is closed with an error, that err is -// returned as err; otherwise err is os.EPIPE. -func (w *PipeWriter) Write(data []byte) (n int, err os.Error) { +func (w *pipeWrite) Write(data []byte) (n int, err os.Error) { w.lock.Lock(); defer w.lock.Unlock(); return w.p.Write(data); } -// Close closes the writer; subsequent reads from the -// read half of the pipe will return no bytes and a nil error. -func (w *PipeWriter) Close() os.Error { - w.lock.Lock(); - defer w.lock.Unlock(); - - return w.p.CloseWriter(nil); -} - -// CloseWithError closes the writer; subsequent reads from the -// read half of the pipe will return no bytes and the error werr. -func (w *PipeWriter) CloseWithError(werr os.Error) os.Error { +func (w *pipeWrite) Close() os.Error { w.lock.Lock(); defer w.lock.Unlock(); - return w.p.CloseWriter(werr); + return w.p.CloseWriter(); } -func (w *PipeWriter) finish() { +func (w *pipeWrite) finish() { w.Close(); } // Pipe creates a synchronous in-memory pipe. -// It can be used to connect code expecting an io.Reader +// Used to connect code expecting an io.Reader // with code expecting an io.Writer. -// Reads on one end are matched with writes on the other. -func Pipe() (*PipeReader, *PipeWriter) { +// +// Reads on one end are matched by writes on the other. +// Writes don't complete until all the data has been +// written or the read end is closed. Reads return +// any available data or block until the next write +// or the write end is closed. +func Pipe() (io.ReadCloser, io.WriteCloser) { p := new(pipe); p.cr = make(chan []byte, 1); p.cw = make(chan pipeReturn, 1); - r := new(PipeReader); + r := new(pipeRead); r.p = p; - w := new(PipeWriter); + w := new(pipeWrite); w.p = p; return r, w; } |
