1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // This package provides basic interfaces to I/O 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
8 // abstract the functionality, plus some other related primitives.
13 // Error represents an unexpected I/O behavior.
18 // ErrShortWrite means that a write accepted fewer bytes than requested
19 // but failed to return an explicit error.
20 var ErrShortWrite os.Error = &Error{"short write"}
22 // ErrShortBuffer means that a read required a longer buffer than was provided.
23 var ErrShortBuffer os.Error = &Error{"short buffer"}
25 // ErrUnexpectedEOF means that os.EOF was encountered in the
26 // middle of reading a fixed-size block or data structure.
27 var ErrUnexpectedEOF os.Error = &Error{"unexpected EOF"}
29 // Reader is the interface that wraps the basic Read method.
31 // Read reads up to len(p) bytes into p. It returns the number of bytes
32 // read (0 <= n <= len(p)) and any error encountered.
33 // Even if Read returns n < len(p),
34 // it may use all of p as scratch space during the call.
35 // If some data is available but not len(p) bytes, Read conventionally
36 // returns what is available rather than block waiting for more.
38 // At the end of the input stream, Read returns 0, os.EOF.
39 // Read may return a non-zero number of bytes with a non-nil err.
40 // In particular, a Read that exhausts the input may return n > 0, os.EOF.
41 type Reader interface {
42 Read(p []byte) (n int, err os.Error)
45 // Writer is the interface that wraps the basic Write method.
47 // Write writes len(p) bytes from p to the underlying data stream.
48 // It returns the number of bytes written from p (0 <= n <= len(p))
49 // and any error encountered that caused the write to stop early.
50 // Write must return a non-nil error if it returns n < len(p).
51 type Writer interface {
52 Write(p []byte) (n int, err os.Error)
55 // Closer is the interface that wraps the basic Close method.
56 type Closer interface {
60 // Seeker is the interface that wraps the basic Seek method.
62 // Seek sets the offset for the next Read or Write to offset,
63 // interpreted according to whence: 0 means relative to the origin of
64 // the file, 1 means relative to the current offset, and 2 means
65 // relative to the end. Seek returns the new offset and an Error, if
67 type Seeker interface {
68 Seek(offset int64, whence int) (ret int64, err os.Error)
71 // ReadWriter is the interface that groups the basic Read and Write methods.
72 type ReadWriter interface {
77 // ReadCloser is the interface that groups the basic Read and Close methods.
78 type ReadCloser interface {
83 // WriteCloser is the interface that groups the basic Write and Close methods.
84 type WriteCloser interface {
89 // ReadWriteCloser is the interface that groups the basic Read, Write and Close methods.
90 type ReadWriteCloser interface {
96 // ReadSeeker is the interface that groups the basic Read and Seek methods.
97 type ReadSeeker interface {
102 // WriteSeeker is the interface that groups the basic Write and Seek methods.
103 type WriteSeeker interface {
108 // ReadWriteSeeker is the interface that groups the basic Read, Write and Seek methods.
109 type ReadWriteSeeker interface {
115 // ReaderFrom is the interface that wraps the ReadFrom method.
116 type ReaderFrom interface {
117 ReadFrom(r Reader) (n int64, err os.Error)
120 // WriterTo is the interface that wraps the WriteTo method.
121 type WriterTo interface {
122 WriteTo(w Writer) (n int64, err os.Error)
125 // ReaderAt is the interface that wraps the basic ReadAt method.
127 // ReadAt reads len(p) bytes into p starting at offset off in the
128 // underlying data stream. It returns the number of bytes
129 // read (0 <= n <= len(p)) and any error encountered.
131 // Even if ReadAt returns n < len(p),
132 // it may use all of p as scratch space during the call.
133 // If some data is available but not len(p) bytes, ReadAt blocks
134 // until either all the data is available or an error occurs.
136 // At the end of the input stream, ReadAt returns 0, os.EOF.
137 // ReadAt may return a non-zero number of bytes with a non-nil err.
138 // In particular, a ReadAt that exhausts the input may return n > 0, os.EOF.
139 type ReaderAt interface {
140 ReadAt(p []byte, off int64) (n int, err os.Error)
143 // WriterAt is the interface that wraps the basic WriteAt method.
145 // WriteAt writes len(p) bytes from p to the underlying data stream
146 // at offset off. It returns the number of bytes written from p (0 <= n <= len(p))
147 // and any error encountered that caused the write to stop early.
148 // WriteAt must return a non-nil error if it returns n < len(p).
149 type WriterAt interface {
150 WriteAt(p []byte, off int64) (n int, err os.Error)
153 // ReadByter is the interface that wraps the ReadByte method.
155 // ReadByte reads and returns the next byte from the input.
156 // If no byte is available, err will be set.
157 type ReadByter interface {
158 ReadByte() (c byte, err os.Error)
161 // WriteString writes the contents of the string s to w, which accepts an array of bytes.
162 func WriteString(w Writer, s string) (n int, err os.Error) {
163 return w.Write([]byte(s))
166 // ReadAtLeast reads from r into buf until it has read at least min bytes.
167 // It returns the number of bytes copied and an error if fewer bytes were read.
168 // The error is os.EOF only if no bytes were read.
169 // If an EOF happens after reading fewer than min bytes,
170 // ReadAtLeast returns ErrUnexpectedEOF.
171 // If min is greater than the length of buf, ReadAtLeast returns ErrShortBuffer.
172 func ReadAtLeast(r Reader, buf []byte, min int) (n int, err os.Error) {
174 return 0, ErrShortBuffer
177 nn, e := r.Read(buf[n:])
182 if e == os.EOF && n > 0 {
191 // ReadFull reads exactly len(buf) bytes from r into buf.
192 // It returns the number of bytes copied and an error if fewer bytes were read.
193 // The error is os.EOF only if no bytes were read.
194 // If an EOF happens after reading some but not all the bytes,
195 // ReadFull returns ErrUnexpectedEOF.
196 func ReadFull(r Reader, buf []byte) (n int, err os.Error) {
197 return ReadAtLeast(r, buf, len(buf))
200 // Copyn copies n bytes (or until an error) from src to dst.
201 // It returns the number of bytes copied and the error, if any.
203 // If dst implements the ReaderFrom interface,
204 // the copy is implemented by calling dst.ReadFrom(src).
205 func Copyn(dst Writer, src Reader, n int64) (written int64, err os.Error) {
206 // If the writer has a ReadFrom method, use it to do the copy.
207 // Avoids a buffer allocation and a copy.
208 if rt, ok := dst.(ReaderFrom); ok {
209 written, err = rt.ReadFrom(LimitReader(src, n))
210 if written < n && err == nil {
211 // rt stopped early; must have been EOF.
216 buf := make([]byte, 32*1024)
219 if d := n - written; d < int64(l) {
222 nr, er := src.Read(buf[0:l])
224 nw, ew := dst.Write(buf[0:nr])
245 // Copy copies from src to dst until either EOF is reached
246 // on src or an error occurs. It returns the number of bytes
247 // copied and the error, if any.
249 // If dst implements the ReaderFrom interface,
250 // the copy is implemented by calling dst.ReadFrom(src).
251 // Otherwise, if src implements the WriterTo interface,
252 // the copy is implemented by calling src.WriteTo(dst).
253 func Copy(dst Writer, src Reader) (written int64, err os.Error) {
254 // If the writer has a ReadFrom method, use it to do the copy.
255 // Avoids an allocation and a copy.
256 if rt, ok := dst.(ReaderFrom); ok {
257 return rt.ReadFrom(src)
259 // Similarly, if the reader has a WriteTo method, use it to do the copy.
260 if wt, ok := src.(WriterTo); ok {
261 return wt.WriteTo(dst)
263 buf := make([]byte, 32*1024)
265 nr, er := src.Read(buf)
267 nw, ew := dst.Write(buf[0:nr])
291 // LimitReader returns a Reader that reads from r
292 // but stops with os.EOF after n bytes.
293 func LimitReader(r Reader, n int64) Reader { return &limitedReader{r, n} }
295 type limitedReader struct {
300 func (l *limitedReader) Read(p []byte) (n int, err os.Error) {
304 if int64(len(p)) > l.n {
312 // NewSectionReader returns a SectionReader that reads from r
313 // starting at offset off and stops with os.EOF after n bytes.
314 func NewSectionReader(r ReaderAt, off int64, n int64) *SectionReader {
315 return &SectionReader{r, off, off, off + n}
318 // SectionReader implements Read, Seek, and ReadAt on a section
319 // of an underlying ReaderAt.
320 type SectionReader struct {
327 func (s *SectionReader) Read(p []byte) (n int, err os.Error) {
328 if s.off >= s.limit {
331 if max := s.limit - s.off; int64(len(p)) > max {
334 n, err = s.r.ReadAt(p, s.off)
339 func (s *SectionReader) Seek(offset int64, whence int) (ret int64, err os.Error) {
350 if offset < s.base || offset > s.limit {
354 return offset - s.base, nil
357 func (s *SectionReader) ReadAt(p []byte, off int64) (n int, err os.Error) {
358 if off < 0 || off >= s.limit-s.base {
362 if max := s.limit - off; int64(len(p)) > max {
365 return s.r.ReadAt(p, off)
368 // Size returns the size of the section in bytes.
369 func (s *SectionReader) Size() int64 { return s.limit - s.base }