-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ba12af6
commit cbcc6cc
Showing
19 changed files
with
1,580 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ | |
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
output/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package tapewriter | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"sync" | ||
"syscall" | ||
) | ||
|
||
var ( | ||
_ = io.WriteCloser(new(BlockWriter)) | ||
) | ||
|
||
type BlockWriter struct { | ||
target uintptr | ||
blockSize int | ||
buffer chan []byte | ||
pool sync.Pool | ||
closed sync.WaitGroup | ||
|
||
current []byte | ||
off int | ||
} | ||
|
||
func NewBlockWriter(tape *os.File, blockSize, bufferBlocks int) *BlockWriter { | ||
w := &BlockWriter{ | ||
target: tape.Fd(), | ||
blockSize: blockSize, | ||
buffer: make(chan []byte, bufferBlocks), | ||
current: make([]byte, blockSize), | ||
pool: sync.Pool{New: func() interface{} { return make([]byte, blockSize) }}, | ||
} | ||
|
||
w.closed.Add(1) | ||
go w.loop() | ||
return w | ||
} | ||
|
||
func (w *BlockWriter) Write(buf []byte) (int, error) { | ||
var n, cn int | ||
for len(buf) > 0 { | ||
cn = copy(w.current, buf) | ||
buf = buf[cn:] | ||
w.off += cn | ||
n += cn | ||
|
||
if w.off >= w.blockSize { | ||
w.buffer <- w.current | ||
w.current = w.pool.Get().([]byte) | ||
} | ||
} | ||
|
||
return n, nil | ||
} | ||
|
||
func (w *BlockWriter) Close() error { | ||
w.buffer <- w.current[:w.off] | ||
close(w.buffer) | ||
|
||
w.closed.Wait() | ||
return nil | ||
} | ||
|
||
func (w *BlockWriter) loop() { | ||
defer w.closed.Done() | ||
|
||
for { | ||
buf, ok := <-w.buffer | ||
if !ok { | ||
break | ||
} | ||
|
||
_, err := syscall.Write(int(w.target), buf) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
} |
Oops, something went wrong.