PageRenderTime 35ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

/third_party/gofrontend/libgo/go/io/io.go

http://github.com/axw/llgo
Go | 521 lines | 250 code | 48 blank | 223 comment | 48 complexity | 7b625c41a195f1d63fe06f976bf3037a MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  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. //
  9. // Because these interfaces and primitives wrap lower-level operations with
  10. // various implementations, unless otherwise informed clients should not
  11. // assume they are safe for parallel execution.
  12. package io
  13. import (
  14. "errors"
  15. )
  16. // ErrShortWrite means that a write accepted fewer bytes than requested
  17. // but failed to return an explicit error.
  18. var ErrShortWrite = errors.New("short write")
  19. // ErrShortBuffer means that a read required a longer buffer than was provided.
  20. var ErrShortBuffer = errors.New("short buffer")
  21. // EOF is the error returned by Read when no more input is available.
  22. // Functions should return EOF only to signal a graceful end of input.
  23. // If the EOF occurs unexpectedly in a structured data stream,
  24. // the appropriate error is either ErrUnexpectedEOF or some other error
  25. // giving more detail.
  26. var EOF = errors.New("EOF")
  27. // ErrUnexpectedEOF means that EOF was encountered in the
  28. // middle of reading a fixed-size block or data structure.
  29. var ErrUnexpectedEOF = errors.New("unexpected EOF")
  30. // ErrNoProgress is returned by some clients of an io.Reader when
  31. // many calls to Read have failed to return any data or error,
  32. // usually the sign of a broken io.Reader implementation.
  33. var ErrNoProgress = errors.New("multiple Read calls return no data or error")
  34. // Reader is the interface that wraps the basic Read method.
  35. //
  36. // Read reads up to len(p) bytes into p. It returns the number of bytes
  37. // read (0 <= n <= len(p)) and any error encountered. Even if Read
  38. // returns n < len(p), it may use all of p as scratch space during the call.
  39. // If some data is available but not len(p) bytes, Read conventionally
  40. // returns what is available instead of waiting for more.
  41. //
  42. // When Read encounters an error or end-of-file condition after
  43. // successfully reading n > 0 bytes, it returns the number of
  44. // bytes read. It may return the (non-nil) error from the same call
  45. // or return the error (and n == 0) from a subsequent call.
  46. // An instance of this general case is that a Reader returning
  47. // a non-zero number of bytes at the end of the input stream may
  48. // return either err == EOF or err == nil. The next Read should
  49. // return 0, EOF.
  50. //
  51. // Callers should always process the n > 0 bytes returned before
  52. // considering the error err. Doing so correctly handles I/O errors
  53. // that happen after reading some bytes and also both of the
  54. // allowed EOF behaviors.
  55. //
  56. // Implementations of Read are discouraged from returning a
  57. // zero byte count with a nil error, except when len(p) == 0.
  58. // Callers should treat a return of 0 and nil as indicating that
  59. // nothing happened; in particular it does not indicate EOF.
  60. //
  61. // Implementations must not retain p.
  62. type Reader interface {
  63. Read(p []byte) (n int, err error)
  64. }
  65. // Writer is the interface that wraps the basic Write method.
  66. //
  67. // Write writes len(p) bytes from p to the underlying data stream.
  68. // It returns the number of bytes written from p (0 <= n <= len(p))
  69. // and any error encountered that caused the write to stop early.
  70. // Write must return a non-nil error if it returns n < len(p).
  71. // Write must not modify the slice data, even temporarily.
  72. //
  73. // Implementations must not retain p.
  74. type Writer interface {
  75. Write(p []byte) (n int, err error)
  76. }
  77. // Closer is the interface that wraps the basic Close method.
  78. //
  79. // The behavior of Close after the first call is undefined.
  80. // Specific implementations may document their own behavior.
  81. type Closer interface {
  82. Close() error
  83. }
  84. // Seeker is the interface that wraps the basic Seek method.
  85. //
  86. // Seek sets the offset for the next Read or Write to offset,
  87. // interpreted according to whence: 0 means relative to the origin of
  88. // the file, 1 means relative to the current offset, and 2 means
  89. // relative to the end. Seek returns the new offset and an error, if
  90. // any.
  91. //
  92. // Seeking to a negative offset is an error. Seeking to any positive
  93. // offset is legal, but the behavior of subsequent I/O operations on
  94. // the underlying object is implementation-dependent.
  95. type Seeker interface {
  96. Seek(offset int64, whence int) (int64, error)
  97. }
  98. // ReadWriter is the interface that groups the basic Read and Write methods.
  99. type ReadWriter interface {
  100. Reader
  101. Writer
  102. }
  103. // ReadCloser is the interface that groups the basic Read and Close methods.
  104. type ReadCloser interface {
  105. Reader
  106. Closer
  107. }
  108. // WriteCloser is the interface that groups the basic Write and Close methods.
  109. type WriteCloser interface {
  110. Writer
  111. Closer
  112. }
  113. // ReadWriteCloser is the interface that groups the basic Read, Write and Close methods.
  114. type ReadWriteCloser interface {
  115. Reader
  116. Writer
  117. Closer
  118. }
  119. // ReadSeeker is the interface that groups the basic Read and Seek methods.
  120. type ReadSeeker interface {
  121. Reader
  122. Seeker
  123. }
  124. // WriteSeeker is the interface that groups the basic Write and Seek methods.
  125. type WriteSeeker interface {
  126. Writer
  127. Seeker
  128. }
  129. // ReadWriteSeeker is the interface that groups the basic Read, Write and Seek methods.
  130. type ReadWriteSeeker interface {
  131. Reader
  132. Writer
  133. Seeker
  134. }
  135. // ReaderFrom is the interface that wraps the ReadFrom method.
  136. //
  137. // ReadFrom reads data from r until EOF or error.
  138. // The return value n is the number of bytes read.
  139. // Any error except io.EOF encountered during the read is also returned.
  140. //
  141. // The Copy function uses ReaderFrom if available.
  142. type ReaderFrom interface {
  143. ReadFrom(r Reader) (n int64, err error)
  144. }
  145. // WriterTo is the interface that wraps the WriteTo method.
  146. //
  147. // WriteTo writes data to w until there's no more data to write or
  148. // when an error occurs. The return value n is the number of bytes
  149. // written. Any error encountered during the write is also returned.
  150. //
  151. // The Copy function uses WriterTo if available.
  152. type WriterTo interface {
  153. WriteTo(w Writer) (n int64, err error)
  154. }
  155. // ReaderAt is the interface that wraps the basic ReadAt method.
  156. //
  157. // ReadAt reads len(p) bytes into p starting at offset off in the
  158. // underlying input source. It returns the number of bytes
  159. // read (0 <= n <= len(p)) and any error encountered.
  160. //
  161. // When ReadAt returns n < len(p), it returns a non-nil error
  162. // explaining why more bytes were not returned. In this respect,
  163. // ReadAt is stricter than Read.
  164. //
  165. // Even if ReadAt returns n < len(p), it may use all of p as scratch
  166. // space during the call. If some data is available but not len(p) bytes,
  167. // ReadAt blocks until either all the data is available or an error occurs.
  168. // In this respect ReadAt is different from Read.
  169. //
  170. // If the n = len(p) bytes returned by ReadAt are at the end of the
  171. // input source, ReadAt may return either err == EOF or err == nil.
  172. //
  173. // If ReadAt is reading from an input source with a seek offset,
  174. // ReadAt should not affect nor be affected by the underlying
  175. // seek offset.
  176. //
  177. // Clients of ReadAt can execute parallel ReadAt calls on the
  178. // same input source.
  179. //
  180. // Implementations must not retain p.
  181. type ReaderAt interface {
  182. ReadAt(p []byte, off int64) (n int, err error)
  183. }
  184. // WriterAt is the interface that wraps the basic WriteAt method.
  185. //
  186. // WriteAt writes len(p) bytes from p to the underlying data stream
  187. // at offset off. It returns the number of bytes written from p (0 <= n <= len(p))
  188. // and any error encountered that caused the write to stop early.
  189. // WriteAt must return a non-nil error if it returns n < len(p).
  190. //
  191. // If WriteAt is writing to a destination with a seek offset,
  192. // WriteAt should not affect nor be affected by the underlying
  193. // seek offset.
  194. //
  195. // Clients of WriteAt can execute parallel WriteAt calls on the same
  196. // destination if the ranges do not overlap.
  197. //
  198. // Implementations must not retain p.
  199. type WriterAt interface {
  200. WriteAt(p []byte, off int64) (n int, err error)
  201. }
  202. // ByteReader is the interface that wraps the ReadByte method.
  203. //
  204. // ReadByte reads and returns the next byte from the input.
  205. // If no byte is available, err will be set.
  206. type ByteReader interface {
  207. ReadByte() (c byte, err error)
  208. }
  209. // ByteScanner is the interface that adds the UnreadByte method to the
  210. // basic ReadByte method.
  211. //
  212. // UnreadByte causes the next call to ReadByte to return the same byte
  213. // as the previous call to ReadByte.
  214. // It may be an error to call UnreadByte twice without an intervening
  215. // call to ReadByte.
  216. type ByteScanner interface {
  217. ByteReader
  218. UnreadByte() error
  219. }
  220. // ByteWriter is the interface that wraps the WriteByte method.
  221. type ByteWriter interface {
  222. WriteByte(c byte) error
  223. }
  224. // RuneReader is the interface that wraps the ReadRune method.
  225. //
  226. // ReadRune reads a single UTF-8 encoded Unicode character
  227. // and returns the rune and its size in bytes. If no character is
  228. // available, err will be set.
  229. type RuneReader interface {
  230. ReadRune() (r rune, size int, err error)
  231. }
  232. // RuneScanner is the interface that adds the UnreadRune method to the
  233. // basic ReadRune method.
  234. //
  235. // UnreadRune causes the next call to ReadRune to return the same rune
  236. // as the previous call to ReadRune.
  237. // It may be an error to call UnreadRune twice without an intervening
  238. // call to ReadRune.
  239. type RuneScanner interface {
  240. RuneReader
  241. UnreadRune() error
  242. }
  243. // stringWriter is the interface that wraps the WriteString method.
  244. type stringWriter interface {
  245. WriteString(s string) (n int, err error)
  246. }
  247. // WriteString writes the contents of the string s to w, which accepts a slice of bytes.
  248. // If w implements a WriteString method, it is invoked directly.
  249. func WriteString(w Writer, s string) (n int, err error) {
  250. if sw, ok := w.(stringWriter); ok {
  251. return sw.WriteString(s)
  252. }
  253. return w.Write([]byte(s))
  254. }
  255. // ReadAtLeast reads from r into buf until it has read at least min bytes.
  256. // It returns the number of bytes copied and an error if fewer bytes were read.
  257. // The error is EOF only if no bytes were read.
  258. // If an EOF happens after reading fewer than min bytes,
  259. // ReadAtLeast returns ErrUnexpectedEOF.
  260. // If min is greater than the length of buf, ReadAtLeast returns ErrShortBuffer.
  261. // On return, n >= min if and only if err == nil.
  262. func ReadAtLeast(r Reader, buf []byte, min int) (n int, err error) {
  263. if len(buf) < min {
  264. return 0, ErrShortBuffer
  265. }
  266. for n < min && err == nil {
  267. var nn int
  268. nn, err = r.Read(buf[n:])
  269. n += nn
  270. }
  271. if n >= min {
  272. err = nil
  273. } else if n > 0 && err == EOF {
  274. err = ErrUnexpectedEOF
  275. }
  276. return
  277. }
  278. // ReadFull reads exactly len(buf) bytes from r into buf.
  279. // It returns the number of bytes copied and an error if fewer bytes were read.
  280. // The error is EOF only if no bytes were read.
  281. // If an EOF happens after reading some but not all the bytes,
  282. // ReadFull returns ErrUnexpectedEOF.
  283. // On return, n == len(buf) if and only if err == nil.
  284. func ReadFull(r Reader, buf []byte) (n int, err error) {
  285. return ReadAtLeast(r, buf, len(buf))
  286. }
  287. // CopyN copies n bytes (or until an error) from src to dst.
  288. // It returns the number of bytes copied and the earliest
  289. // error encountered while copying.
  290. // On return, written == n if and only if err == nil.
  291. //
  292. // If dst implements the ReaderFrom interface,
  293. // the copy is implemented using it.
  294. func CopyN(dst Writer, src Reader, n int64) (written int64, err error) {
  295. written, err = Copy(dst, LimitReader(src, n))
  296. if written == n {
  297. return n, nil
  298. }
  299. if written < n && err == nil {
  300. // src stopped early; must have been EOF.
  301. err = EOF
  302. }
  303. return
  304. }
  305. // Copy copies from src to dst until either EOF is reached
  306. // on src or an error occurs. It returns the number of bytes
  307. // copied and the first error encountered while copying, if any.
  308. //
  309. // A successful Copy returns err == nil, not err == EOF.
  310. // Because Copy is defined to read from src until EOF, it does
  311. // not treat an EOF from Read as an error to be reported.
  312. //
  313. // If src implements the WriterTo interface,
  314. // the copy is implemented by calling src.WriteTo(dst).
  315. // Otherwise, if dst implements the ReaderFrom interface,
  316. // the copy is implemented by calling dst.ReadFrom(src).
  317. func Copy(dst Writer, src Reader) (written int64, err error) {
  318. return copyBuffer(dst, src, nil)
  319. }
  320. // CopyBuffer is identical to Copy except that it stages through the
  321. // provided buffer (if one is required) rather than allocating a
  322. // temporary one. If buf is nil, one is allocated; otherwise if it has
  323. // zero length, CopyBuffer panics.
  324. func CopyBuffer(dst Writer, src Reader, buf []byte) (written int64, err error) {
  325. if buf != nil && len(buf) == 0 {
  326. panic("empty buffer in io.CopyBuffer")
  327. }
  328. return copyBuffer(dst, src, buf)
  329. }
  330. // copyBuffer is the actual implementation of Copy and CopyBuffer.
  331. // if buf is nil, one is allocated.
  332. func copyBuffer(dst Writer, src Reader, buf []byte) (written int64, err error) {
  333. // If the reader has a WriteTo method, use it to do the copy.
  334. // Avoids an allocation and a copy.
  335. if wt, ok := src.(WriterTo); ok {
  336. return wt.WriteTo(dst)
  337. }
  338. // Similarly, if the writer has a ReadFrom method, use it to do the copy.
  339. if rt, ok := dst.(ReaderFrom); ok {
  340. return rt.ReadFrom(src)
  341. }
  342. if buf == nil {
  343. buf = make([]byte, 32*1024)
  344. }
  345. for {
  346. nr, er := src.Read(buf)
  347. if nr > 0 {
  348. nw, ew := dst.Write(buf[0:nr])
  349. if nw > 0 {
  350. written += int64(nw)
  351. }
  352. if ew != nil {
  353. err = ew
  354. break
  355. }
  356. if nr != nw {
  357. err = ErrShortWrite
  358. break
  359. }
  360. }
  361. if er == EOF {
  362. break
  363. }
  364. if er != nil {
  365. err = er
  366. break
  367. }
  368. }
  369. return written, err
  370. }
  371. // LimitReader returns a Reader that reads from r
  372. // but stops with EOF after n bytes.
  373. // The underlying implementation is a *LimitedReader.
  374. func LimitReader(r Reader, n int64) Reader { return &LimitedReader{r, n} }
  375. // A LimitedReader reads from R but limits the amount of
  376. // data returned to just N bytes. Each call to Read
  377. // updates N to reflect the new amount remaining.
  378. type LimitedReader struct {
  379. R Reader // underlying reader
  380. N int64 // max bytes remaining
  381. }
  382. func (l *LimitedReader) Read(p []byte) (n int, err error) {
  383. if l.N <= 0 {
  384. return 0, EOF
  385. }
  386. if int64(len(p)) > l.N {
  387. p = p[0:l.N]
  388. }
  389. n, err = l.R.Read(p)
  390. l.N -= int64(n)
  391. return
  392. }
  393. // NewSectionReader returns a SectionReader that reads from r
  394. // starting at offset off and stops with EOF after n bytes.
  395. func NewSectionReader(r ReaderAt, off int64, n int64) *SectionReader {
  396. return &SectionReader{r, off, off, off + n}
  397. }
  398. // SectionReader implements Read, Seek, and ReadAt on a section
  399. // of an underlying ReaderAt.
  400. type SectionReader struct {
  401. r ReaderAt
  402. base int64
  403. off int64
  404. limit int64
  405. }
  406. func (s *SectionReader) Read(p []byte) (n int, err error) {
  407. if s.off >= s.limit {
  408. return 0, EOF
  409. }
  410. if max := s.limit - s.off; int64(len(p)) > max {
  411. p = p[0:max]
  412. }
  413. n, err = s.r.ReadAt(p, s.off)
  414. s.off += int64(n)
  415. return
  416. }
  417. var errWhence = errors.New("Seek: invalid whence")
  418. var errOffset = errors.New("Seek: invalid offset")
  419. func (s *SectionReader) Seek(offset int64, whence int) (int64, error) {
  420. switch whence {
  421. default:
  422. return 0, errWhence
  423. case 0:
  424. offset += s.base
  425. case 1:
  426. offset += s.off
  427. case 2:
  428. offset += s.limit
  429. }
  430. if offset < s.base {
  431. return 0, errOffset
  432. }
  433. s.off = offset
  434. return offset - s.base, nil
  435. }
  436. func (s *SectionReader) ReadAt(p []byte, off int64) (n int, err error) {
  437. if off < 0 || off >= s.limit-s.base {
  438. return 0, EOF
  439. }
  440. off += s.base
  441. if max := s.limit - off; int64(len(p)) > max {
  442. p = p[0:max]
  443. n, err = s.r.ReadAt(p, off)
  444. if err == nil {
  445. err = EOF
  446. }
  447. return n, err
  448. }
  449. return s.r.ReadAt(p, off)
  450. }
  451. // Size returns the size of the section in bytes.
  452. func (s *SectionReader) Size() int64 { return s.limit - s.base }
  453. // TeeReader returns a Reader that writes to w what it reads from r.
  454. // All reads from r performed through it are matched with
  455. // corresponding writes to w. There is no internal buffering -
  456. // the write must complete before the read completes.
  457. // Any error encountered while writing is reported as a read error.
  458. func TeeReader(r Reader, w Writer) Reader {
  459. return &teeReader{r, w}
  460. }
  461. type teeReader struct {
  462. r Reader
  463. w Writer
  464. }
  465. func (t *teeReader) Read(p []byte) (n int, err error) {
  466. n, err = t.r.Read(p)
  467. if n > 0 {
  468. if n, err := t.w.Write(p[:n]); err != nil {
  469. return n, err
  470. }
  471. }
  472. return
  473. }