github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/gnovm/stdlibs/io/io.gno (about) 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 5 // Package io 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. 9 // 10 // Because these interfaces and primitives wrap lower-level operations with 11 // various implementations, unless otherwise informed clients should not 12 // assume they are safe for parallel execution. 13 package io 14 15 import ( 16 "errors" 17 ) 18 19 // TODO: implement rest of io package after sync package added. 20 21 // Seek whence values. 22 const ( 23 SeekStart = 0 // seek relative to the origin of the file 24 SeekCurrent = 1 // seek relative to the current offset 25 SeekEnd = 2 // seek relative to the end 26 ) 27 28 // ErrShortWrite means that a write accepted fewer bytes than requested 29 // but failed to return an explicit error. 30 var ErrShortWrite = errors.New("short write") 31 32 // errInvalidWrite means that a write returned an impossible count. 33 var errInvalidWrite = errors.New("invalid write result") 34 35 // ErrShortBuffer means that a read required a longer buffer than was provided. 36 var ErrShortBuffer = errors.New("short buffer") 37 38 // EOF is the error returned by Read when no more input is available. 39 // (Read must return EOF itself, not an error wrapping EOF, 40 // because callers will test for EOF using ==.) 41 // Functions should return EOF only to signal a graceful end of input. 42 // If the EOF occurs unexpectedly in a structured data stream, 43 // the appropriate error is either ErrUnexpectedEOF or some other error 44 // giving more detail. 45 var EOF = errors.New("EOF") 46 47 // ErrUnexpectedEOF means that EOF was encountered in the 48 // middle of reading a fixed-size block or data structure. 49 var ErrUnexpectedEOF = errors.New("unexpected EOF") 50 51 // ErrNoProgress is returned by some clients of an Reader when 52 // many calls to Read have failed to return any data or error, 53 // usually the sign of a broken Reader implementation. 54 var ErrNoProgress = errors.New("multiple Read calls return no data or error") 55 56 // Reader is the interface that wraps the basic Read method. 57 // 58 // Read reads up to len(p) bytes into p. It returns the number of bytes 59 // read (0 <= n <= len(p)) and any error encountered. Even if Read 60 // returns n < len(p), it may use all of p as scratch space during the call. 61 // If some data is available but not len(p) bytes, Read conventionally 62 // returns what is available instead of waiting for more. 63 // 64 // When Read encounters an error or end-of-file condition after 65 // successfully reading n > 0 bytes, it returns the number of 66 // bytes read. It may return the (non-nil) error from the same call 67 // or return the error (and n == 0) from a subsequent call. 68 // An instance of this general case is that a Reader returning 69 // a non-zero number of bytes at the end of the input stream may 70 // return either err == EOF or err == nil. The next Read should 71 // return 0, EOF. 72 // 73 // Callers should always process the n > 0 bytes returned before 74 // considering the error err. Doing so correctly handles I/O errors 75 // that happen after reading some bytes and also both of the 76 // allowed EOF behaviors. 77 // 78 // Implementations of Read are discouraged from returning a 79 // zero byte count with a nil error, except when len(p) == 0. 80 // Callers should treat a return of 0 and nil as indicating that 81 // nothing happened; in particular it does not indicate EOF. 82 // 83 // Implementations must not retain p. 84 type Reader interface { 85 Read(p []byte) (n int, err error) 86 } 87 88 // Writer is the interface that wraps the basic Write method. 89 // 90 // Write writes len(p) bytes from p to the underlying data stream. 91 // It returns the number of bytes written from p (0 <= n <= len(p)) 92 // and any error encountered that caused the write to stop early. 93 // Write must return a non-nil error if it returns n < len(p). 94 // Write must not modify the slice data, even temporarily. 95 // 96 // Implementations must not retain p. 97 type Writer interface { 98 Write(p []byte) (n int, err error) 99 } 100 101 // Closer is the interface that wraps the basic Close method. 102 // 103 // The behavior of Close after the first call is undefined. 104 // Specific implementations may document their own behavior. 105 type Closer interface { 106 Close() error 107 } 108 109 // Seeker is the interface that wraps the basic Seek method. 110 // 111 // Seek sets the offset for the next Read or Write to offset, 112 // interpreted according to whence: 113 // SeekStart means relative to the start of the file, 114 // SeekCurrent means relative to the current offset, and 115 // SeekEnd means relative to the end. 116 // Seek returns the new offset relative to the start of the 117 // file and an error, if any. 118 // 119 // Seeking to an offset before the start of the file is an error. 120 // Seeking to any positive offset is legal, but the behavior of subsequent 121 // I/O operations on the underlying object is implementation-dependent. 122 type Seeker interface { 123 Seek(offset int64, whence int) (int64, error) 124 } 125 126 // ReadWriter is the interface that groups the basic Read and Write methods. 127 type ReadWriter interface { 128 Reader 129 Writer 130 } 131 132 // ReadCloser is the interface that groups the basic Read and Close methods. 133 type ReadCloser interface { 134 Reader 135 Closer 136 } 137 138 // WriteCloser is the interface that groups the basic Write and Close methods. 139 type WriteCloser interface { 140 Writer 141 Closer 142 } 143 144 // ReadWriteCloser is the interface that groups the basic Read, Write and Close methods. 145 type ReadWriteCloser interface { 146 Reader 147 Writer 148 Closer 149 } 150 151 // ReadSeeker is the interface that groups the basic Read and Seek methods. 152 type ReadSeeker interface { 153 Reader 154 Seeker 155 } 156 157 // ReadSeekCloser is the interface that groups the basic Read, Seek and Close 158 // methods. 159 type ReadSeekCloser interface { 160 Reader 161 Seeker 162 Closer 163 } 164 165 // WriteSeeker is the interface that groups the basic Write and Seek methods. 166 type WriteSeeker interface { 167 Writer 168 Seeker 169 } 170 171 // ReadWriteSeeker is the interface that groups the basic Read, Write and Seek methods. 172 type ReadWriteSeeker interface { 173 Reader 174 Writer 175 Seeker 176 } 177 178 // ReaderFrom is the interface that wraps the ReadFrom method. 179 // 180 // ReadFrom reads data from r until EOF or error. 181 // The return value n is the number of bytes read. 182 // Any error except EOF encountered during the read is also returned. 183 // 184 // The Copy function uses ReaderFrom if available. 185 type ReaderFrom interface { 186 ReadFrom(r Reader) (n int64, err error) 187 } 188 189 // WriterTo is the interface that wraps the WriteTo method. 190 // 191 // WriteTo writes data to w until there's no more data to write or 192 // when an error occurs. The return value n is the number of bytes 193 // written. Any error encountered during the write is also returned. 194 // 195 // The Copy function uses WriterTo if available. 196 type WriterTo interface { 197 WriteTo(w Writer) (n int64, err error) 198 } 199 200 // ReaderAt is the interface that wraps the basic ReadAt method. 201 // 202 // ReadAt reads len(p) bytes into p starting at offset off in the 203 // underlying input source. It returns the number of bytes 204 // read (0 <= n <= len(p)) and any error encountered. 205 // 206 // When ReadAt returns n < len(p), it returns a non-nil error 207 // explaining why more bytes were not returned. In this respect, 208 // ReadAt is stricter than Read. 209 // 210 // Even if ReadAt returns n < len(p), it may use all of p as scratch 211 // space during the call. If some data is available but not len(p) bytes, 212 // ReadAt blocks until either all the data is available or an error occurs. 213 // In this respect ReadAt is different from Read. 214 // 215 // If the n = len(p) bytes returned by ReadAt are at the end of the 216 // input source, ReadAt may return either err == EOF or err == nil. 217 // 218 // If ReadAt is reading from an input source with a seek offset, 219 // ReadAt should not affect nor be affected by the underlying 220 // seek offset. 221 // 222 // Clients of ReadAt can execute parallel ReadAt calls on the 223 // same input source. 224 // 225 // Implementations must not retain p. 226 type ReaderAt interface { 227 ReadAt(p []byte, off int64) (n int, err error) 228 } 229 230 // WriterAt is the interface that wraps the basic WriteAt method. 231 // 232 // WriteAt writes len(p) bytes from p to the underlying data stream 233 // at offset off. It returns the number of bytes written from p (0 <= n <= len(p)) 234 // and any error encountered that caused the write to stop early. 235 // WriteAt must return a non-nil error if it returns n < len(p). 236 // 237 // If WriteAt is writing to a destination with a seek offset, 238 // WriteAt should not affect nor be affected by the underlying 239 // seek offset. 240 // 241 // Clients of WriteAt can execute parallel WriteAt calls on the same 242 // destination if the ranges do not overlap. 243 // 244 // Implementations must not retain p. 245 type WriterAt interface { 246 WriteAt(p []byte, off int64) (n int, err error) 247 } 248 249 // ByteReader is the interface that wraps the ReadByte method. 250 // 251 // ReadByte reads and returns the next byte from the input or 252 // any error encountered. If ReadByte returns an error, no input 253 // byte was consumed, and the returned byte value is undefined. 254 // 255 // ReadByte provides an efficient interface for byte-at-time 256 // processing. A Reader that does not implement ByteReader 257 // can be wrapped using bufio.NewReader to add this method. 258 type ByteReader interface { 259 ReadByte() (byte, error) 260 } 261 262 // ByteScanner is the interface that adds the UnreadByte method to the 263 // basic ReadByte method. 264 // 265 // UnreadByte causes the next call to ReadByte to return the same byte 266 // as the previous call to ReadByte. 267 // It may be an error to call UnreadByte twice without an intervening 268 // call to ReadByte. 269 type ByteScanner interface { 270 ByteReader 271 UnreadByte() error 272 } 273 274 // ByteWriter is the interface that wraps the WriteByte method. 275 type ByteWriter interface { 276 WriteByte(c byte) error 277 } 278 279 // RuneReader is the interface that wraps the ReadRune method. 280 // 281 // ReadRune reads a single UTF-8 encoded Unicode character 282 // and returns the rune and its size in bytes. If no character is 283 // available, err will be set. 284 type RuneReader interface { 285 ReadRune() (r rune, size int, err error) 286 } 287 288 // RuneScanner is the interface that adds the UnreadRune method to the 289 // basic ReadRune method. 290 // 291 // UnreadRune causes the next call to ReadRune to return the same rune 292 // as the previous call to ReadRune. 293 // It may be an error to call UnreadRune twice without an intervening 294 // call to ReadRune. 295 type RuneScanner interface { 296 RuneReader 297 UnreadRune() error 298 } 299 300 // StringWriter is the interface that wraps the WriteString method. 301 type StringWriter interface { 302 WriteString(s string) (n int, err error) 303 } 304 305 // WriteString writes the contents of the string s to w, which accepts a slice of bytes. 306 // If w implements StringWriter, its WriteString method is invoked directly. 307 // Otherwise, w.Write is called exactly once. 308 func WriteString(w Writer, s string) (n int, err error) { 309 if sw, ok := w.(StringWriter); ok { 310 return sw.WriteString(s) 311 } 312 return w.Write([]byte(s)) 313 } 314 315 // ReadAtLeast reads from r into buf until it has read at least min bytes. 316 // It returns the number of bytes copied and an error if fewer bytes were read. 317 // The error is EOF only if no bytes were read. 318 // If an EOF happens after reading fewer than min bytes, 319 // ReadAtLeast returns ErrUnexpectedEOF. 320 // If min is greater than the length of buf, ReadAtLeast returns ErrShortBuffer. 321 // On return, n >= min if and only if err == nil. 322 // If r returns an error having read at least min bytes, the error is dropped. 323 func ReadAtLeast(r Reader, buf []byte, min int) (n int, err error) { 324 if len(buf) < min { 325 return 0, ErrShortBuffer 326 } 327 for n < min && err == nil { 328 var nn int 329 nn, err = r.Read(buf[n:]) 330 n += nn 331 } 332 if n >= min { 333 err = nil 334 } else if n > 0 && err == EOF { 335 err = ErrUnexpectedEOF 336 } 337 return 338 } 339 340 // ReadFull reads exactly len(buf) bytes from r into buf. 341 // It returns the number of bytes copied and an error if fewer bytes were read. 342 // The error is EOF only if no bytes were read. 343 // If an EOF happens after reading some but not all the bytes, 344 // ReadFull returns ErrUnexpectedEOF. 345 // On return, n == len(buf) if and only if err == nil. 346 // If r returns an error having read at least len(buf) bytes, the error is dropped. 347 func ReadFull(r Reader, buf []byte) (n int, err error) { 348 return ReadAtLeast(r, buf, len(buf)) 349 } 350 351 // CopyN copies n bytes (or until an error) from src to dst. 352 // It returns the number of bytes copied and the earliest 353 // error encountered while copying. 354 // On return, written == n if and only if err == nil. 355 // 356 // If dst implements the ReaderFrom interface, 357 // the copy is implemented using it. 358 func CopyN(dst Writer, src Reader, n int64) (written int64, err error) { 359 written, err = Copy(dst, LimitReader(src, n)) 360 if written == n { 361 return n, nil 362 } 363 if written < n && err == nil { 364 // src stopped early; must have been EOF. 365 err = EOF 366 } 367 return 368 } 369 370 // Copy copies from src to dst until either EOF is reached 371 // on src or an error occurs. It returns the number of bytes 372 // copied and the first error encountered while copying, if any. 373 // 374 // A successful Copy returns err == nil, not err == EOF. 375 // Because Copy is defined to read from src until EOF, it does 376 // not treat an EOF from Read as an error to be reported. 377 // 378 // If src implements the WriterTo interface, 379 // the copy is implemented by calling src.WriteTo(dst). 380 // Otherwise, if dst implements the ReaderFrom interface, 381 // the copy is implemented by calling dst.ReadFrom(src). 382 func Copy(dst Writer, src Reader) (written int64, err error) { 383 return copyBuffer(dst, src, nil) 384 } 385 386 // CopyBuffer is identical to Copy except that it stages through the 387 // provided buffer (if one is required) rather than allocating a 388 // temporary one. If buf is nil, one is allocated; otherwise if it has 389 // zero length, CopyBuffer panics. 390 // 391 // If either src implements WriterTo or dst implements ReaderFrom, 392 // buf will not be used to perform the copy. 393 func CopyBuffer(dst Writer, src Reader, buf []byte) (written int64, err error) { 394 if buf != nil && len(buf) == 0 { 395 panic("empty buffer in CopyBuffer") 396 } 397 return copyBuffer(dst, src, buf) 398 } 399 400 // copyBuffer is the actual implementation of Copy and CopyBuffer. 401 // if buf is nil, one is allocated. 402 func copyBuffer(dst Writer, src Reader, buf []byte) (written int64, err error) { 403 // If the reader has a WriteTo method, use it to do the copy. 404 // Avoids an allocation and a copy. 405 if wt, ok := src.(WriterTo); ok { 406 return wt.WriteTo(dst) 407 } 408 // Similarly, if the writer has a ReadFrom method, use it to do the copy. 409 if rt, ok := dst.(ReaderFrom); ok { 410 return rt.ReadFrom(src) 411 } 412 if buf == nil { 413 size := 32 * 1024 414 if l, ok := src.(*LimitedReader); ok && int64(size) > l.N { 415 if l.N < 1 { 416 size = 1 417 } else { 418 size = int(l.N) 419 } 420 } 421 buf = make([]byte, size) 422 } 423 for { 424 nr, er := src.Read(buf) 425 if nr > 0 { 426 nw, ew := dst.Write(buf[0:nr]) 427 if nw < 0 || nr < nw { 428 nw = 0 429 if ew == nil { 430 ew = errInvalidWrite 431 } 432 } 433 written += int64(nw) 434 if ew != nil { 435 err = ew 436 break 437 } 438 if nr != nw { 439 err = ErrShortWrite 440 break 441 } 442 } 443 if er != nil { 444 if er != EOF { 445 err = er 446 } 447 break 448 } 449 } 450 return written, err 451 } 452 453 // LimitReader returns a Reader that reads from r 454 // but stops with EOF after n bytes. 455 // The underlying implementation is a *LimitedReader. 456 func LimitReader(r Reader, n int64) Reader { return &LimitedReader{r, n} } 457 458 // A LimitedReader reads from R but limits the amount of 459 // data returned to just N bytes. Each call to Read 460 // updates N to reflect the new amount remaining. 461 // Read returns EOF when N <= 0 or when the underlying R returns EOF. 462 type LimitedReader struct { 463 R Reader // underlying reader 464 N int64 // max bytes remaining 465 } 466 467 func (l *LimitedReader) Read(p []byte) (n int, err error) { 468 if l.N <= 0 { 469 return 0, EOF 470 } 471 if int64(len(p)) > l.N { 472 p = p[0:l.N] 473 } 474 n, err = l.R.Read(p) 475 l.N -= int64(n) 476 return 477 } 478 479 // NewSectionReader returns a [SectionReader] that reads from r 480 // starting at offset off and stops with EOF after n bytes. 481 func NewSectionReader(r ReaderAt, off int64, n int64) *SectionReader { 482 var remaining int64 483 const maxint64 = 1<<63 - 1 484 if off <= maxint64-n { 485 remaining = n + off 486 } else { 487 // Overflow, with no way to return error. 488 // Assume we can read up to an offset of 1<<63 - 1. 489 remaining = maxint64 490 } 491 return &SectionReader{r, off, off, remaining, n} 492 } 493 494 // SectionReader implements Read, Seek, and ReadAt on a section 495 // of an underlying [ReaderAt]. 496 type SectionReader struct { 497 r ReaderAt // constant after creation 498 base int64 // constant after creation 499 off int64 500 limit int64 // constant after creation 501 n int64 // constant after creation 502 } 503 504 func (s *SectionReader) Read(p []byte) (n int, err error) { 505 if s.off >= s.limit { 506 return 0, EOF 507 } 508 if max := s.limit - s.off; int64(len(p)) > max { 509 p = p[0:max] 510 } 511 n, err = s.r.ReadAt(p, s.off) 512 s.off += int64(n) 513 return 514 } 515 516 var ( 517 errWhence = errors.New("Seek: invalid whence") 518 errOffset = errors.New("Seek: invalid offset") 519 ) 520 521 func (s *SectionReader) Seek(offset int64, whence int) (int64, error) { 522 switch whence { 523 default: 524 return 0, errWhence 525 case SeekStart: 526 offset += s.base 527 case SeekCurrent: 528 offset += s.off 529 case SeekEnd: 530 offset += s.limit 531 } 532 if offset < s.base { 533 return 0, errOffset 534 } 535 s.off = offset 536 return offset - s.base, nil 537 } 538 539 func (s *SectionReader) ReadAt(p []byte, off int64) (n int, err error) { 540 if off < 0 || off >= s.Size() { 541 return 0, EOF 542 } 543 off += s.base 544 if max := s.limit - off; int64(len(p)) > max { 545 p = p[0:max] 546 n, err = s.r.ReadAt(p, off) 547 if err == nil { 548 err = EOF 549 } 550 return n, err 551 } 552 return s.r.ReadAt(p, off) 553 } 554 555 // Size returns the size of the section in bytes. 556 func (s *SectionReader) Size() int64 { return s.limit - s.base } 557 558 // Outer returns the underlying [ReaderAt] and offsets for the section. 559 // 560 // The returned values are the same that were passed to [NewSectionReader] 561 // when the [SectionReader] was created. 562 func (s *SectionReader) Outer() (r ReaderAt, off int64, n int64) { 563 return s.r, s.base, s.n 564 } 565 566 // An OffsetWriter maps writers at offset base to offset base + off in the underlying writer. 567 type OffsetWriter struct { 568 w WriterAt 569 base int64 // the original offset 570 off int64 // the current offset 571 } 572 573 // NewOffsetWriter returns a new OffsetWriter that writes to w starting at offset off. 574 func NewOffsetWriter(w WriterAt, off int64) *OffsetWriter { 575 return &OffsetWriter{w: w, off: off} 576 } 577 578 func (o *OffsetWriter) Write(p []byte) (n int, err error) { 579 // n, err = o.w.WriterAt(p, o.off) 580 wa := o.w 581 n, err = wa.WriteAt(p, o.off) 582 o.off += int64(n) 583 return 584 } 585 586 func (o *OffsetWriter) WriteAt(p []byte, off int64) (n int, err error) { 587 if off < 0 { 588 return 0, errOffset 589 } 590 591 off += o.base 592 return o.w.WriteAt(p, off) 593 } 594 595 func (o *OffsetWriter) Seek(offset int64, whence int) (int64, error) { 596 switch whence { 597 default: 598 return 0, errWhence 599 case SeekStart: 600 offset += o.base 601 case SeekCurrent: 602 offset += o.off 603 } 604 605 if offset < o.base { 606 return 0, errOffset 607 } 608 609 o.off = offset 610 return offset - o.base, nil 611 } 612 613 // TeeReader returns a Reader that writes to w what it reads from r. 614 // All reads from r performed through it are matched with 615 // corresponding writes to w. There is no internal buffering - 616 // the write must complete before the read completes. 617 // Any error encountered while writing is reported as a read error. 618 func TeeReader(r Reader, w Writer) Reader { 619 return &teeReader{r, w} 620 } 621 622 type teeReader struct { 623 r Reader 624 w Writer 625 } 626 627 func (t *teeReader) Read(p []byte) (n int, err error) { 628 n, err = t.r.Read(p) 629 if n > 0 { 630 if n, err := t.w.Write(p[:n]); err != nil { 631 return n, err 632 } 633 } 634 return 635 } 636 637 // Discard is a Writer on which all Write calls succeed 638 // without doing anything. 639 var Discard Writer = discard{} 640 641 type discard struct{} 642 643 // discard implements ReaderFrom as an optimization so Copy to 644 // io.Discard can avoid doing unnecessary work. 645 var _ ReaderFrom = discard{} 646 647 func (discard) Write(p []byte) (int, error) { 648 return len(p), nil 649 } 650 651 func (discard) WriteString(s string) (int, error) { 652 return len(s), nil 653 } 654 655 /* 656 var blackHolePool = sync.Pool{ 657 New: func() interface{} { 658 b := make([]byte, 8192) 659 return &b 660 }, 661 } 662 */ 663 664 // XXX modified to remove reference to blackHolePool 665 func (discard) ReadFrom(r Reader) (n int64, err error) { 666 // bufp := blackHolePool.Get().(*[]byte) 667 buf := make([]byte, 8192) 668 readSize := 0 669 for { 670 readSize, err = r.Read(buf) 671 n += int64(readSize) 672 if err != nil { 673 // blackHolePool.Put(bufp) 674 if err == EOF { 675 return n, nil 676 } 677 return 678 } 679 } 680 } 681 682 // NopCloser returns a ReadCloser with a no-op Close method wrapping 683 // the provided Reader r. 684 // If r implements WriterTo, the returned ReadCloser will implement WriterTo 685 // by forwarding calls to r. 686 func NopCloser(r Reader) ReadCloser { 687 if _, ok := r.(WriterTo); ok { 688 return nopCloserWriterTo{r} 689 } 690 return nopCloser{r} 691 } 692 693 type nopCloser struct { 694 Reader 695 } 696 697 func (nopCloser) Close() error { return nil } 698 699 type nopCloserWriterTo struct { 700 Reader 701 } 702 703 func (nopCloserWriterTo) Close() error { return nil } 704 705 func (c nopCloserWriterTo) WriteTo(w Writer) (n int64, err error) { 706 return c.Reader.(WriterTo).WriteTo(w) 707 } 708 709 // ReadAll reads from r until an error or EOF and returns the data it read. 710 // A successful call returns err == nil, not err == EOF. Because ReadAll is 711 // defined to read from src until EOF, it does not treat an EOF from Read 712 // as an error to be reported. 713 func ReadAll(r Reader) ([]byte, error) { 714 b := make([]byte, 0, 512) 715 for { 716 if len(b) == cap(b) { 717 // Add more capacity (let append pick how much). 718 b = append(b, 0)[:len(b)] 719 } 720 n, err := r.Read(b[len(b):cap(b)]) 721 b = b[:len(b)+n] 722 if err != nil { 723 if err == EOF { 724 err = nil 725 } 726 return b, err 727 } 728 if len(b) == cap(b) { 729 // Add more capacity (let append pick how much). 730 b = append(b, 0)[:len(b)] 731 } 732 } 733 }