diff options
author | Ian Lance Taylor <ian@gcc.gnu.org> | 2011-05-20 00:18:15 +0000 |
---|---|---|
committer | Ian Lance Taylor <ian@gcc.gnu.org> | 2011-05-20 00:18:15 +0000 |
commit | 9ff56c9570642711d5b7ab29920ecf5dbff14a27 (patch) | |
tree | c891bdec1e6f073f73fedeef23718bc3ac30d499 /libgo/go/io | |
parent | 37cb25ed7acdb844b218231130e54b8b7a0ff6e6 (diff) | |
download | gcc-9ff56c9570642711d5b7ab29920ecf5dbff14a27.tar.gz |
Update to current version of Go library.
From-SVN: r173931
Diffstat (limited to 'libgo/go/io')
-rw-r--r-- | libgo/go/io/io.go | 26 | ||||
-rw-r--r-- | libgo/go/io/io_test.go | 33 | ||||
-rw-r--r-- | libgo/go/io/ioutil/ioutil.go | 13 | ||||
-rw-r--r-- | libgo/go/io/multi.go | 6 |
4 files changed, 56 insertions, 22 deletions
diff --git a/libgo/go/io/io.go b/libgo/go/io/io.go index 3b879189798..0bc73d67dd9 100644 --- a/libgo/go/io/io.go +++ b/libgo/go/io/io.go @@ -2,7 +2,7 @@ // 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. +// Package io 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, plus some other related primitives. @@ -136,6 +136,10 @@ type WriterTo interface { // At the end of the input stream, ReadAt returns 0, os.EOF. // ReadAt may return a non-zero number of bytes with a non-nil err. // In particular, a ReadAt that exhausts the input may return n > 0, os.EOF. +// +// If ReadAt is reading from an data stream with a seek offset, +// ReadAt should not affect nor be affected by the underlying +// seek offset. type ReaderAt interface { ReadAt(p []byte, off int64) (n int, err os.Error) } @@ -182,16 +186,16 @@ func ReadAtLeast(r Reader, buf []byte, min int) (n int, err os.Error) { if len(buf) < min { return 0, ErrShortBuffer } - for n < min { - nn, e := r.Read(buf[n:]) - if nn > 0 { - n += nn - } - if e != nil { - if e == os.EOF && n > 0 { - e = ErrUnexpectedEOF - } - return n, e + for n < min && err == nil { + var nn int + nn, err = r.Read(buf[n:]) + n += nn + } + if err == os.EOF { + if n >= min { + err = nil + } else if n > 0 { + err = ErrUnexpectedEOF } } return diff --git a/libgo/go/io/io_test.go b/libgo/go/io/io_test.go index 4fcd85e693e..bc4f354af40 100644 --- a/libgo/go/io/io_test.go +++ b/libgo/go/io/io_test.go @@ -118,27 +118,50 @@ func TestCopynEOF(t *testing.T) { func TestReadAtLeast(t *testing.T) { var rb bytes.Buffer + testReadAtLeast(t, &rb) +} + +// A version of bytes.Buffer that returns n > 0, os.EOF on Read +// when the input is exhausted. +type dataAndEOFBuffer struct { + bytes.Buffer +} + +func (r *dataAndEOFBuffer) Read(p []byte) (n int, err os.Error) { + n, err = r.Buffer.Read(p) + if n > 0 && r.Buffer.Len() == 0 && err == nil { + err = os.EOF + } + return +} + +func TestReadAtLeastWithDataAndEOF(t *testing.T) { + var rb dataAndEOFBuffer + testReadAtLeast(t, &rb) +} + +func testReadAtLeast(t *testing.T, rb ReadWriter) { rb.Write([]byte("0123")) buf := make([]byte, 2) - n, err := ReadAtLeast(&rb, buf, 2) + n, err := ReadAtLeast(rb, buf, 2) if err != nil { t.Error(err) } - n, err = ReadAtLeast(&rb, buf, 4) + n, err = ReadAtLeast(rb, buf, 4) if err != ErrShortBuffer { t.Errorf("expected ErrShortBuffer got %v", err) } if n != 0 { t.Errorf("expected to have read 0 bytes, got %v", n) } - n, err = ReadAtLeast(&rb, buf, 1) + n, err = ReadAtLeast(rb, buf, 1) if err != nil { t.Error(err) } if n != 2 { t.Errorf("expected to have read 2 bytes, got %v", n) } - n, err = ReadAtLeast(&rb, buf, 2) + n, err = ReadAtLeast(rb, buf, 2) if err != os.EOF { t.Errorf("expected EOF, got %v", err) } @@ -146,7 +169,7 @@ func TestReadAtLeast(t *testing.T) { t.Errorf("expected to have read 0 bytes, got %v", n) } rb.Write([]byte("4")) - n, err = ReadAtLeast(&rb, buf, 2) + n, err = ReadAtLeast(rb, buf, 2) if err != ErrUnexpectedEOF { t.Errorf("expected ErrUnexpectedEOF, got %v", err) } diff --git a/libgo/go/io/ioutil/ioutil.go b/libgo/go/io/ioutil/ioutil.go index 57d797e851c..5f1eecaabed 100644 --- a/libgo/go/io/ioutil/ioutil.go +++ b/libgo/go/io/ioutil/ioutil.go @@ -2,8 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// Utility functions. - +// Package ioutil implements some I/O utility functions. package ioutil import ( @@ -102,3 +101,13 @@ func (nopCloser) Close() os.Error { return nil } func NopCloser(r io.Reader) io.ReadCloser { return nopCloser{r} } + +type devNull int + +func (devNull) Write(p []byte) (int, os.Error) { + return len(p), nil +} + +// Discard is an io.Writer on which all Write calls succeed +// without doing anything. +var Discard io.Writer = devNull(0) diff --git a/libgo/go/io/multi.go b/libgo/go/io/multi.go index 88e4f1b7698..d702d46c725 100644 --- a/libgo/go/io/multi.go +++ b/libgo/go/io/multi.go @@ -15,10 +15,8 @@ func (mr *multiReader) Read(p []byte) (n int, err os.Error) { n, err = mr.readers[0].Read(p) if n > 0 || err != os.EOF { if err == os.EOF { - // This shouldn't happen. - // Well-behaved Readers should never - // return non-zero bytes read with an - // EOF. But if so, we clean it. + // Don't return EOF yet. There may be more bytes + // in the remaining readers. err = nil } return |