-
Notifications
You must be signed in to change notification settings - Fork 3
/
tx_iter_file.go
47 lines (42 loc) · 879 Bytes
/
tx_iter_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
package main
import (
"bufio"
"encoding/hex"
"fmt"
"log"
"os"
)
type TxIterFile struct {
file *os.File
scanner *bufio.Scanner
}
func NewTxIterFile(filename string) *TxIterFile {
fmt.Printf("Transaction file name: %v\n", filename)
it := &TxIterFile{}
var err error
it.file, err = os.Open(filename)
if err != nil {
log.Fatalln("couldn't open file", err)
}
it.scanner = bufio.NewScanner(it.file)
buf := make([]byte, 0, 64*1024)
it.scanner.Buffer(buf, 1024*1024)
return it
}
func (it *TxIterFile) Next() []byte {
if !it.scanner.Scan() {
if it.scanner.Err() == nil {
return nil
}
log.Fatalln("Couldn't scan next line in file:", it.scanner.Err())
}
line := it.scanner.Text()
b, err := hex.DecodeString(line)
if err != nil {
log.Fatalln("Couldn't decode line from file:", err)
}
return b
}
func (it *TxIterFile) Close() {
it.file.Close()
}