-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
397 lines (349 loc) · 9.49 KB
/
file.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package memio
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"io/fs"
"math"
"slices"
"time"
"unsafe"
)
// File implements file-like methods on an in-memory buffer
type File struct {
pos int
buf []byte
}
// Len returns the length of the internal buffer
func (f *File) Len() int {
return len(f.buf)
}
// Offset returns the current read/write position of the internal buffer
func (f *File) Offset() int64 {
return int64(f.pos)
}
// Bytes returns the internal buffer
func (f *File) Bytes() []byte {
return f.buf
}
// String returns a copy of the internal buffer as a string
func (f *File) String() string {
return string(f.buf)
}
// StringRef returns a reference to the internal buffer as a string
func (f *File) StringRef() string {
return unsafe.String(unsafe.SliceData(f.buf), len(f.buf))
}
// Reset sets the internal buffer to p and the internal offset to 0
func (f *File) Reset(p []byte) {
f.pos = 0
f.buf = p
}
// Read implements io.Reader
func (f *File) Read(p []byte) (int, error) {
if f.pos >= len(f.buf) {
return 0, io.EOF
}
n := copy(p, f.buf[f.pos:])
f.pos += n
return n, nil
}
// ReadByte implements io.ByteReader
func (f *File) ReadByte() (byte, error) {
if f.pos >= len(f.buf) {
return 0, io.EOF
}
c := f.buf[f.pos]
f.pos += 1
return c, nil
}
// readBytes implements ReadBytes and ReadString, returning a slice to the internal buffer
func (f *File) readBytes(delim byte) ([]byte, error) {
if f.pos >= len(f.buf) {
return nil, io.ErrUnexpectedEOF
}
if i := bytes.IndexByte(f.buf[f.pos:], delim); i >= 0 {
s := f.buf[f.pos : f.pos+i]
f.pos += i + 1 // skip over delim
return s, nil
}
s := f.buf[f.pos:]
f.pos = len(f.buf)
return s, io.ErrUnexpectedEOF
}
// ReadBytes reads bytes up to and excluding delim
// An error (wrapping io.ErrUnexpectedEOF) is returned iff delim is not found
func (f *File) ReadBytes(delim byte) ([]byte, error) {
p, err := f.readBytes(delim)
q := append([]byte(nil), p...)
if err != nil {
return q, fmt.Errorf("File.ReadBytes: %w", err)
}
return q, nil
}
// ReadString reads bytes up to and excluding delim
// An error (wrapping io.ErrUnexpectedEOF) is returned iff delim is not found
func (f *File) ReadString(delim byte) (string, error) {
p, err := f.readBytes(delim)
q := string(p)
if err != nil {
return q, fmt.Errorf("File.ReadString: %w", err)
}
return q, nil
}
// ReadFull fills buffer p, or returns the number of bytes read and error io.ErrUnexpectedEOF
func (f *File) ReadFull(p []byte) (int, error) {
if f.pos >= len(f.buf) {
return 0, io.ErrUnexpectedEOF
}
n := copy(p, f.buf[f.pos:])
f.pos += n
if n < len(p) {
return n, io.ErrUnexpectedEOF
}
return n, nil
}
// ReadUint16 reads a 16-bit number in the byte order specified by o
func (f *File) ReadUint16(o binary.ByteOrder) (uint16, error) {
p := [2]byte{}
if _, err := f.ReadFull(p[:]); err != nil {
return 0, fmt.Errorf("File.ReadUint16: %w", err)
}
return o.Uint16(p[:]), nil
}
// ReadUint32 reads a 32-bit number in the byte order specified by o
func (f *File) ReadUint32(o binary.ByteOrder) (uint32, error) {
p := [4]byte{}
if _, err := f.ReadFull(p[:]); err != nil {
return 0, fmt.Errorf("File.ReadUint32: %w", err)
}
return o.Uint32(p[:]), nil
}
// ReadUint64 reads a 64-bit number in the byte order specified by o
func (f *File) ReadUint64(o binary.ByteOrder) (uint64, error) {
p := [8]byte{}
if _, err := f.ReadFull(p[:]); err != nil {
return 0, fmt.Errorf("File.ReadUint64: %w", err)
}
return o.Uint64(p[:]), nil
}
// ReadInt16 is a wrapper around int16(ReadUint16)
func (f *File) ReadInt16(o binary.ByteOrder) (int16, error) {
n, err := f.ReadUint16(o)
return int16(n), err
}
// ReadInt32 is a wrapper around int32(ReadUint32)
func (f *File) ReadInt32(o binary.ByteOrder) (int32, error) {
n, err := f.ReadUint32(o)
return int32(n), err
}
// ReadInt64 is a wrapper around int64(ReadUint64)
func (f *File) ReadInt64(o binary.ByteOrder) (int64, error) {
n, err := f.ReadUint64(o)
return int64(n), err
}
// ReadFloat32 reads a 32-bit floating point number in the byte order specified by o
func (f *File) ReadFloat32(o binary.ByteOrder) (float32, error) {
n, err := f.ReadUint32(o)
if err != nil {
return 0, fmt.Errorf("File.ReadFloat32: %w", err)
}
return math.Float32frombits(n), nil
}
// ReadFloat64 reads a 64-bit floating point number in the byte order specified by o
func (f *File) ReadFloat64(o binary.ByteOrder) (float64, error) {
n, err := f.ReadUint64(o)
if err != nil {
return 0, fmt.Errorf("File.ReadFloat64: %w", err)
}
return math.Float64frombits(n), nil
}
// Expand grows the internal buffer to fill n bytes and sets pos to the end
//
// It returns a slice that should be filled with n bytes of content
func (f *File) Expand(n int) []byte {
n += f.pos
f.buf = slices.Grow(f.buf, n)
if n > len(f.buf) {
f.buf = f.buf[:n]
}
s := f.buf[f.pos:n]
f.pos = n
return s
}
// Grow increases the capacity of the internal buffer to guarantee space for another n byte without reallocation
func (f *File) Grow(n int) *File {
f.buf = slices.Grow(f.buf, f.pos+n)
return f
}
// ReadFrom implements io.ReaderFrom
func (f *File) ReadFrom(r io.Reader) (n int64, err error) {
for {
f.Grow(1 << 10)
m, err := r.Read(f.buf[f.pos:cap(f.buf)])
if m < 0 {
panic(fmt.Sprintf("%T.Read() returned negative count %d", r, m))
}
f.buf = f.buf[:f.pos+m]
n += int64(m)
if err != nil {
if errors.Is(err, io.EOF) {
return n, nil
}
return n, err
}
}
}
// WriteTo implements io.WriterTo
func (f *File) WriteTo(w io.Writer) (int64, error) {
if f.pos >= len(f.buf) {
return 0, fmt.Errorf("File.WriteTo: %w", io.ErrUnexpectedEOF)
}
s := f.buf[f.pos:]
n, err := w.Write(s)
f.pos += n
if err != nil {
return int64(n), fmt.Errorf("File.WriteTo: %w", err)
}
if n < len(s) {
return int64(n), fmt.Errorf("File.WriteTo: %w", io.ErrShortWrite)
}
return int64(n), nil
}
// Seek implements io.Writer
func (f *File) Write(p []byte) (int, error) {
return copy(f.Expand(len(p)), p), nil
}
// WriteString implements io.StringWriter
func (f *File) WriteString(p string) (int, error) {
s := f.Expand(len(p))
n := copy(s, p)
return n, nil
}
// WriteByte implements io.ByteWriter
func (f *File) WriteByte(p byte) error {
s := f.Expand(1)
s[0] = p
return nil
}
// WriteUint16 writes n in the byte order specified by o
func (f *File) WriteUint16(o binary.ByteOrder, n uint16) {
s := f.Expand(2)
o.PutUint16(s, n)
}
// WriteUint32 writes n in the byte order specified by o
func (f *File) WriteUint32(o binary.ByteOrder, n uint32) {
s := f.Expand(4)
o.PutUint32(s, n)
}
// WriteUint64 writes n in the byte order specified by o
func (f *File) WriteUint64(o binary.ByteOrder, n uint64) {
s := f.Expand(8)
o.PutUint64(s, n)
}
// WriteInt16 is a wrapper around f.WriteUint16(o, uint16(n))
func (f *File) WriteInt16(o binary.ByteOrder, n int16) {
f.WriteUint16(o, uint16(n))
}
// WriteInt32 is a wrapper around f.WriteUint32(o, uint32(n))
func (f *File) WriteInt32(o binary.ByteOrder, n int32) {
f.WriteUint32(o, uint32(n))
}
// WriteInt64 is a wrapper around f.WriteUint64(o, uint64(n))
func (f *File) WriteInt64(o binary.ByteOrder, n int64) {
f.WriteUint64(o, uint64(n))
}
// WriteFloat64 is a wrapper around f.WriteUint64(o, math.Float64bits(n))
func (f *File) WriteFloat64(o binary.ByteOrder, n float64) {
f.WriteUint64(o, math.Float64bits(n))
}
// WriteFloat32 is a wrapper around f.WriteUint32(o, math.Float32bits(n))
func (f *File) WriteFloat32(o binary.ByteOrder, n float32) {
f.WriteUint32(o, math.Float32bits(n))
}
// Seek implements io.Seeker
//
// If the final offset is greater than Len(), the internal buffer is expanded accordingly
func (f *File) Seek(offset int64, whence int) (int64, error) {
var sp int64
switch whence {
case io.SeekStart:
sp = offset
case io.SeekCurrent:
sp = int64(f.pos) + offset
case io.SeekEnd:
sp = int64(len(f.buf)) + offset
default:
return 0, fmt.Errorf("File.Seek: invalid whence(%d): %w", whence, fs.ErrInvalid)
}
if sp < 0 {
return 0, fmt.Errorf("File.Seek: negative offset(%d): %w", sp, fs.ErrInvalid)
}
f.pos = int(sp)
// simulates creating "holes" in files
if f.pos > len(f.buf) {
f.buf = slices.Grow(f.buf, f.pos)[:f.pos]
}
return sp, nil
}
// Rewind sets the Read position of the internal buffer back to the start
//
// It's equivalent to Seek(0, io.SeekStart) or Seek(0, 0)
func (f *File) Rewind() *File {
f.pos = 0
return f
}
// Stat implements the fs.File.Stat interface
//
// It always returns itself
func (f *File) Stat() (fs.FileInfo, error) {
return f, nil
}
// Close implements the fs.File.Close interface
//
// It always returns nil
func (f *File) Close() error {
return nil
}
// Name implements the fs.FileInfo.Name interface
//
// It always returns ""
func (f *File) Name() string {
return ""
}
// Size implements the fs.FileInfo.Size interface
//
// It returns the length of the internal buffer
func (f *File) Size() int64 {
return int64(len(f.buf))
}
// Mode implements the fs.FileInfo.Mode interface
//
// It always returns fs.ModeIrregular
func (f *File) Mode() fs.FileMode {
return fs.ModeIrregular
}
// ModTime implements the fs.FileInfo.ModTime interface
//
// It always returns time.Time{}
func (f *File) ModTime() time.Time {
return time.Time{}
}
// IsDir implements the fs.FileInfo.IsDir interface
//
// It always returns false.
func (f *File) IsDir() bool {
return false
}
// Sys implements the fs.FileInfo.Sys interface
//
// It always returns nil
func (f *File) Sys() any {
return nil
}
// NewFile returns a new File instance with the internal buffer set to s
func NewFile(s []byte) *File {
return &File{buf: s}
}