1 // Copyright (c) 2013-2016 The btcsuite developers
2 // Use of this source code is governed by an ISC
3 // license that can be found in the LICENSE file.
12 // fixedWriter implements the io.Writer interface and intentially allows
13 // testing of error paths by forcing short writes.
14 type fixedWriter struct {
19 // Write writes the contents of p to w. When the contents of p would cause
20 // the writer to exceed the maximum allowed size of the fixed writer,
21 // io.ErrShortWrite is returned and the writer is left unchanged.
23 // This satisfies the io.Writer interface.
24 func (w *fixedWriter) Write(p []byte) (n int, err error) {
26 if w.pos+lenp > cap(w.b) {
27 return 0, io.ErrShortWrite
30 w.pos += copy(w.b[w.pos:], p)
34 // Bytes returns the bytes already written to the fixed writer.
35 func (w *fixedWriter) Bytes() []byte {
39 // newFixedWriter returns a new io.Writer that will error once more bytes than
40 // the specified max have been written.
41 func newFixedWriter(max int) io.Writer {
42 b := make([]byte, max)
43 fw := fixedWriter{b, 0}
47 // fixedReader implements the io.Reader interface and intentially allows
48 // testing of error paths by forcing short reads.
49 type fixedReader struct {
55 // Read reads the next len(p) bytes from the fixed reader. When the number of
56 // bytes read would exceed the maximum number of allowed bytes to be read from
57 // the fixed writer, an error is returned.
59 // This satisfies the io.Reader interface.
60 func (fr *fixedReader) Read(p []byte) (n int, err error) {
61 n, err = fr.iobuf.Read(p)
66 // newFixedReader returns a new io.Reader that will error once more bytes than
67 // the specified max have been read.
68 func newFixedReader(max int, buf []byte) io.Reader {
69 b := make([]byte, max)
74 iobuf := bytes.NewBuffer(b)
75 fr := fixedReader{b, 0, iobuf}