-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcontent.go
More file actions
56 lines (45 loc) · 1.24 KB
/
content.go
File metadata and controls
56 lines (45 loc) · 1.24 KB
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
package testutil
import (
"bytes"
"crypto/md5"
"fmt"
"io"
)
// ContentAt allows users of test clients to implement their own content storage.
// This is useful when mocking very large files.
type ContentAt interface {
io.ReaderAt
io.WriterAt
// Size returns the total byte count of the contents.
Size() int64
// Checksum returns the checksum of the contents. It is typically an MD5 hex
// string, following the S3 convention.
Checksum() string
}
// ByteContent stores data for content storage tests.
type ByteContent struct {
Data []byte
}
// ReadAt reads from the specified offset
func (bc *ByteContent) ReadAt(p []byte, off int64) (int, error) {
reader := bytes.NewReader(bc.Data)
return reader.ReadAt(p, off)
}
// WriteAt writes at the specified offset
func (bc *ByteContent) WriteAt(p []byte, off int64) (int, error) {
if off+int64(len(p)) > int64(len(bc.Data)) {
tmp := make([]byte, off+int64(len(p)))
copy(tmp, bc.Data)
bc.Data = tmp
}
copy(bc.Data[off:off+int64(len(p))], p)
return len(p), nil
}
// Checksum implements ContentAt.
func (bc *ByteContent) Checksum() string {
return fmt.Sprintf("%x", md5.Sum(bc.Data))
}
// Size returns the size of the contents
func (bc *ByteContent) Size() int64 {
return int64(len(bc.Data))
}