-
Notifications
You must be signed in to change notification settings - Fork 3
/
stringio.go
227 lines (200 loc) · 5.17 KB
/
stringio.go
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// 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.
package stringio
import (
"errors"
"fmt"
"syscall"
)
const buf_size = 4096
var OSError error = errors.New("I/O operation on closed file")
// A stringIO object is similar to a File object.
// It mimics all File I/O operations by implementing the
// common interfaces where File is implemented.
// The main difference is StringIO never read/write to filesystem.
// All operations are done in memory by accessing its underly buffer.
// The difference b/w bytes.Buffer is that StringIO supports
// Random access where Buffer does not.
// Most buffer operations are similar to bytes.Buffer.
// StringIO also does not support any non I/O operations such as
// Mkdir, Stats, Symlink, etc which does not have a real semantics for
// buffer manipulation.
// A StringIO object can not be reused once it is closed, just like
// the file object.
type stringIO struct {
buf []byte
isclosed bool
pos, last int
name string
}
// Factory method served as the constructor.
func New() *stringIO {
buf := make([]byte, buf_size)
sio := new(stringIO)
sio.buf = buf
sio.isclosed = false
sio.pos = 0
sio.last = 0
sio.name = fmt.Sprintf("StringIO <%p>", sio)
return sio
}
func (s *stringIO) Len() int { return len(s.buf[s.pos:s.last]) }
// Query for stringio object's fd is an error.
func (s *stringIO) Fd() (fd int, err error) {
return -1, errors.New("invalid")
}
func (s *stringIO) GoString() string { return s.name }
func (s *stringIO) Name() string { return s.name }
// Return the unread buffer.
func (s *stringIO) String() string {
if s.isClosed() {
return "<nil>"
}
return string(s.buf[s.pos:s.last])
}
// Return stored buffer until the last written position.
func (s *stringIO) GetValueString() string {
if s.isClosed() {
return "<nil>"
}
return string(s.buf[0:s.last])
}
// Return stored buffer as a byte array.
func (s *stringIO) GetValueBytes() []byte {
if s.isClosed() {
return s.buf[0:0]
}
return s.buf[0:s.last]
}
// Call Close will release the buffer/memory.
func (s *stringIO) Close() (err error) {
s.Truncate(0)
s.isclosed = true
s.name = "StringIO <closed>"
return
}
func (s *stringIO) Truncate(n int) {
if s.isClosed() != true {
if n == 0 {
s.pos = 0
s.last = 0
}
s.last = s.pos + n
s.buf = s.buf[0:s.last]
}
}
func (s *stringIO) Seek(offset int64, whence int) (ret int64, err error) {
if s.isClosed() {
return 0, OSError
}
pos, length := int64(s.pos), int64(len(s.buf))
int64_O := int64(0)
switch whence {
case 0:
ret = offset
case 1:
ret = offset + pos
case 2:
ret = offset + length
default:
return 0, errors.New("invalid")
}
if ret < int64_O {
ret = int64_O
}
// stringIO currently does not support Seek beyond the
// buf end, whereas posix does allow seek outside of
// the file size, which will end up with a file hole.
// However, StringIO does allow a byte hold within its
// buffer size.
if ret > length {
ret = length
}
// Unfortunately, this will have to be a downcast.
s.pos = int(ret)
return
}
func (s *stringIO) Read(b []byte) (n int, err error) {
if s.isClosed() {
return 0, OSError
}
if s.pos >= len(s.buf) {
return 0, errors.New("eof")
}
return s.readBytes(b)
}
func (s *stringIO) ReadAt(b []byte, offset int64) (n int, err error) {
if s.isClosed() {
return 0, OSError
}
s.setPos(offset)
return s.readBytes(b)
}
// stringIO Write will always be success until memory is used up
// or system limit is reached.
func (s *stringIO) Write(b []byte) (n int, err error) {
if s.isClosed() {
return 0, OSError
}
return s.writeBytes(b)
}
func (s *stringIO) WriteAt(b []byte, offset int64) (n int, err error) {
if s.isClosed() {
return 0, OSError
}
s.setPos(offset)
return s.writeBytes(b)
}
func (s *stringIO) WriteString(str string) (ret int, err error) {
b := syscall.StringByteSlice(str)
return s.Write(b[0 : len(b)-1])
}
// private methods
func (s *stringIO) readBytes(b []byte) (n int, err error) {
if s.pos > s.last {
return 0, nil
}
n = len(b)
// Require more than what we have only get what we have.
// In other words, empty bytes will not be sent out.
if s.pos+n > s.last {
n = s.last - s.pos
}
copy(b, s.buf[s.pos:s.pos+n])
s.pos += n
return
}
func (s *stringIO) writeBytes(b []byte) (n int, err error) {
n = len(b)
if n > s.length() {
s.resize(n)
}
copy(s.buf[s.pos:s.pos+n], b)
s.pos += n
if s.pos > s.last {
s.last = s.pos
}
return
}
func (s *stringIO) setPos(offset int64) {
pos, int64_O, length := int64(s.pos), int64(0), int64(len(s.buf))
pos = offset
if offset < int64_O {
pos = int64_O
}
if offset > length {
pos = length
}
s.pos = int(pos)
}
func (s *stringIO) length() int { return len(s.buf) - s.pos }
func (s *stringIO) isClosed() bool { return s.isclosed == true }
// Stolen from bytes.Buffer (Use the same algorithm)
func (s *stringIO) resize(n int) {
if len(s.buf)+n > cap(s.buf) {
buf := make([]byte, 2*cap(s.buf)+n)
copy(buf, s.buf[0:])
s.buf = buf
}
}