-
Notifications
You must be signed in to change notification settings - Fork 2
/
buffer_readwritecloser.go
49 lines (42 loc) · 1.24 KB
/
buffer_readwritecloser.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
package buffer
import (
"github.com/DSiSc/craft/types"
"io"
)
type SystemBufferReadWriterCloser struct {
sysBufferContract *SystemBufferContract
cursor uint64
}
//NewSystemBufferReadWriterCloser create a new instance
func NewSystemBufferReadWriterCloser(sysBufferContract *SystemBufferContract) *SystemBufferReadWriterCloser {
return &SystemBufferReadWriterCloser{
cursor: 0,
sysBufferContract: sysBufferContract,
}
}
func (this *SystemBufferReadWriterCloser) Read(data []byte) (n int, err error) {
totalLen := this.sysBufferContract.Length()
if this.cursor >= totalLen {
return 0, io.EOF
}
size := uint64(0)
if totalLen-this.cursor > uint64(len(data)) {
size = uint64(len(data))
} else {
size = totalLen - this.cursor
}
ret, err := this.sysBufferContract.Read(this.cursor, size)
n = copy(data, ret)
this.cursor += uint64(n)
return n, nil
}
func (this *SystemBufferReadWriterCloser) Write(data []byte) (n int, err error) {
len, err := this.sysBufferContract.Write(data)
return int(len), err
}
func (this *SystemBufferReadWriterCloser) Close() error {
return this.sysBufferContract.Close()
}
func (this *SystemBufferReadWriterCloser) ContractAddress() types.Address {
return this.sysBufferContract.Address()
}