-
Notifications
You must be signed in to change notification settings - Fork 48
/
util.go
59 lines (47 loc) · 1016 Bytes
/
util.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
// Copyright 2013 Michael Yang. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package id3
import (
"io"
"os"
)
func shiftBytesBack(file *os.File, start, offset int64) error {
stat, err := file.Stat()
if err != nil {
return err
}
end := stat.Size()
wrBuf := make([]byte, offset)
rdBuf := make([]byte, offset)
wrOffset := offset
rdOffset := start
rn, err := file.ReadAt(wrBuf, rdOffset)
if err != nil && err != io.EOF {
panic(err)
}
rdOffset += int64(rn)
for {
if rdOffset >= end {
break
}
n, err := file.ReadAt(rdBuf, rdOffset)
if err != nil && err != io.EOF {
return err
}
if rdOffset+int64(n) > end {
n = int(end - rdOffset)
}
if _, err := file.WriteAt(wrBuf[:rn], wrOffset); err != nil {
return err
}
rdOffset += int64(n)
wrOffset += int64(rn)
copy(wrBuf, rdBuf)
rn = n
}
if _, err := file.WriteAt(wrBuf[:rn], wrOffset); err != nil {
return err
}
return nil
}