OSDN Git Service

Update Go library to r60.
[pf3gnuchains/gcc-fork.git] / libgo / go / testing / iotest / reader.go
index e4003d7..dcf5565 100644 (file)
@@ -37,7 +37,6 @@ func (r *halfReader) Read(p []byte) (int, os.Error) {
        return r.r.Read(p[0 : (len(p)+1)/2])
 }
 
-
 // DataErrReader returns a Reader that returns the final
 // error with the last data read, instead of by itself with
 // zero bytes of data.
@@ -58,7 +57,7 @@ func (r *dataErrReader) Read(p []byte) (n int, err os.Error) {
                        r.unread = r.data[0:n1]
                        err = err1
                }
-               if n > 0 {
+               if n > 0 || err != nil {
                        break
                }
                n = copy(p, r.unread)
@@ -66,3 +65,22 @@ func (r *dataErrReader) Read(p []byte) (n int, err os.Error) {
        }
        return
 }
+
+var ErrTimeout = os.NewError("timeout")
+
+// TimeoutReader returns ErrTimeout on the second read
+// with no data.  Subsequent calls to read succeed.
+func TimeoutReader(r io.Reader) io.Reader { return &timeoutReader{r, 0} }
+
+type timeoutReader struct {
+       r     io.Reader
+       count int
+}
+
+func (r *timeoutReader) Read(p []byte) (int, os.Error) {
+       r.count++
+       if r.count == 2 {
+               return 0, ErrTimeout
+       }
+       return r.r.Read(p)
+}