2016-08-08 18:42:54 +02:00
|
|
|
// Copyright (c) 2013-2016 The btcsuite developers
|
2013-05-08 21:31:00 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2016-08-08 18:42:54 +02:00
|
|
|
package wire
|
2013-05-08 21:31:00 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-03-30 19:19:17 +02:00
|
|
|
// 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.
|
2013-05-08 21:31:00 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-02-25 18:17:12 +01:00
|
|
|
// Bytes returns the bytes already written to the fixed writer.
|
2013-05-08 21:31:00 +02:00
|
|
|
func (w *fixedWriter) Bytes() []byte {
|
|
|
|
return w.b
|
|
|
|
}
|
|
|
|
|
2014-03-30 19:19:17 +02:00
|
|
|
// 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 {
|
2017-03-22 21:18:32 +01:00
|
|
|
b := make([]byte, max)
|
2013-05-08 21:31:00 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-03-30 19:19:17 +02:00
|
|
|
// 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.
|
2013-05-08 21:31:00 +02:00
|
|
|
func (fr *fixedReader) Read(p []byte) (n int, err error) {
|
|
|
|
n, err = fr.iobuf.Read(p)
|
|
|
|
fr.pos += n
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-03-30 19:19:17 +02:00
|
|
|
// 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 {
|
2017-03-22 21:18:32 +01:00
|
|
|
b := make([]byte, max)
|
2013-05-08 21:31:00 +02:00
|
|
|
if buf != nil {
|
2018-09-13 23:35:08 +02:00
|
|
|
copy(b, buf)
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
iobuf := bytes.NewBuffer(b)
|
|
|
|
fr := fixedReader{b, 0, iobuf}
|
|
|
|
return &fr
|
|
|
|
}
|