2015-02-05 22:41:38 +01:00
|
|
|
// copied from wire
|
2014-02-24 20:35:30 +01:00
|
|
|
|
|
|
|
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2014-05-08 21:48:42 +02:00
|
|
|
package txstore_test
|
2014-02-24 20:35:30 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
// fixedWriter implements the io.Writer interface and intentially allows
|
|
|
|
// testing of error paths by forcing short writes.
|
|
|
|
type fixedWriter struct {
|
|
|
|
b []byte
|
|
|
|
pos int
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write ...
|
|
|
|
func (w *fixedWriter) Write(p []byte) (n int, err error) {
|
|
|
|
lenp := len(p)
|
|
|
|
if w.pos+lenp > cap(w.b) {
|
|
|
|
return 0, io.ErrShortWrite
|
|
|
|
}
|
|
|
|
n = lenp
|
|
|
|
w.pos += copy(w.b[w.pos:], p)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bytes ...
|
|
|
|
func (w *fixedWriter) Bytes() []byte {
|
|
|
|
return w.b
|
|
|
|
}
|
|
|
|
|
|
|
|
// newFixedWriter...
|
|
|
|
func newFixedWriter(max int64) *fixedWriter {
|
|
|
|
b := make([]byte, max, max)
|
|
|
|
fw := fixedWriter{b, 0}
|
|
|
|
return &fw
|
|
|
|
}
|