1 // Copyright 2012 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.
13 // A Reader implements the io.Reader, io.ReaderAt, io.WriterTo, io.Seeker,
14 // io.ByteScanner, and io.RuneScanner interfaces by reading from
16 // Unlike a Buffer, a Reader is read-only and supports seeking.
17 // The zero value for Reader operates like a Reader of an empty slice.
20 i
int64 // current reading index
21 prevRune
int // index of previous rune; or < 0
24 // Len returns the number of bytes of the unread portion of the
26 func (r
*Reader
) Len() int {
27 if r
.i
>= int64(len(r
.s
)) {
30 return int(int64(len(r
.s
)) - r
.i
)
33 // Size returns the original length of the underlying byte slice.
34 // Size is the number of bytes available for reading via ReadAt.
35 // The returned value is always the same and is not affected by calls
36 // to any other method.
37 func (r
*Reader
) Size() int64 { return int64(len(r
.s
)) }
39 // Read implements the io.Reader interface.
40 func (r
*Reader
) Read(b
[]byte) (n
int, err error
) {
41 if r
.i
>= int64(len(r
.s
)) {
45 n
= copy(b
, r
.s
[r
.i
:])
50 // ReadAt implements the io.ReaderAt interface.
51 func (r
*Reader
) ReadAt(b
[]byte, off
int64) (n
int, err error
) {
52 // cannot modify state - see io.ReaderAt
54 return 0, errors
.New("bytes.Reader.ReadAt: negative offset")
56 if off
>= int64(len(r
.s
)) {
59 n
= copy(b
, r
.s
[off
:])
66 // ReadByte implements the io.ByteReader interface.
67 func (r
*Reader
) ReadByte() (byte, error
) {
69 if r
.i
>= int64(len(r
.s
)) {
77 // UnreadByte complements ReadByte in implementing the io.ByteScanner interface.
78 func (r
*Reader
) UnreadByte() error
{
80 return errors
.New("bytes.Reader.UnreadByte: at beginning of slice")
87 // ReadRune implements the io.RuneReader interface.
88 func (r
*Reader
) ReadRune() (ch rune
, size
int, err error
) {
89 if r
.i
>= int64(len(r
.s
)) {
94 if c
:= r
.s
[r
.i
]; c
< utf8
.RuneSelf
{
96 return rune(c
), 1, nil
98 ch
, size
= utf8
.DecodeRune(r
.s
[r
.i
:])
103 // UnreadRune complements ReadRune in implementing the io.RuneScanner interface.
104 func (r
*Reader
) UnreadRune() error
{
106 return errors
.New("bytes.Reader.UnreadRune: at beginning of slice")
109 return errors
.New("bytes.Reader.UnreadRune: previous operation was not ReadRune")
111 r
.i
= int64(r
.prevRune
)
116 // Seek implements the io.Seeker interface.
117 func (r
*Reader
) Seek(offset
int64, whence
int) (int64, error
) {
126 abs
= int64(len(r
.s
)) + offset
128 return 0, errors
.New("bytes.Reader.Seek: invalid whence")
131 return 0, errors
.New("bytes.Reader.Seek: negative position")
137 // WriteTo implements the io.WriterTo interface.
138 func (r
*Reader
) WriteTo(w io
.Writer
) (n
int64, err error
) {
140 if r
.i
>= int64(len(r
.s
)) {
146 panic("bytes.Reader.WriteTo: invalid Write count")
150 if m
!= len(b
) && err
== nil {
151 err
= io
.ErrShortWrite
156 // Reset resets the Reader to be reading from b.
157 func (r
*Reader
) Reset(b
[]byte) { *r
= Reader
{b
, 0, -1} }
159 // NewReader returns a new Reader reading from b.
160 func NewReader(b
[]byte) *Reader
{ return &Reader
{b
, 0, -1} }