2008-09-12 17:42:53 -06: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.
|
|
|
|
|
2009-03-06 04:43:44 -07:00
|
|
|
// This package provides basic interfaces to I/O primitives.
|
|
|
|
// Its primary job is to wrap existing implementations of such primitives,
|
|
|
|
// such as those in package os, into shared public interfaces that
|
2009-12-02 23:02:14 -07:00
|
|
|
// abstract the functionality, plus some other related primitives.
|
2008-09-12 17:42:53 -06:00
|
|
|
package io
|
2008-11-11 18:28:36 -07:00
|
|
|
|
2010-02-25 17:01:29 -07:00
|
|
|
import "os"
|
2008-11-11 18:28:36 -07:00
|
|
|
|
2009-05-18 12:47:35 -06:00
|
|
|
// Error represents an unexpected I/O behavior.
|
2009-05-08 15:40:20 -06:00
|
|
|
type Error struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
os.ErrorString
|
2009-05-08 15:40:20 -06:00
|
|
|
}
|
2009-05-18 12:47:35 -06:00
|
|
|
|
|
|
|
// ErrShortWrite means that a write accepted fewer bytes than requested
|
|
|
|
// but failed to return an explicit error.
|
|
|
|
var ErrShortWrite os.Error = &Error{"short write"}
|
|
|
|
|
2010-08-22 20:04:15 -06:00
|
|
|
// ErrShortBuffer means that a read required a longer buffer than was provided.
|
|
|
|
var ErrShortBuffer os.Error = &Error{"short buffer"}
|
|
|
|
|
2009-06-22 14:26:13 -06:00
|
|
|
// ErrUnexpectedEOF means that os.EOF was encountered in the
|
|
|
|
// middle of reading a fixed-size block or data structure.
|
|
|
|
var ErrUnexpectedEOF os.Error = &Error{"unexpected EOF"}
|
2009-05-18 12:47:35 -06:00
|
|
|
|
2009-05-08 12:22:57 -06:00
|
|
|
// Reader is the interface that wraps the basic Read method.
|
2009-06-22 14:26:13 -06:00
|
|
|
//
|
|
|
|
// Read reads up to len(p) bytes into p. It returns the number of bytes
|
2009-06-22 15:44:07 -06:00
|
|
|
// read (0 <= n <= len(p)) and any error encountered.
|
|
|
|
// Even if Read returns n < len(p),
|
2009-06-22 14:26:13 -06:00
|
|
|
// it may use all of p as scratch space during the call.
|
|
|
|
// If some data is available but not len(p) bytes, Read conventionally
|
|
|
|
// returns what is available rather than block waiting for more.
|
|
|
|
//
|
|
|
|
// At the end of the input stream, Read returns 0, os.EOF.
|
|
|
|
// Read may return a non-zero number of bytes with a non-nil err.
|
|
|
|
// In particular, a Read that exhausts the input may return n > 0, os.EOF.
|
2009-05-08 12:22:57 -06:00
|
|
|
type Reader interface {
|
2009-12-15 16:35:38 -07:00
|
|
|
Read(p []byte) (n int, err os.Error)
|
2008-09-12 17:42:53 -06:00
|
|
|
}
|
|
|
|
|
2009-05-08 12:22:57 -06:00
|
|
|
// Writer is the interface that wraps the basic Write method.
|
2009-06-22 15:44:07 -06:00
|
|
|
//
|
|
|
|
// 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).
|
2009-05-08 12:22:57 -06:00
|
|
|
type Writer interface {
|
2009-12-15 16:35:38 -07:00
|
|
|
Write(p []byte) (n int, err os.Error)
|
2008-09-12 17:42:53 -06:00
|
|
|
}
|
|
|
|
|
2009-05-08 12:22:57 -06:00
|
|
|
// Closer is the interface that wraps the basic Close method.
|
|
|
|
type Closer interface {
|
2009-12-15 16:35:38 -07:00
|
|
|
Close() os.Error
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
|
|
|
|
2009-08-18 18:47:03 -06: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
|
|
|
|
// relative to the end. Seek returns the new offset and an Error, if
|
|
|
|
// any.
|
|
|
|
type Seeker interface {
|
2009-12-15 16:35:38 -07:00
|
|
|
Seek(offset int64, whence int) (ret int64, err os.Error)
|
2009-08-18 18:47:03 -06:00
|
|
|
}
|
|
|
|
|
2009-11-23 15:22:56 -07:00
|
|
|
// ReadWriter is the interface that groups the basic Read and Write methods.
|
2009-05-08 12:22:57 -06:00
|
|
|
type ReadWriter interface {
|
2009-12-15 16:35:38 -07:00
|
|
|
Reader
|
|
|
|
Writer
|
2008-09-17 14:49:23 -06:00
|
|
|
}
|
|
|
|
|
2009-05-08 12:22:57 -06:00
|
|
|
// ReadCloser is the interface that groups the basic Read and Close methods.
|
|
|
|
type ReadCloser interface {
|
2009-12-15 16:35:38 -07:00
|
|
|
Reader
|
|
|
|
Closer
|
2009-02-16 17:32:30 -07:00
|
|
|
}
|
|
|
|
|
2009-05-08 12:22:57 -06:00
|
|
|
// WriteCloser is the interface that groups the basic Write and Close methods.
|
|
|
|
type WriteCloser interface {
|
2009-12-15 16:35:38 -07:00
|
|
|
Writer
|
|
|
|
Closer
|
2008-10-20 13:37:07 -06:00
|
|
|
}
|
|
|
|
|
2009-05-08 12:22:57 -06:00
|
|
|
// ReadWriteCloser is the interface that groups the basic Read, Write and Close methods.
|
|
|
|
type ReadWriteCloser interface {
|
2009-12-15 16:35:38 -07:00
|
|
|
Reader
|
|
|
|
Writer
|
|
|
|
Closer
|
2009-02-03 15:16:22 -07:00
|
|
|
}
|
|
|
|
|
2009-08-18 18:47:03 -06:00
|
|
|
// ReadSeeker is the interface that groups the basic Read and Seek methods.
|
|
|
|
type ReadSeeker interface {
|
2009-12-15 16:35:38 -07:00
|
|
|
Reader
|
|
|
|
Seeker
|
2009-08-18 18:47:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteSeeker is the interface that groups the basic Write and Seek methods.
|
|
|
|
type WriteSeeker interface {
|
2009-12-15 16:35:38 -07:00
|
|
|
Writer
|
|
|
|
Seeker
|
2009-08-18 18:47:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadWriteSeeker is the interface that groups the basic Read, Write and Seek methods.
|
|
|
|
type ReadWriteSeeker interface {
|
2009-12-15 16:35:38 -07:00
|
|
|
Reader
|
|
|
|
Writer
|
|
|
|
Seeker
|
2009-08-18 18:47:03 -06:00
|
|
|
}
|
|
|
|
|
2009-12-03 13:56:16 -07:00
|
|
|
// ReaderFrom is the interface that wraps the ReadFrom method.
|
|
|
|
type ReaderFrom interface {
|
2009-12-15 16:35:38 -07:00
|
|
|
ReadFrom(r Reader) (n int64, err os.Error)
|
2009-12-03 13:56:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriterTo is the interface that wraps the WriteTo method.
|
|
|
|
type WriterTo interface {
|
2009-12-15 16:35:38 -07:00
|
|
|
WriteTo(w Writer) (n int64, err os.Error)
|
2009-12-03 13:56:16 -07:00
|
|
|
}
|
|
|
|
|
2009-08-31 17:34:43 -06:00
|
|
|
// ReaderAt is the interface that wraps the basic ReadAt method.
|
|
|
|
//
|
|
|
|
// ReadAt reads len(p) bytes into p starting at offset off in the
|
|
|
|
// underlying data stream. It returns the number of bytes
|
|
|
|
// read (0 <= n <= len(p)) and any error encountered.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// At the end of the input stream, ReadAt returns 0, os.EOF.
|
|
|
|
// ReadAt may return a non-zero number of bytes with a non-nil err.
|
|
|
|
// In particular, a ReadAt that exhausts the input may return n > 0, os.EOF.
|
|
|
|
type ReaderAt interface {
|
2009-12-15 16:35:38 -07:00
|
|
|
ReadAt(p []byte, off int64) (n int, err os.Error)
|
2009-08-31 17:34:43 -06: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).
|
|
|
|
type WriterAt interface {
|
2009-12-15 16:35:38 -07:00
|
|
|
WriteAt(p []byte, off int64) (n int, err os.Error)
|
2009-08-31 17:34:43 -06:00
|
|
|
}
|
|
|
|
|
2010-01-06 19:29:43 -07:00
|
|
|
// ReadByter is the interface that wraps the ReadByte method.
|
|
|
|
//
|
|
|
|
// ReadByte reads and returns the next byte from the input.
|
|
|
|
// If no byte is available, err will be set.
|
|
|
|
type ReadByter interface {
|
|
|
|
ReadByte() (c byte, err os.Error)
|
|
|
|
}
|
|
|
|
|
2011-02-01 22:09:33 -07:00
|
|
|
// ReadRuner 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 ReadRuner interface {
|
|
|
|
ReadRune() (rune int, size int, err os.Error)
|
|
|
|
}
|
|
|
|
|
2009-03-06 04:43:44 -07:00
|
|
|
// WriteString writes the contents of the string s to w, which accepts an array of bytes.
|
2009-05-08 12:22:57 -06:00
|
|
|
func WriteString(w Writer, s string) (n int, err os.Error) {
|
2010-02-25 17:01:29 -07:00
|
|
|
return w.Write([]byte(s))
|
2008-09-12 17:42:53 -06:00
|
|
|
}
|
2008-11-11 18:28:36 -07:00
|
|
|
|
2009-06-22 14:26:13 -06:00
|
|
|
// 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.
|
|
|
|
// The error is os.EOF only if no bytes were read.
|
|
|
|
// If an EOF happens after reading fewer than min bytes,
|
|
|
|
// ReadAtLeast returns ErrUnexpectedEOF.
|
2010-08-22 20:04:15 -06:00
|
|
|
// If min is greater than the length of buf, ReadAtLeast returns ErrShortBuffer.
|
2009-05-19 15:01:03 -06:00
|
|
|
func ReadAtLeast(r Reader, buf []byte, min int) (n int, err os.Error) {
|
2010-08-22 20:04:15 -06:00
|
|
|
if len(buf) < min {
|
|
|
|
return 0, ErrShortBuffer
|
|
|
|
}
|
2009-05-19 15:01:03 -06:00
|
|
|
for n < min {
|
2009-12-15 16:35:38 -07:00
|
|
|
nn, e := r.Read(buf[n:])
|
2008-11-11 18:28:36 -07:00
|
|
|
if nn > 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
n += nn
|
2008-11-11 18:28:36 -07:00
|
|
|
}
|
|
|
|
if e != nil {
|
2009-06-22 14:26:13 -06:00
|
|
|
if e == os.EOF && n > 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
e = ErrUnexpectedEOF
|
2009-06-22 14:26:13 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return n, e
|
2008-11-11 18:28:36 -07:00
|
|
|
}
|
|
|
|
}
|
2010-08-22 20:04:15 -06:00
|
|
|
return
|
2008-11-11 18:28:36 -07:00
|
|
|
}
|
|
|
|
|
2009-06-22 14:26:13 -06:00
|
|
|
// 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.
|
|
|
|
// The error is os.EOF only if no bytes were read.
|
|
|
|
// If an EOF happens after reading some but not all the bytes,
|
|
|
|
// ReadFull returns ErrUnexpectedEOF.
|
|
|
|
func ReadFull(r Reader, buf []byte) (n int, err os.Error) {
|
2009-11-09 13:07:39 -07:00
|
|
|
return ReadAtLeast(r, buf, len(buf))
|
2009-05-19 15:01:03 -06:00
|
|
|
}
|
|
|
|
|
2009-06-22 14:26:13 -06:00
|
|
|
// Copyn copies n bytes (or until an error) from src to dst.
|
2009-03-06 04:43:44 -07:00
|
|
|
// It returns the number of bytes copied and the error, if any.
|
2009-12-03 13:56:16 -07:00
|
|
|
//
|
|
|
|
// If dst implements the ReaderFrom interface,
|
|
|
|
// the copy is implemented by calling dst.ReadFrom(src).
|
2009-11-01 21:59:49 -07:00
|
|
|
func Copyn(dst Writer, src Reader, n int64) (written int64, err os.Error) {
|
2010-12-30 16:37:55 -07:00
|
|
|
// If the writer has a ReadFrom method, use it to do the copy.
|
2009-12-03 13:56:16 -07:00
|
|
|
// Avoids a buffer allocation and a copy.
|
|
|
|
if rt, ok := dst.(ReaderFrom); ok {
|
2011-01-05 12:35:13 -07:00
|
|
|
written, err = rt.ReadFrom(LimitReader(src, n))
|
|
|
|
if written < n && err == nil {
|
|
|
|
// rt stopped early; must have been EOF.
|
|
|
|
err = os.EOF
|
|
|
|
}
|
|
|
|
return
|
2009-12-03 13:56:16 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
buf := make([]byte, 32*1024)
|
2008-11-18 19:45:51 -07:00
|
|
|
for written < n {
|
2009-12-15 16:35:38 -07:00
|
|
|
l := len(buf)
|
2009-11-09 22:23:52 -07:00
|
|
|
if d := n - written; d < int64(l) {
|
2009-11-09 13:07:39 -07:00
|
|
|
l = int(d)
|
2008-11-18 19:08:05 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
nr, er := src.Read(buf[0:l])
|
2008-11-18 19:08:05 -07:00
|
|
|
if nr > 0 {
|
2009-12-15 16:35:38 -07:00
|
|
|
nw, ew := dst.Write(buf[0:nr])
|
2008-11-18 19:45:51 -07:00
|
|
|
if nw > 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
written += int64(nw)
|
2008-11-18 19:45:51 -07:00
|
|
|
}
|
|
|
|
if ew != nil {
|
2009-12-15 16:35:38 -07:00
|
|
|
err = ew
|
|
|
|
break
|
2008-11-18 19:08:05 -07:00
|
|
|
}
|
2008-11-18 19:45:51 -07:00
|
|
|
if nr != nw {
|
2009-12-15 16:35:38 -07:00
|
|
|
err = ErrShortWrite
|
|
|
|
break
|
2008-11-18 19:45:51 -07:00
|
|
|
}
|
2008-11-18 19:08:05 -07:00
|
|
|
}
|
|
|
|
if er != nil {
|
2009-12-15 16:35:38 -07:00
|
|
|
err = er
|
|
|
|
break
|
2008-11-18 19:08:05 -07:00
|
|
|
}
|
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return written, err
|
2008-11-18 19:08:05 -07:00
|
|
|
}
|
2008-11-18 19:45:51 -07:00
|
|
|
|
2009-06-22 14:26:13 -06:00
|
|
|
// Copy copies from src to dst until either EOF is reached
|
|
|
|
// on src or an error occurs. It returns the number of bytes
|
|
|
|
// copied and the error, if any.
|
2009-12-03 13:56:16 -07:00
|
|
|
//
|
|
|
|
// If dst implements the ReaderFrom interface,
|
|
|
|
// the copy is implemented by calling dst.ReadFrom(src).
|
|
|
|
// Otherwise, if src implements the WriterTo interface,
|
|
|
|
// the copy is implemented by calling src.WriteTo(dst).
|
2009-11-01 21:59:49 -07:00
|
|
|
func Copy(dst Writer, src Reader) (written int64, err os.Error) {
|
2010-12-30 16:37:55 -07:00
|
|
|
// If the writer has a ReadFrom method, use it to do the copy.
|
2009-12-03 13:56:16 -07:00
|
|
|
// Avoids an allocation and a copy.
|
|
|
|
if rt, ok := dst.(ReaderFrom); ok {
|
|
|
|
return rt.ReadFrom(src)
|
|
|
|
}
|
2010-12-30 16:37:55 -07:00
|
|
|
// Similarly, if the reader has a WriteTo method, use it to do the copy.
|
2009-12-03 13:56:16 -07:00
|
|
|
if wt, ok := src.(WriterTo); ok {
|
|
|
|
return wt.WriteTo(dst)
|
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
buf := make([]byte, 32*1024)
|
2008-11-18 19:45:51 -07:00
|
|
|
for {
|
2009-12-15 16:35:38 -07:00
|
|
|
nr, er := src.Read(buf)
|
2008-11-18 19:45:51 -07:00
|
|
|
if nr > 0 {
|
2009-12-15 16:35:38 -07:00
|
|
|
nw, ew := dst.Write(buf[0:nr])
|
2008-11-18 19:45:51 -07:00
|
|
|
if nw > 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
written += int64(nw)
|
2008-11-18 19:45:51 -07:00
|
|
|
}
|
|
|
|
if ew != nil {
|
2009-12-15 16:35:38 -07:00
|
|
|
err = ew
|
|
|
|
break
|
2008-11-18 19:45:51 -07:00
|
|
|
}
|
|
|
|
if nr != nw {
|
2009-12-15 16:35:38 -07:00
|
|
|
err = ErrShortWrite
|
|
|
|
break
|
2008-11-18 19:45:51 -07:00
|
|
|
}
|
|
|
|
}
|
2009-06-22 14:26:13 -06:00
|
|
|
if er == os.EOF {
|
2009-11-09 13:07:39 -07:00
|
|
|
break
|
2008-11-18 19:45:51 -07:00
|
|
|
}
|
2009-06-22 14:26:13 -06:00
|
|
|
if er != nil {
|
2009-12-15 16:35:38 -07:00
|
|
|
err = er
|
|
|
|
break
|
2008-11-18 19:45:51 -07:00
|
|
|
}
|
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return written, err
|
2008-11-18 19:45:51 -07:00
|
|
|
}
|
2009-05-18 14:31:56 -06:00
|
|
|
|
2009-08-26 17:23:54 -06:00
|
|
|
// LimitReader returns a Reader that reads from r
|
|
|
|
// but stops with os.EOF after n bytes.
|
2009-12-15 16:35:38 -07:00
|
|
|
func LimitReader(r Reader, n int64) Reader { return &limitedReader{r, n} }
|
2009-05-18 14:31:56 -06:00
|
|
|
|
2009-08-26 17:23:54 -06:00
|
|
|
type limitedReader struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
r Reader
|
|
|
|
n int64
|
2009-08-26 17:23:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *limitedReader) Read(p []byte) (n int, err os.Error) {
|
|
|
|
if l.n <= 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, os.EOF
|
2009-06-22 14:26:13 -06:00
|
|
|
}
|
2009-08-26 17:23:54 -06:00
|
|
|
if int64(len(p)) > l.n {
|
2009-11-09 22:23:52 -07:00
|
|
|
p = p[0:l.n]
|
2009-05-18 14:31:56 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
n, err = l.r.Read(p)
|
|
|
|
l.n -= int64(n)
|
|
|
|
return
|
2009-05-18 14:31:56 -06:00
|
|
|
}
|
2009-08-31 17:34:43 -06:00
|
|
|
|
|
|
|
// NewSectionReader returns a SectionReader that reads from r
|
|
|
|
// starting at offset off and stops with os.EOF after n bytes.
|
|
|
|
func NewSectionReader(r ReaderAt, off int64, n int64) *SectionReader {
|
2009-11-09 22:23:52 -07:00
|
|
|
return &SectionReader{r, off, off, off + n}
|
2009-08-31 17:34:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// SectionReader implements Read, Seek, and ReadAt on a section
|
|
|
|
// of an underlying ReaderAt.
|
|
|
|
type SectionReader struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
r ReaderAt
|
|
|
|
base int64
|
|
|
|
off int64
|
|
|
|
limit int64
|
2009-08-31 17:34:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SectionReader) Read(p []byte) (n int, err os.Error) {
|
|
|
|
if s.off >= s.limit {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, os.EOF
|
2009-08-31 17:34:43 -06:00
|
|
|
}
|
|
|
|
if max := s.limit - s.off; int64(len(p)) > max {
|
2009-11-09 13:07:39 -07:00
|
|
|
p = p[0:max]
|
2009-08-31 17:34:43 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
n, err = s.r.ReadAt(p, s.off)
|
|
|
|
s.off += int64(n)
|
|
|
|
return
|
2009-08-31 17:34:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SectionReader) Seek(offset int64, whence int) (ret int64, err os.Error) {
|
|
|
|
switch whence {
|
|
|
|
default:
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, os.EINVAL
|
2009-08-31 17:34:43 -06:00
|
|
|
case 0:
|
2009-11-09 13:07:39 -07:00
|
|
|
offset += s.base
|
2009-08-31 17:34:43 -06:00
|
|
|
case 1:
|
2009-11-09 13:07:39 -07:00
|
|
|
offset += s.off
|
2009-08-31 17:34:43 -06:00
|
|
|
case 2:
|
2009-11-09 13:07:39 -07:00
|
|
|
offset += s.limit
|
2009-08-31 17:34:43 -06:00
|
|
|
}
|
2010-07-08 00:57:07 -06:00
|
|
|
if offset < s.base || offset > s.limit {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, os.EINVAL
|
2009-08-31 17:34:43 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
s.off = offset
|
|
|
|
return offset - s.base, nil
|
2009-08-31 17:34:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SectionReader) ReadAt(p []byte, off int64) (n int, err os.Error) {
|
2009-11-09 22:23:52 -07:00
|
|
|
if off < 0 || off >= s.limit-s.base {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, os.EOF
|
2009-08-31 17:34:43 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
off += s.base
|
2009-08-31 17:34:43 -06:00
|
|
|
if max := s.limit - off; int64(len(p)) > max {
|
2009-11-09 13:07:39 -07:00
|
|
|
p = p[0:max]
|
2009-08-31 17:34:43 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return s.r.ReadAt(p, off)
|
2009-08-31 17:34:43 -06:00
|
|
|
}
|
|
|
|
|
2009-09-01 17:11:17 -06:00
|
|
|
// Size returns the size of the section in bytes.
|
2009-12-15 16:35:38 -07:00
|
|
|
func (s *SectionReader) Size() int64 { return s.limit - s.base }
|