OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Package io provides basic interfaces to I/O primitives. | 5 // Package io provides basic interfaces to I/O primitives. |
6 // Its primary job is to wrap existing implementations of such primitives, | 6 // Its primary job is to wrap existing implementations of such primitives, |
7 // such as those in package os, into shared public interfaces that | 7 // such as those in package os, into shared public interfaces that |
8 // abstract the functionality, plus some other related primitives. | 8 // abstract the functionality, plus some other related primitives. |
9 // | 9 // |
10 // Because these interfaces and primitives wrap lower-level operations with | 10 // Because these interfaces and primitives wrap lower-level operations with |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 // return either err == EOF or err == nil. The next Read should | 56 // return either err == EOF or err == nil. The next Read should |
57 // return 0, EOF regardless. | 57 // return 0, EOF regardless. |
58 // | 58 // |
59 // Callers should always process the n > 0 bytes returned before | 59 // Callers should always process the n > 0 bytes returned before |
60 // considering the error err. Doing so correctly handles I/O errors | 60 // considering the error err. Doing so correctly handles I/O errors |
61 // that happen after reading some bytes and also both of the | 61 // that happen after reading some bytes and also both of the |
62 // allowed EOF behaviors. | 62 // allowed EOF behaviors. |
63 // | 63 // |
64 // Implementations of Read are discouraged from returning a | 64 // Implementations of Read are discouraged from returning a |
65 // zero byte count with a nil error, and callers should treat | 65 // zero byte count with a nil error, and callers should treat |
66 // that situation as a no-op. | 66 // that situation as a no-op. Implementations must not retain p. |
67 type Reader interface { | 67 type Reader interface { |
68 Read(p []byte) (n int, err error) | 68 Read(p []byte) (n int, err error) |
69 } | 69 } |
70 | 70 |
71 // Writer is the interface that wraps the basic Write method. | 71 // Writer is the interface that wraps the basic Write method. |
72 // | 72 // |
73 // Write writes len(p) bytes from p to the underlying data stream. | 73 // Write writes len(p) bytes from p to the underlying data stream. |
74 // It returns the number of bytes written from p (0 <= n <= len(p)) | 74 // It returns the number of bytes written from p (0 <= n <= len(p)) |
75 // and any error encountered that caused the write to stop early. | 75 // and any error encountered that caused the write to stop early. |
76 // Write must return a non-nil error if it returns n < len(p). | 76 // Write must return a non-nil error if it returns n < len(p). |
77 // Write must not modify the slice data, even temporarily. | 77 // Write must not modify the slice data, even temporarily. |
| 78 // |
| 79 // Implementations must not retain p. |
78 type Writer interface { | 80 type Writer interface { |
79 Write(p []byte) (n int, err error) | 81 Write(p []byte) (n int, err error) |
80 } | 82 } |
81 | 83 |
82 // Closer is the interface that wraps the basic Close method. | 84 // Closer is the interface that wraps the basic Close method. |
83 // | 85 // |
84 // The behavior of Close after the first call is undefined. | 86 // The behavior of Close after the first call is undefined. |
85 // Specific implementations may document their own behavior. | 87 // Specific implementations may document their own behavior. |
86 type Closer interface { | 88 type Closer interface { |
87 Close() error | 89 Close() error |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 // | 187 // |
186 // If the n = len(p) bytes returned by ReadAt are at the end of the | 188 // If the n = len(p) bytes returned by ReadAt are at the end of the |
187 // input source, ReadAt may return either err == EOF or err == nil. | 189 // input source, ReadAt may return either err == EOF or err == nil. |
188 // | 190 // |
189 // If ReadAt is reading from an input source with a seek offset, | 191 // If ReadAt is reading from an input source with a seek offset, |
190 // ReadAt should not affect nor be affected by the underlying | 192 // ReadAt should not affect nor be affected by the underlying |
191 // seek offset. | 193 // seek offset. |
192 // | 194 // |
193 // Clients of ReadAt can execute parallel ReadAt calls on the | 195 // Clients of ReadAt can execute parallel ReadAt calls on the |
194 // same input source. | 196 // same input source. |
| 197 // |
| 198 // Implementations must not retain p. |
195 type ReaderAt interface { | 199 type ReaderAt interface { |
196 ReadAt(p []byte, off int64) (n int, err error) | 200 ReadAt(p []byte, off int64) (n int, err error) |
197 } | 201 } |
198 | 202 |
199 // WriterAt is the interface that wraps the basic WriteAt method. | 203 // WriterAt is the interface that wraps the basic WriteAt method. |
200 // | 204 // |
201 // WriteAt writes len(p) bytes from p to the underlying data stream | 205 // WriteAt writes len(p) bytes from p to the underlying data stream |
202 // at offset off. It returns the number of bytes written from p (0 <= n <= len(
p)) | 206 // at offset off. It returns the number of bytes written from p (0 <= n <= len(
p)) |
203 // and any error encountered that caused the write to stop early. | 207 // and any error encountered that caused the write to stop early. |
204 // WriteAt must return a non-nil error if it returns n < len(p). | 208 // WriteAt must return a non-nil error if it returns n < len(p). |
205 // | 209 // |
206 // If WriteAt is writing to a destination with a seek offset, | 210 // If WriteAt is writing to a destination with a seek offset, |
207 // WriteAt should not affect nor be affected by the underlying | 211 // WriteAt should not affect nor be affected by the underlying |
208 // seek offset. | 212 // seek offset. |
209 // | 213 // |
210 // Clients of WriteAt can execute parallel WriteAt calls on the same | 214 // Clients of WriteAt can execute parallel WriteAt calls on the same |
211 // destination if the ranges do not overlap. | 215 // destination if the ranges do not overlap. |
| 216 // |
| 217 // Implementations must not retain p. |
212 type WriterAt interface { | 218 type WriterAt interface { |
213 WriteAt(p []byte, off int64) (n int, err error) | 219 WriteAt(p []byte, off int64) (n int, err error) |
214 } | 220 } |
215 | 221 |
216 // ByteReader is the interface that wraps the ReadByte method. | 222 // ByteReader is the interface that wraps the ReadByte method. |
217 // | 223 // |
218 // ReadByte reads and returns the next byte from the input. | 224 // ReadByte reads and returns the next byte from the input. |
219 // If no byte is available, err will be set. | 225 // If no byte is available, err will be set. |
220 type ByteReader interface { | 226 type ByteReader interface { |
221 ReadByte() (c byte, err error) | 227 ReadByte() (c byte, err error) |
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
484 | 490 |
485 func (t *teeReader) Read(p []byte) (n int, err error) { | 491 func (t *teeReader) Read(p []byte) (n int, err error) { |
486 n, err = t.r.Read(p) | 492 n, err = t.r.Read(p) |
487 if n > 0 { | 493 if n > 0 { |
488 if n, err := t.w.Write(p[:n]); err != nil { | 494 if n, err := t.w.Write(p[:n]); err != nil { |
489 return n, err | 495 return n, err |
490 } | 496 } |
491 } | 497 } |
492 return | 498 return |
493 } | 499 } |
OLD | NEW |