1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
// Copyright 2015 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.
// Package bio implements common I/O abstractions used within the Go toolchain.
package bio
import (
"bufio"
"io"
"log"
"os"
)
// Reader implements a seekable buffered io.Reader.
type Reader struct {
f *os.File
*bufio.Reader
}
// Writer implements a seekable buffered io.Writer.
type Writer struct {
f *os.File
*bufio.Writer
}
// Create creates the file named name and returns a Writer
// for that file.
func Create(name string) (*Writer, error) {
f, err := os.Create(name)
if err != nil {
return nil, err
}
return &Writer{f: f, Writer: bufio.NewWriter(f)}, nil
}
// Open returns a Reader for the file named name.
func Open(name string) (*Reader, error) {
f, err := os.Open(name)
if err != nil {
return nil, err
}
return NewReader(f), nil
}
// NewReader returns a Reader from an open file.
func NewReader(f *os.File) *Reader {
return &Reader{f: f, Reader: bufio.NewReader(f)}
}
func (r *Reader) MustSeek(offset int64, whence int) int64 {
if whence == 1 {
offset -= int64(r.Buffered())
}
off, err := r.f.Seek(offset, whence)
if err != nil {
log.Fatalf("seeking in output: %v", err)
}
r.Reset(r.f)
return off
}
func (w *Writer) MustSeek(offset int64, whence int) int64 {
if err := w.Flush(); err != nil {
log.Fatalf("writing output: %v", err)
}
off, err := w.f.Seek(offset, whence)
if err != nil {
log.Fatalf("seeking in output: %v", err)
}
return off
}
func (r *Reader) Offset() int64 {
off, err := r.f.Seek(0, 1)
if err != nil {
log.Fatalf("seeking in output [0, 1]: %v", err)
}
off -= int64(r.Buffered())
return off
}
func (w *Writer) Offset() int64 {
if err := w.Flush(); err != nil {
log.Fatalf("writing output: %v", err)
}
off, err := w.f.Seek(0, 1)
if err != nil {
log.Fatalf("seeking in output [0, 1]: %v", err)
}
return off
}
func (r *Reader) Close() error {
return r.f.Close()
}
func (w *Writer) Close() error {
err := w.Flush()
err1 := w.f.Close()
if err == nil {
err = err1
}
return err
}
func (r *Reader) File() *os.File {
return r.f
}
func (w *Writer) File() *os.File {
return w.f
}
// Slice reads the next length bytes of r into a slice.
//
// This slice may be backed by mmap'ed memory. Currently, this memory
// will never be unmapped. The second result reports whether the
// backing memory is read-only.
func (r *Reader) Slice(length uint64) ([]byte, bool, error) {
if length == 0 {
return []byte{}, false, nil
}
data, ok := r.sliceOS(length)
if ok {
return data, true, nil
}
data = make([]byte, length)
_, err := io.ReadFull(r, data)
if err != nil {
return nil, false, err
}
return data, false, nil
}
// SliceRO returns a slice containing the next length bytes of r
// backed by a read-only mmap'd data. If the mmap cannot be
// established (limit exceeded, region too small, etc) a nil slice
// will be returned. If mmap succeeds, it will never be unmapped.
func (r *Reader) SliceRO(length uint64) []byte {
data, ok := r.sliceOS(length)
if ok {
return data
}
return nil
}
|