PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/pkg/io/io.go

http://github.com/ssrl/go
Go | 439 lines | 241 code | 41 blank | 157 comment | 47 complexity | 7f98196b099aee0186c5ce1d0cbf8e26 MD5 | raw file
Possible License(s): BSD-3-Clause
  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.
  4. // Package io provides basic interfaces to I/O primitives.
  5. // Its primary job is to wrap existing implementations of such primitives,
  6. // such as those in package os, into shared public interfaces that
  7. // abstract the functionality, plus some other related primitives.
  8. package io
  9. import "os"
  10. // Error represents an unexpected I/O behavior.
  11. type Error struct {
  12. ErrorString string
  13. }
  14. func (err *Error) String() string { return err.ErrorString }
  15. // ErrShortWrite means that a write accepted fewer bytes than requested
  16. // but failed to return an explicit error.
  17. var ErrShortWrite os.Error = &Error{"short write"}
  18. // ErrShortBuffer means that a read required a longer buffer than was provided.
  19. var ErrShortBuffer os.Error = &Error{"short buffer"}
  20. // ErrUnexpectedEOF means that os.EOF was encountered in the
  21. // middle of reading a fixed-size block or data structure.
  22. var ErrUnexpectedEOF os.Error = &Error{"unexpected EOF"}
  23. // Reader is the interface that wraps the basic Read method.
  24. //
  25. // Read reads up to len(p) bytes into p. It returns the number of bytes
  26. // read (0 <= n <= len(p)) and any error encountered. Even if Read
  27. // returns n < len(p), it may use all of p as scratch space during the call.
  28. // If some data is available but not len(p) bytes, Read conventionally
  29. // returns what is available instead of waiting for more.
  30. //
  31. // When Read encounters an error or end-of-file condition after
  32. // successfully reading n > 0 bytes, it returns the number of
  33. // bytes read. It may return the (non-nil) error from the same call
  34. // or return the error (and n == 0) from a subsequent call.
  35. // An instance of this general case is that a Reader returning
  36. // a non-zero number of bytes at the end of the input stream may
  37. // return either err == os.EOF or err == nil. The next Read should
  38. // return 0, os.EOF regardless.
  39. //
  40. // Callers should always process the n > 0 bytes returned before
  41. // considering the error err. Doing so correctly handles I/O errors
  42. // that happen after reading some bytes and also both of the
  43. // allowed EOF behaviors.
  44. type Reader interface {
  45. Read(p []byte) (n int, err os.Error)
  46. }
  47. // Writer is the interface that wraps the basic Write method.
  48. //
  49. // Write writes len(p) bytes from p to the underlying data stream.
  50. // It returns the number of bytes written from p (0 <= n <= len(p))
  51. // and any error encountered that caused the write to stop early.
  52. // Write must return a non-nil error if it returns n < len(p).
  53. type Writer interface {
  54. Write(p []byte) (n int, err os.Error)
  55. }
  56. // Closer is the interface that wraps the basic Close method.
  57. type Closer interface {
  58. Close() os.Error
  59. }
  60. // Seeker is the interface that wraps the basic Seek method.
  61. //
  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
  66. // any.
  67. type Seeker interface {
  68. Seek(offset int64, whence int) (ret int64, err os.Error)
  69. }
  70. // ReadWriter is the interface that groups the basic Read and Write methods.
  71. type ReadWriter interface {
  72. Reader
  73. Writer
  74. }
  75. // ReadCloser is the interface that groups the basic Read and Close methods.
  76. type ReadCloser interface {
  77. Reader
  78. Closer
  79. }
  80. // WriteCloser is the interface that groups the basic Write and Close methods.
  81. type WriteCloser interface {
  82. Writer
  83. Closer
  84. }
  85. // ReadWriteCloser is the interface that groups the basic Read, Write and Close methods.
  86. type ReadWriteCloser interface {
  87. Reader
  88. Writer
  89. Closer
  90. }
  91. // ReadSeeker is the interface that groups the basic Read and Seek methods.
  92. type ReadSeeker interface {
  93. Reader
  94. Seeker
  95. }
  96. // WriteSeeker is the interface that groups the basic Write and Seek methods.
  97. type WriteSeeker interface {
  98. Writer
  99. Seeker
  100. }
  101. // ReadWriteSeeker is the interface that groups the basic Read, Write and Seek methods.
  102. type ReadWriteSeeker interface {
  103. Reader
  104. Writer
  105. Seeker
  106. }
  107. // ReaderFrom is the interface that wraps the ReadFrom method.
  108. type ReaderFrom interface {
  109. ReadFrom(r Reader) (n int64, err os.Error)
  110. }
  111. // WriterTo is the interface that wraps the WriteTo method.
  112. type WriterTo interface {
  113. WriteTo(w Writer) (n int64, err os.Error)
  114. }
  115. // ReaderAt is the interface that wraps the basic ReadAt method.
  116. //
  117. // ReadAt reads len(p) bytes into p starting at offset off in the
  118. // underlying input source. It returns the number of bytes
  119. // read (0 <= n <= len(p)) and any error encountered.
  120. //
  121. // When ReadAt returns n < len(p), it returns a non-nil error
  122. // explaining why more bytes were not returned. In this respect,
  123. // ReadAt is stricter than Read.
  124. //
  125. // Even if ReadAt returns n < len(p), it may use all of p as scratch
  126. // space during the call. If some data is available but not len(p) bytes,
  127. // ReadAt blocks until either all the data is available or an error occurs.
  128. // In this respect ReadAt is different from Read.
  129. //
  130. // If the n = len(p) bytes returned by ReadAt are at the end of the
  131. // input source, ReadAt may return either err == os.EOF or err == nil.
  132. //
  133. // If ReadAt is reading from an input source with a seek offset,
  134. // ReadAt should not affect nor be affected by the underlying
  135. // seek offset.
  136. type ReaderAt interface {
  137. ReadAt(p []byte, off int64) (n int, err os.Error)
  138. }
  139. // WriterAt is the interface that wraps the basic WriteAt method.
  140. //
  141. // WriteAt writes len(p) bytes from p to the underlying data stream
  142. // at offset off. It returns the number of bytes written from p (0 <= n <= len(p))
  143. // and any error encountered that caused the write to stop early.
  144. // WriteAt must return a non-nil error if it returns n < len(p).
  145. type WriterAt interface {
  146. WriteAt(p []byte, off int64) (n int, err os.Error)
  147. }
  148. // ByteReader is the interface that wraps the ReadByte method.
  149. //
  150. // ReadByte reads and returns the next byte from the input.
  151. // If no byte is available, err will be set.
  152. type ByteReader interface {
  153. ReadByte() (c byte, err os.Error)
  154. }
  155. // ByteScanner is the interface that adds the UnreadByte method to the
  156. // basic ReadByte method.
  157. //
  158. // UnreadByte causes the next call to ReadByte to return the same byte
  159. // as the previous call to ReadByte.
  160. // It may be an error to call UnreadByte twice without an intervening
  161. // call to ReadByte.
  162. type ByteScanner interface {
  163. ByteReader
  164. UnreadByte() os.Error
  165. }
  166. // RuneReader is the interface that wraps the ReadRune method.
  167. //
  168. // ReadRune reads a single UTF-8 encoded Unicode character
  169. // and returns the rune and its size in bytes. If no character is
  170. // available, err will be set.
  171. type RuneReader interface {
  172. ReadRune() (rune int, size int, err os.Error)
  173. }
  174. // RuneScanner is the interface that adds the UnreadRune method to the
  175. // basic ReadRune method.
  176. //
  177. // UnreadRune causes the next call to ReadRune to return the same rune
  178. // as the previous call to ReadRune.
  179. // It may be an error to call UnreadRune twice without an intervening
  180. // call to ReadRune.
  181. type RuneScanner interface {
  182. RuneReader
  183. UnreadRune() os.Error
  184. }
  185. // stringWriter is the interface that wraps the WriteString method.
  186. type stringWriter interface {
  187. WriteString(s string) (n int, err os.Error)
  188. }
  189. // WriteString writes the contents of the string s to w, which accepts an array of bytes.
  190. func WriteString(w Writer, s string) (n int, err os.Error) {
  191. if sw, ok := w.(stringWriter); ok {
  192. return sw.WriteString(s)
  193. }
  194. return w.Write([]byte(s))
  195. }
  196. // ReadAtLeast reads from r into buf until it has read at least min bytes.
  197. // It returns the number of bytes copied and an error if fewer bytes were read.
  198. // The error is os.EOF only if no bytes were read.
  199. // If an EOF happens after reading fewer than min bytes,
  200. // ReadAtLeast returns ErrUnexpectedEOF.
  201. // If min is greater than the length of buf, ReadAtLeast returns ErrShortBuffer.
  202. func ReadAtLeast(r Reader, buf []byte, min int) (n int, err os.Error) {
  203. if len(buf) < min {
  204. return 0, ErrShortBuffer
  205. }
  206. for n < min && err == nil {
  207. var nn int
  208. nn, err = r.Read(buf[n:])
  209. n += nn
  210. }
  211. if err == os.EOF {
  212. if n >= min {
  213. err = nil
  214. } else if n > 0 {
  215. err = ErrUnexpectedEOF
  216. }
  217. }
  218. return
  219. }
  220. // ReadFull reads exactly len(buf) bytes from r into buf.
  221. // It returns the number of bytes copied and an error if fewer bytes were read.
  222. // The error is os.EOF only if no bytes were read.
  223. // If an EOF happens after reading some but not all the bytes,
  224. // ReadFull returns ErrUnexpectedEOF.
  225. func ReadFull(r Reader, buf []byte) (n int, err os.Error) {
  226. return ReadAtLeast(r, buf, len(buf))
  227. }
  228. // Copyn copies n bytes (or until an error) from src to dst.
  229. // It returns the number of bytes copied and the earliest
  230. // error encountered while copying. Because Read can
  231. // return the full amount requested as well as an error
  232. // (including os.EOF), so can Copyn.
  233. //
  234. // If dst implements the ReaderFrom interface,
  235. // the copy is implemented by calling dst.ReadFrom(src).
  236. func Copyn(dst Writer, src Reader, n int64) (written int64, err os.Error) {
  237. // If the writer has a ReadFrom method, use it to do the copy.
  238. // Avoids a buffer allocation and a copy.
  239. if rt, ok := dst.(ReaderFrom); ok {
  240. written, err = rt.ReadFrom(LimitReader(src, n))
  241. if written < n && err == nil {
  242. // rt stopped early; must have been EOF.
  243. err = os.EOF
  244. }
  245. return
  246. }
  247. buf := make([]byte, 32*1024)
  248. for written < n {
  249. l := len(buf)
  250. if d := n - written; d < int64(l) {
  251. l = int(d)
  252. }
  253. nr, er := src.Read(buf[0:l])
  254. if nr > 0 {
  255. nw, ew := dst.Write(buf[0:nr])
  256. if nw > 0 {
  257. written += int64(nw)
  258. }
  259. if ew != nil {
  260. err = ew
  261. break
  262. }
  263. if nr != nw {
  264. err = ErrShortWrite
  265. break
  266. }
  267. }
  268. if er != nil {
  269. err = er
  270. break
  271. }
  272. }
  273. return written, err
  274. }
  275. // Copy copies from src to dst until either EOF is reached
  276. // on src or an error occurs. It returns the number of bytes
  277. // copied and the first error encountered while copying, if any.
  278. //
  279. // A successful Copy returns err == nil, not err == os.EOF.
  280. // Because Copy is defined to read from src until EOF, it does
  281. // not treat an EOF from Read as an error to be reported.
  282. //
  283. // If dst implements the ReaderFrom interface,
  284. // the copy is implemented by calling dst.ReadFrom(src).
  285. // Otherwise, if src implements the WriterTo interface,
  286. // the copy is implemented by calling src.WriteTo(dst).
  287. func Copy(dst Writer, src Reader) (written int64, err os.Error) {
  288. // If the writer has a ReadFrom method, use it to do the copy.
  289. // Avoids an allocation and a copy.
  290. if rt, ok := dst.(ReaderFrom); ok {
  291. return rt.ReadFrom(src)
  292. }
  293. // Similarly, if the reader has a WriteTo method, use it to do the copy.
  294. if wt, ok := src.(WriterTo); ok {
  295. return wt.WriteTo(dst)
  296. }
  297. buf := make([]byte, 32*1024)
  298. for {
  299. nr, er := src.Read(buf)
  300. if nr > 0 {
  301. nw, ew := dst.Write(buf[0:nr])
  302. if nw > 0 {
  303. written += int64(nw)
  304. }
  305. if ew != nil {
  306. err = ew
  307. break
  308. }
  309. if nr != nw {
  310. err = ErrShortWrite
  311. break
  312. }
  313. }
  314. if er == os.EOF {
  315. break
  316. }
  317. if er != nil {
  318. err = er
  319. break
  320. }
  321. }
  322. return written, err
  323. }
  324. // LimitReader returns a Reader that reads from r
  325. // but stops with os.EOF after n bytes.
  326. // The underlying implementation is a *LimitedReader.
  327. func LimitReader(r Reader, n int64) Reader { return &LimitedReader{r, n} }
  328. // A LimitedReader reads from R but limits the amount of
  329. // data returned to just N bytes. Each call to Read
  330. // updates N to reflect the new amount remaining.
  331. type LimitedReader struct {
  332. R Reader // underlying reader
  333. N int64 // max bytes remaining
  334. }
  335. func (l *LimitedReader) Read(p []byte) (n int, err os.Error) {
  336. if l.N <= 0 {
  337. return 0, os.EOF
  338. }
  339. if int64(len(p)) > l.N {
  340. p = p[0:l.N]
  341. }
  342. n, err = l.R.Read(p)
  343. l.N -= int64(n)
  344. return
  345. }
  346. // NewSectionReader returns a SectionReader that reads from r
  347. // starting at offset off and stops with os.EOF after n bytes.
  348. func NewSectionReader(r ReaderAt, off int64, n int64) *SectionReader {
  349. return &SectionReader{r, off, off, off + n}
  350. }
  351. // SectionReader implements Read, Seek, and ReadAt on a section
  352. // of an underlying ReaderAt.
  353. type SectionReader struct {
  354. r ReaderAt
  355. base int64
  356. off int64
  357. limit int64
  358. }
  359. func (s *SectionReader) Read(p []byte) (n int, err os.Error) {
  360. if s.off >= s.limit {
  361. return 0, os.EOF
  362. }
  363. if max := s.limit - s.off; int64(len(p)) > max {
  364. p = p[0:max]
  365. }
  366. n, err = s.r.ReadAt(p, s.off)
  367. s.off += int64(n)
  368. return
  369. }
  370. func (s *SectionReader) Seek(offset int64, whence int) (ret int64, err os.Error) {
  371. switch whence {
  372. default:
  373. return 0, os.EINVAL
  374. case 0:
  375. offset += s.base
  376. case 1:
  377. offset += s.off
  378. case 2:
  379. offset += s.limit
  380. }
  381. if offset < s.base || offset > s.limit {
  382. return 0, os.EINVAL
  383. }
  384. s.off = offset
  385. return offset - s.base, nil
  386. }
  387. func (s *SectionReader) ReadAt(p []byte, off int64) (n int, err os.Error) {
  388. if off < 0 || off >= s.limit-s.base {
  389. return 0, os.EOF
  390. }
  391. off += s.base
  392. if max := s.limit - off; int64(len(p)) > max {
  393. p = p[0:max]
  394. }
  395. return s.r.ReadAt(p, off)
  396. }
  397. // Size returns the size of the section in bytes.
  398. func (s *SectionReader) Size() int64 { return s.limit - s.base }