2eef3720a9
This commit contains the entire btcwire repository along with several changes needed to move all of the files into the wire directory in order to prepare it for merging. This does NOT update btcd or any of the other packages to use the new location as that will be done separately. - All import paths in the old btcwire test files have been changed to the new location - All references to btcwire as the package name have been chagned to wire - The coveralls badge has been removed since it unfortunately doesn't support coverage of sub-packages This is ongoing work toward #214.
77 lines
2 KiB
Go
77 lines
2 KiB
Go
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
|
// Use of this source code is governed by an ISC
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package wire_test
|
|
|
|
import (
|
|
"bytes"
|
|
"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 writes the contents of p to w. When the contents of p would cause
|
|
// the writer to exceed the maximum allowed size of the fixed writer,
|
|
// io.ErrShortWrite is returned and the writer is left unchanged.
|
|
//
|
|
// This satisfies the io.Writer interface.
|
|
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 returns the bytes alreayd written to the fixed writer.
|
|
func (w *fixedWriter) Bytes() []byte {
|
|
return w.b
|
|
}
|
|
|
|
// newFixedWriter returns a new io.Writer that will error once more bytes than
|
|
// the specified max have been written.
|
|
func newFixedWriter(max int) io.Writer {
|
|
b := make([]byte, max, max)
|
|
fw := fixedWriter{b, 0}
|
|
return &fw
|
|
}
|
|
|
|
// fixedReader implements the io.Reader interface and intentially allows
|
|
// testing of error paths by forcing short reads.
|
|
type fixedReader struct {
|
|
buf []byte
|
|
pos int
|
|
iobuf *bytes.Buffer
|
|
}
|
|
|
|
// Read reads the next len(p) bytes from the fixed reader. When the number of
|
|
// bytes read would exceed the maximum number of allowed bytes to be read from
|
|
// the fixed writer, an error is returned.
|
|
//
|
|
// This satisfies the io.Reader interface.
|
|
func (fr *fixedReader) Read(p []byte) (n int, err error) {
|
|
n, err = fr.iobuf.Read(p)
|
|
fr.pos += n
|
|
return
|
|
}
|
|
|
|
// newFixedReader returns a new io.Reader that will error once more bytes than
|
|
// the specified max have been read.
|
|
func newFixedReader(max int, buf []byte) io.Reader {
|
|
b := make([]byte, max, max)
|
|
if buf != nil {
|
|
copy(b[:], buf)
|
|
}
|
|
|
|
iobuf := bytes.NewBuffer(b)
|
|
fr := fixedReader{b, 0, iobuf}
|
|
return &fr
|
|
}
|