Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
e5962971
Commit
e5962971
authored
Aug 31, 2009
by
Russ Cox
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add SectionReader, ReaderAt.
R=r DELTA=85 (85 added, 0 deleted, 0 changed) OCL=34141 CL=34144
parent
60222bf5
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
85 additions
and
0 deletions
+85
-0
io.go
src/pkg/io/io.go
+85
-0
No files found.
src/pkg/io/io.go
View file @
e5962971
...
...
@@ -114,6 +114,34 @@ type ReadWriteSeeker interface {
Seeker
;
}
// 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
{
ReadAt
(
p
[]
byte
,
off
int64
)
(
n
int
,
err
os
.
Error
);
}
// 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
{
WriteAt
(
p
[]
byte
,
off
int64
)
(
n
int
,
err
os
.
Error
);
}
// WriteString writes the contents of the string s to w, which accepts an array of bytes.
func
WriteString
(
w
Writer
,
s
string
)
(
n
int
,
err
os
.
Error
)
{
return
w
.
Write
(
strings
.
Bytes
(
s
))
...
...
@@ -236,3 +264,60 @@ func (l *limitedReader) Read(p []byte) (n int, err os.Error) {
l
.
n
-=
int64
(
n
);
return
;
}
// 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
{
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
;
}
func
(
s
*
SectionReader
)
Read
(
p
[]
byte
)
(
n
int
,
err
os
.
Error
)
{
if
s
.
off
>=
s
.
limit
{
return
0
,
os
.
EOF
;
}
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
;
}
func
(
s
*
SectionReader
)
Seek
(
offset
int64
,
whence
int
)
(
ret
int64
,
err
os
.
Error
)
{
switch
whence
{
default
:
return
0
,
os
.
EINVAL
case
0
:
offset
+=
s
.
base
case
1
:
offset
+=
s
.
off
case
2
:
offset
+=
s
.
limit
}
if
offset
<
s
.
off
||
offset
>
s
.
limit
{
return
0
,
os
.
EINVAL
}
s
.
off
=
offset
;
return
offset
-
s
.
base
,
nil
}
func
(
s
*
SectionReader
)
ReadAt
(
p
[]
byte
,
off
int64
)
(
n
int
,
err
os
.
Error
)
{
if
off
<
0
||
off
>=
s
.
limit
-
s
.
base
{
return
0
,
os
.
EOF
;
}
off
+=
s
.
base
;
if
max
:=
s
.
limit
-
off
;
int64
(
len
(
p
))
>
max
{
p
=
p
[
0
:
max
];
}
return
s
.
r
.
ReadAt
(
p
,
off
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment