2010-12-03 05:34:57 +01:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
// Package io provides basic interfaces to I/O primitives.
|
2010-12-03 05:34:57 +01:00
|
|
|
// Its primary job is to wrap existing implementations of such primitives,
|
|
|
|
// such as those in package os, into shared public interfaces that
|
|
|
|
// abstract the functionality, plus some other related primitives.
|
2012-03-06 18:57:23 +01:00
|
|
|
//
|
|
|
|
// Because these interfaces and primitives wrap lower-level operations with
|
|
|
|
// various implementations, unless otherwise informed clients should not
|
|
|
|
// assume they are safe for parallel execution.
|
2010-12-03 05:34:57 +01:00
|
|
|
package io
|
|
|
|
|
2012-02-09 09:19:58 +01:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
)
|
2011-09-16 17:47:21 +02:00
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// ErrShortWrite means that a write accepted fewer bytes than requested
|
|
|
|
// but failed to return an explicit error.
|
2012-02-09 09:19:58 +01:00
|
|
|
var ErrShortWrite = errors.New("short write")
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// ErrShortBuffer means that a read required a longer buffer than was provided.
|
2012-02-09 09:19:58 +01:00
|
|
|
var ErrShortBuffer = errors.New("short buffer")
|
2011-12-03 03:17:34 +01:00
|
|
|
|
|
|
|
// EOF is the error returned by Read when no more input is available.
|
|
|
|
// Functions should return EOF only to signal a graceful end of input.
|
|
|
|
// If the EOF occurs unexpectedly in a structured data stream,
|
|
|
|
// the appropriate error is either ErrUnexpectedEOF or some other error
|
|
|
|
// giving more detail.
|
2012-02-09 09:19:58 +01:00
|
|
|
var EOF = errors.New("EOF")
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
// ErrUnexpectedEOF means that EOF was encountered in the
|
2010-12-03 05:34:57 +01:00
|
|
|
// middle of reading a fixed-size block or data structure.
|
2012-02-09 09:19:58 +01:00
|
|
|
var ErrUnexpectedEOF = errors.New("unexpected EOF")
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
// ErrNoProgress is returned by some clients of an io.Reader when
|
|
|
|
// many calls to Read have failed to return any data or error,
|
|
|
|
// usually the sign of a broken io.Reader implementation.
|
|
|
|
var ErrNoProgress = errors.New("multiple Read calls return no data or error")
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// Reader is the interface that wraps the basic Read method.
|
|
|
|
//
|
|
|
|
// Read reads up to len(p) bytes into p. It returns the number of bytes
|
2011-09-16 17:47:21 +02:00
|
|
|
// read (0 <= n <= len(p)) and any error encountered. Even if Read
|
|
|
|
// returns n < len(p), it may use all of p as scratch space during the call.
|
2010-12-03 05:34:57 +01:00
|
|
|
// If some data is available but not len(p) bytes, Read conventionally
|
2011-09-16 17:47:21 +02:00
|
|
|
// returns what is available instead of waiting for more.
|
2010-12-03 05:34:57 +01:00
|
|
|
//
|
2011-09-16 17:47:21 +02:00
|
|
|
// When Read encounters an error or end-of-file condition after
|
|
|
|
// successfully reading n > 0 bytes, it returns the number of
|
|
|
|
// bytes read. It may return the (non-nil) error from the same call
|
|
|
|
// or return the error (and n == 0) from a subsequent call.
|
|
|
|
// An instance of this general case is that a Reader returning
|
|
|
|
// a non-zero number of bytes at the end of the input stream may
|
2011-12-03 03:17:34 +01:00
|
|
|
// return either err == EOF or err == nil. The next Read should
|
|
|
|
// return 0, EOF regardless.
|
2011-09-16 17:47:21 +02:00
|
|
|
//
|
|
|
|
// Callers should always process the n > 0 bytes returned before
|
|
|
|
// considering the error err. Doing so correctly handles I/O errors
|
|
|
|
// that happen after reading some bytes and also both of the
|
|
|
|
// allowed EOF behaviors.
|
2013-07-16 08:54:42 +02:00
|
|
|
//
|
|
|
|
// Implementations of Read are discouraged from returning a
|
|
|
|
// zero byte count with a nil error, and callers should treat
|
|
|
|
// that situation as a no-op.
|
2010-12-03 05:34:57 +01:00
|
|
|
type Reader interface {
|
2011-12-03 03:17:34 +01:00
|
|
|
Read(p []byte) (n int, err error)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Writer is the interface that wraps the basic Write method.
|
|
|
|
//
|
|
|
|
// Write writes len(p) bytes from p to the underlying data stream.
|
|
|
|
// It returns the number of bytes written from p (0 <= n <= len(p))
|
|
|
|
// and any error encountered that caused the write to stop early.
|
|
|
|
// Write must return a non-nil error if it returns n < len(p).
|
2014-07-19 10:53:52 +02:00
|
|
|
// Write must not modify the slice data, even temporarily.
|
2010-12-03 05:34:57 +01:00
|
|
|
type Writer interface {
|
2011-12-03 03:17:34 +01:00
|
|
|
Write(p []byte) (n int, err error)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Closer is the interface that wraps the basic Close method.
|
2013-07-16 08:54:42 +02:00
|
|
|
//
|
|
|
|
// The behavior of Close after the first call is undefined.
|
|
|
|
// Specific implementations may document their own behavior.
|
2010-12-03 05:34:57 +01:00
|
|
|
type Closer interface {
|
2011-12-03 03:17:34 +01:00
|
|
|
Close() error
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Seeker is the interface that wraps the basic Seek method.
|
|
|
|
//
|
|
|
|
// Seek sets the offset for the next Read or Write to offset,
|
|
|
|
// interpreted according to whence: 0 means relative to the origin of
|
|
|
|
// the file, 1 means relative to the current offset, and 2 means
|
2013-11-06 20:49:01 +01:00
|
|
|
// relative to the end. Seek returns the new offset and an error, if
|
2010-12-03 05:34:57 +01:00
|
|
|
// any.
|
2013-11-06 20:49:01 +01:00
|
|
|
//
|
|
|
|
// Seeking to a negative offset is an error. Seeking to any positive
|
|
|
|
// offset is legal, but the behavior of subsequent I/O operations on
|
|
|
|
// the underlying object is implementation-dependent.
|
2010-12-03 05:34:57 +01:00
|
|
|
type Seeker interface {
|
2013-11-06 20:49:01 +01:00
|
|
|
Seek(offset int64, whence int) (int64, error)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadWriter is the interface that groups the basic Read and Write methods.
|
|
|
|
type ReadWriter interface {
|
|
|
|
Reader
|
|
|
|
Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadCloser is the interface that groups the basic Read and Close methods.
|
|
|
|
type ReadCloser interface {
|
|
|
|
Reader
|
|
|
|
Closer
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteCloser is the interface that groups the basic Write and Close methods.
|
|
|
|
type WriteCloser interface {
|
|
|
|
Writer
|
|
|
|
Closer
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadWriteCloser is the interface that groups the basic Read, Write and Close methods.
|
|
|
|
type ReadWriteCloser interface {
|
|
|
|
Reader
|
|
|
|
Writer
|
|
|
|
Closer
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadSeeker is the interface that groups the basic Read and Seek methods.
|
|
|
|
type ReadSeeker interface {
|
|
|
|
Reader
|
|
|
|
Seeker
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteSeeker is the interface that groups the basic Write and Seek methods.
|
|
|
|
type WriteSeeker interface {
|
|
|
|
Writer
|
|
|
|
Seeker
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadWriteSeeker is the interface that groups the basic Read, Write and Seek methods.
|
|
|
|
type ReadWriteSeeker interface {
|
|
|
|
Reader
|
|
|
|
Writer
|
|
|
|
Seeker
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReaderFrom is the interface that wraps the ReadFrom method.
|
2012-10-03 07:27:36 +02:00
|
|
|
//
|
|
|
|
// ReadFrom reads data from r until EOF or error.
|
|
|
|
// The return value n is the number of bytes read.
|
|
|
|
// Any error except io.EOF encountered during the read is also returned.
|
|
|
|
//
|
|
|
|
// The Copy function uses ReaderFrom if available.
|
2010-12-03 05:34:57 +01:00
|
|
|
type ReaderFrom interface {
|
2011-12-03 03:17:34 +01:00
|
|
|
ReadFrom(r Reader) (n int64, err error)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriterTo is the interface that wraps the WriteTo method.
|
2012-10-03 07:27:36 +02:00
|
|
|
//
|
|
|
|
// WriteTo writes data to w until there's no more data to write or
|
|
|
|
// when an error occurs. The return value n is the number of bytes
|
|
|
|
// written. Any error encountered during the write is also returned.
|
|
|
|
//
|
|
|
|
// The Copy function uses WriterTo if available.
|
2010-12-03 05:34:57 +01:00
|
|
|
type WriterTo interface {
|
2011-12-03 03:17:34 +01:00
|
|
|
WriteTo(w Writer) (n int64, err error)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReaderAt is the interface that wraps the basic ReadAt method.
|
|
|
|
//
|
|
|
|
// ReadAt reads len(p) bytes into p starting at offset off in the
|
2011-09-16 17:47:21 +02:00
|
|
|
// underlying input source. It returns the number of bytes
|
2010-12-03 05:34:57 +01:00
|
|
|
// read (0 <= n <= len(p)) and any error encountered.
|
|
|
|
//
|
2011-09-16 17:47:21 +02:00
|
|
|
// When ReadAt returns n < len(p), it returns a non-nil error
|
|
|
|
// explaining why more bytes were not returned. In this respect,
|
|
|
|
// ReadAt is stricter than Read.
|
|
|
|
//
|
|
|
|
// Even if ReadAt returns n < len(p), it may use all of p as scratch
|
|
|
|
// space during the call. If some data is available but not len(p) bytes,
|
|
|
|
// ReadAt blocks until either all the data is available or an error occurs.
|
|
|
|
// In this respect ReadAt is different from Read.
|
2010-12-03 05:34:57 +01:00
|
|
|
//
|
2011-09-16 17:47:21 +02:00
|
|
|
// If the n = len(p) bytes returned by ReadAt are at the end of the
|
2011-12-03 03:17:34 +01:00
|
|
|
// input source, ReadAt may return either err == EOF or err == nil.
|
2011-05-20 02:18:15 +02:00
|
|
|
//
|
2011-09-16 17:47:21 +02:00
|
|
|
// If ReadAt is reading from an input source with a seek offset,
|
2011-05-20 02:18:15 +02:00
|
|
|
// ReadAt should not affect nor be affected by the underlying
|
|
|
|
// seek offset.
|
2012-03-06 18:57:23 +01:00
|
|
|
//
|
|
|
|
// Clients of ReadAt can execute parallel ReadAt calls on the
|
|
|
|
// same input source.
|
2010-12-03 05:34:57 +01:00
|
|
|
type ReaderAt interface {
|
2011-12-03 03:17:34 +01:00
|
|
|
ReadAt(p []byte, off int64) (n int, err error)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriterAt is the interface that wraps the basic WriteAt method.
|
|
|
|
//
|
|
|
|
// WriteAt writes len(p) bytes from p to the underlying data stream
|
|
|
|
// at offset off. It returns the number of bytes written from p (0 <= n <= len(p))
|
|
|
|
// and any error encountered that caused the write to stop early.
|
|
|
|
// WriteAt must return a non-nil error if it returns n < len(p).
|
2012-03-30 23:27:11 +02:00
|
|
|
//
|
|
|
|
// If WriteAt is writing to a destination with a seek offset,
|
|
|
|
// WriteAt should not affect nor be affected by the underlying
|
|
|
|
// seek offset.
|
|
|
|
//
|
|
|
|
// Clients of WriteAt can execute parallel WriteAt calls on the same
|
|
|
|
// destination if the ranges do not overlap.
|
2010-12-03 05:34:57 +01:00
|
|
|
type WriterAt interface {
|
2011-12-03 03:17:34 +01:00
|
|
|
WriteAt(p []byte, off int64) (n int, err error)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-03-17 00:05:44 +01:00
|
|
|
// ByteReader is the interface that wraps the ReadByte method.
|
2010-12-03 05:34:57 +01:00
|
|
|
//
|
|
|
|
// ReadByte reads and returns the next byte from the input.
|
|
|
|
// If no byte is available, err will be set.
|
2011-03-17 00:05:44 +01:00
|
|
|
type ByteReader interface {
|
2011-12-03 03:17:34 +01:00
|
|
|
ReadByte() (c byte, err error)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// ByteScanner is the interface that adds the UnreadByte method to the
|
|
|
|
// basic ReadByte method.
|
|
|
|
//
|
|
|
|
// UnreadByte causes the next call to ReadByte to return the same byte
|
|
|
|
// as the previous call to ReadByte.
|
|
|
|
// It may be an error to call UnreadByte twice without an intervening
|
|
|
|
// call to ReadByte.
|
|
|
|
type ByteScanner interface {
|
|
|
|
ByteReader
|
2011-12-03 03:17:34 +01:00
|
|
|
UnreadByte() error
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2012-11-21 08:03:38 +01:00
|
|
|
// ByteWriter is the interface that wraps the WriteByte method.
|
|
|
|
type ByteWriter interface {
|
|
|
|
WriteByte(c byte) error
|
|
|
|
}
|
|
|
|
|
2011-03-17 00:05:44 +01:00
|
|
|
// RuneReader is the interface that wraps the ReadRune method.
|
|
|
|
//
|
|
|
|
// ReadRune reads a single UTF-8 encoded Unicode character
|
|
|
|
// and returns the rune and its size in bytes. If no character is
|
|
|
|
// available, err will be set.
|
|
|
|
type RuneReader interface {
|
2011-12-03 03:17:34 +01:00
|
|
|
ReadRune() (r rune, size int, err error)
|
2011-03-17 00:05:44 +01:00
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// RuneScanner is the interface that adds the UnreadRune method to the
|
|
|
|
// basic ReadRune method.
|
|
|
|
//
|
|
|
|
// UnreadRune causes the next call to ReadRune to return the same rune
|
|
|
|
// as the previous call to ReadRune.
|
|
|
|
// It may be an error to call UnreadRune twice without an intervening
|
|
|
|
// call to ReadRune.
|
|
|
|
type RuneScanner interface {
|
|
|
|
RuneReader
|
2011-12-03 03:17:34 +01:00
|
|
|
UnreadRune() error
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// stringWriter is the interface that wraps the WriteString method.
|
|
|
|
type stringWriter interface {
|
2011-12-03 03:17:34 +01:00
|
|
|
WriteString(s string) (n int, err error)
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// WriteString writes the contents of the string s to w, which accepts an array of bytes.
|
2012-02-09 09:19:58 +01:00
|
|
|
// If w already implements a WriteString method, it is invoked directly.
|
2011-12-03 03:17:34 +01:00
|
|
|
func WriteString(w Writer, s string) (n int, err error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
if sw, ok := w.(stringWriter); ok {
|
|
|
|
return sw.WriteString(s)
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
return w.Write([]byte(s))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadAtLeast reads from r into buf until it has read at least min bytes.
|
|
|
|
// It returns the number of bytes copied and an error if fewer bytes were read.
|
2011-12-03 03:17:34 +01:00
|
|
|
// The error is EOF only if no bytes were read.
|
2010-12-03 05:34:57 +01:00
|
|
|
// If an EOF happens after reading fewer than min bytes,
|
|
|
|
// ReadAtLeast returns ErrUnexpectedEOF.
|
|
|
|
// If min is greater than the length of buf, ReadAtLeast returns ErrShortBuffer.
|
2013-07-16 08:54:42 +02:00
|
|
|
// On return, n >= min if and only if err == nil.
|
2011-12-03 03:17:34 +01:00
|
|
|
func ReadAtLeast(r Reader, buf []byte, min int) (n int, err error) {
|
2010-12-03 05:34:57 +01:00
|
|
|
if len(buf) < min {
|
|
|
|
return 0, ErrShortBuffer
|
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
for n < min && err == nil {
|
|
|
|
var nn int
|
|
|
|
nn, err = r.Read(buf[n:])
|
|
|
|
n += nn
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
if n >= min {
|
|
|
|
err = nil
|
|
|
|
} else if n > 0 && err == EOF {
|
|
|
|
err = ErrUnexpectedEOF
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadFull reads exactly len(buf) bytes from r into buf.
|
|
|
|
// It returns the number of bytes copied and an error if fewer bytes were read.
|
2011-12-03 03:17:34 +01:00
|
|
|
// The error is EOF only if no bytes were read.
|
2010-12-03 05:34:57 +01:00
|
|
|
// If an EOF happens after reading some but not all the bytes,
|
|
|
|
// ReadFull returns ErrUnexpectedEOF.
|
2013-07-16 08:54:42 +02:00
|
|
|
// On return, n == len(buf) if and only if err == nil.
|
2011-12-03 03:17:34 +01:00
|
|
|
func ReadFull(r Reader, buf []byte) (n int, err error) {
|
2010-12-03 05:34:57 +01:00
|
|
|
return ReadAtLeast(r, buf, len(buf))
|
|
|
|
}
|
|
|
|
|
2011-10-27 01:57:58 +02:00
|
|
|
// CopyN copies n bytes (or until an error) from src to dst.
|
2011-09-16 17:47:21 +02:00
|
|
|
// It returns the number of bytes copied and the earliest
|
2013-07-16 08:54:42 +02:00
|
|
|
// error encountered while copying.
|
|
|
|
// On return, written == n if and only if err == nil.
|
2010-12-03 05:34:57 +01:00
|
|
|
//
|
|
|
|
// If dst implements the ReaderFrom interface,
|
2012-02-09 09:19:58 +01:00
|
|
|
// the copy is implemented using it.
|
2011-12-03 03:17:34 +01:00
|
|
|
func CopyN(dst Writer, src Reader, n int64) (written int64, err error) {
|
2013-07-16 08:54:42 +02:00
|
|
|
written, err = Copy(dst, LimitReader(src, n))
|
|
|
|
if written == n {
|
|
|
|
return n, nil
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
if written < n && err == nil {
|
|
|
|
// src stopped early; must have been EOF.
|
|
|
|
err = EOF
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
return
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy copies from src to dst until either EOF is reached
|
|
|
|
// on src or an error occurs. It returns the number of bytes
|
2011-09-16 17:47:21 +02:00
|
|
|
// copied and the first error encountered while copying, if any.
|
|
|
|
//
|
2011-12-03 03:17:34 +01:00
|
|
|
// A successful Copy returns err == nil, not err == EOF.
|
2011-09-16 17:47:21 +02:00
|
|
|
// Because Copy is defined to read from src until EOF, it does
|
|
|
|
// not treat an EOF from Read as an error to be reported.
|
2010-12-03 05:34:57 +01:00
|
|
|
//
|
2013-11-06 20:49:01 +01:00
|
|
|
// If src implements the WriterTo interface,
|
2010-12-03 05:34:57 +01:00
|
|
|
// the copy is implemented by calling src.WriteTo(dst).
|
2013-11-06 20:49:01 +01:00
|
|
|
// Otherwise, if dst implements the ReaderFrom interface,
|
|
|
|
// the copy is implemented by calling dst.ReadFrom(src).
|
2011-12-03 03:17:34 +01:00
|
|
|
func Copy(dst Writer, src Reader) (written int64, err error) {
|
2013-11-06 20:49:01 +01:00
|
|
|
// If the reader has a WriteTo method, use it to do the copy.
|
2010-12-03 05:34:57 +01:00
|
|
|
// Avoids an allocation and a copy.
|
|
|
|
if wt, ok := src.(WriterTo); ok {
|
|
|
|
return wt.WriteTo(dst)
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
// Similarly, if the writer has a ReadFrom method, use it to do the copy.
|
|
|
|
if rt, ok := dst.(ReaderFrom); ok {
|
|
|
|
return rt.ReadFrom(src)
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
buf := make([]byte, 32*1024)
|
|
|
|
for {
|
|
|
|
nr, er := src.Read(buf)
|
|
|
|
if nr > 0 {
|
|
|
|
nw, ew := dst.Write(buf[0:nr])
|
|
|
|
if nw > 0 {
|
|
|
|
written += int64(nw)
|
|
|
|
}
|
|
|
|
if ew != nil {
|
|
|
|
err = ew
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if nr != nw {
|
|
|
|
err = ErrShortWrite
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2011-12-03 03:17:34 +01:00
|
|
|
if er == EOF {
|
2010-12-03 05:34:57 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if er != nil {
|
|
|
|
err = er
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return written, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// LimitReader returns a Reader that reads from r
|
2011-12-03 03:17:34 +01:00
|
|
|
// but stops with EOF after n bytes.
|
2011-09-16 17:47:21 +02:00
|
|
|
// The underlying implementation is a *LimitedReader.
|
|
|
|
func LimitReader(r Reader, n int64) Reader { return &LimitedReader{r, n} }
|
|
|
|
|
|
|
|
// A LimitedReader reads from R but limits the amount of
|
|
|
|
// data returned to just N bytes. Each call to Read
|
|
|
|
// updates N to reflect the new amount remaining.
|
|
|
|
type LimitedReader struct {
|
|
|
|
R Reader // underlying reader
|
|
|
|
N int64 // max bytes remaining
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func (l *LimitedReader) Read(p []byte) (n int, err error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
if l.N <= 0 {
|
2011-12-03 03:17:34 +01:00
|
|
|
return 0, EOF
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
if int64(len(p)) > l.N {
|
|
|
|
p = p[0:l.N]
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
n, err = l.R.Read(p)
|
|
|
|
l.N -= int64(n)
|
2010-12-03 05:34:57 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSectionReader returns a SectionReader that reads from r
|
2011-12-03 03:17:34 +01:00
|
|
|
// starting at offset off and stops with EOF after n bytes.
|
2010-12-03 05:34:57 +01:00
|
|
|
func NewSectionReader(r ReaderAt, off int64, n int64) *SectionReader {
|
|
|
|
return &SectionReader{r, off, off, off + n}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SectionReader implements Read, Seek, and ReadAt on a section
|
|
|
|
// of an underlying ReaderAt.
|
|
|
|
type SectionReader struct {
|
|
|
|
r ReaderAt
|
|
|
|
base int64
|
|
|
|
off int64
|
|
|
|
limit int64
|
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func (s *SectionReader) Read(p []byte) (n int, err error) {
|
2010-12-03 05:34:57 +01:00
|
|
|
if s.off >= s.limit {
|
2011-12-03 03:17:34 +01:00
|
|
|
return 0, EOF
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
if max := s.limit - s.off; int64(len(p)) > max {
|
|
|
|
p = p[0:max]
|
|
|
|
}
|
|
|
|
n, err = s.r.ReadAt(p, s.off)
|
|
|
|
s.off += int64(n)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-02-09 09:19:58 +01:00
|
|
|
var errWhence = errors.New("Seek: invalid whence")
|
|
|
|
var errOffset = errors.New("Seek: invalid offset")
|
2011-12-03 03:17:34 +01:00
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
func (s *SectionReader) Seek(offset int64, whence int) (int64, error) {
|
2010-12-03 05:34:57 +01:00
|
|
|
switch whence {
|
|
|
|
default:
|
2011-12-03 03:17:34 +01:00
|
|
|
return 0, errWhence
|
2010-12-03 05:34:57 +01:00
|
|
|
case 0:
|
|
|
|
offset += s.base
|
|
|
|
case 1:
|
|
|
|
offset += s.off
|
|
|
|
case 2:
|
|
|
|
offset += s.limit
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
if offset < s.base {
|
2011-12-03 03:17:34 +01:00
|
|
|
return 0, errOffset
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
s.off = offset
|
|
|
|
return offset - s.base, nil
|
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func (s *SectionReader) ReadAt(p []byte, off int64) (n int, err error) {
|
2010-12-03 05:34:57 +01:00
|
|
|
if off < 0 || off >= s.limit-s.base {
|
2011-12-03 03:17:34 +01:00
|
|
|
return 0, EOF
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
off += s.base
|
|
|
|
if max := s.limit - off; int64(len(p)) > max {
|
|
|
|
p = p[0:max]
|
2012-12-22 02:15:33 +01:00
|
|
|
n, err = s.r.ReadAt(p, off)
|
|
|
|
if err == nil {
|
|
|
|
err = EOF
|
|
|
|
}
|
|
|
|
return n, err
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
return s.r.ReadAt(p, off)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns the size of the section in bytes.
|
|
|
|
func (s *SectionReader) Size() int64 { return s.limit - s.base }
|
2011-10-27 01:57:58 +02:00
|
|
|
|
|
|
|
// TeeReader returns a Reader that writes to w what it reads from r.
|
|
|
|
// All reads from r performed through it are matched with
|
|
|
|
// corresponding writes to w. There is no internal buffering -
|
|
|
|
// the write must complete before the read completes.
|
|
|
|
// Any error encountered while writing is reported as a read error.
|
|
|
|
func TeeReader(r Reader, w Writer) Reader {
|
|
|
|
return &teeReader{r, w}
|
|
|
|
}
|
|
|
|
|
|
|
|
type teeReader struct {
|
|
|
|
r Reader
|
|
|
|
w Writer
|
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func (t *teeReader) Read(p []byte) (n int, err error) {
|
2011-10-27 01:57:58 +02:00
|
|
|
n, err = t.r.Read(p)
|
|
|
|
if n > 0 {
|
|
|
|
if n, err := t.w.Write(p[:n]); err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|