diff --git a/reader.go b/reader.go index 762a793..d3874da 100644 --- a/reader.go +++ b/reader.go @@ -23,6 +23,14 @@ func NewReader(r io.Reader) *Reader { } } +// --------------------------- io.Reader --------------------------- + +// Read implements io.Reader interface by simply calling the Read method on +// the underlying stream. +func (r *Reader) Read(p []byte) (n int, err error) { + return r.src.Read(p) +} + // --------------------------- Unsigned Integers --------------------------- // ReadUvarint reads a variable-length Uint64 from the buffer. diff --git a/reader_test.go b/reader_test.go index 97d8b2c..a9661f7 100644 --- a/reader_test.go +++ b/reader_test.go @@ -31,6 +31,12 @@ func TestStreamReadShortBuffer(t *testing.T) { } } +func TestReaderImpl(t *testing.T) { + r := NewReader(bytes.NewBuffer(nil)) + _, err := r.Read([]byte{}) + assert.Error(t, err) +} + // assertRead asserts a single read operation func assertRead(t *testing.T, name string, fn func(*Reader) (interface{}, error), input []byte, expect interface{}) { assertReadN(t, name, fn, input, expect, 99999) diff --git a/writer.go b/writer.go index 765f838..eef7507 100644 --- a/writer.go +++ b/writer.go @@ -28,8 +28,16 @@ func (w *Writer) Reset(out io.Writer) { w.out = out } +// --------------------------- io.Writer --------------------------- + +// Write implements io.Writer interface by simply writing into the underlying +// souurce. +func (w *Writer) Write(p []byte) (int, error) { + return w.out.Write(p) +} + // Write writes the contents of p into the buffer. -func (w *Writer) Write(p []byte) (err error) { +func (w *Writer) write(p []byte) (err error) { _, err = w.out.Write(p) return } @@ -45,7 +53,7 @@ func (w *Writer) WriteUvarint(x uint64) error { i++ } w.scratch[i] = byte(x) - return w.Write(w.scratch[:(i + 1)]) + return w.write(w.scratch[:(i + 1)]) } // WriteUint writes a Uint @@ -55,15 +63,15 @@ func (w *Writer) WriteUint(v uint) error { // WriteUint8 writes a Uint8 func (w *Writer) WriteUint8(v uint8) error { - w.scratch[0] = byte(v) - return w.Write(w.scratch[:1]) + w.scratch[0] = v + return w.write(w.scratch[:1]) } // WriteUint16 writes a Uint16 func (w *Writer) WriteUint16(v uint16) error { w.scratch[0] = byte(v) w.scratch[1] = byte(v >> 8) - return w.Write(w.scratch[:2]) + return w.write(w.scratch[:2]) } // WriteUint32 writes a Uint32 @@ -72,7 +80,7 @@ func (w *Writer) WriteUint32(v uint32) error { w.scratch[1] = byte(v >> 8) w.scratch[2] = byte(v >> 16) w.scratch[3] = byte(v >> 24) - return w.Write(w.scratch[:4]) + return w.write(w.scratch[:4]) } // WriteUint64 writes a Uint64 @@ -85,7 +93,7 @@ func (w *Writer) WriteUint64(v uint64) error { w.scratch[5] = byte(v >> 40) w.scratch[6] = byte(v >> 48) w.scratch[7] = byte(v >> 56) - return w.Write(w.scratch[:8]) + return w.write(w.scratch[:8]) } // --------------------------- Signed Integers --------------------------- @@ -104,11 +112,11 @@ func (w *Writer) WriteVarint(v int64) error { i++ } w.scratch[i] = byte(x) - return w.Write(w.scratch[:(i + 1)]) + return w.write(w.scratch[:(i + 1)]) } -// WriteUint writes an int -func (w *Writer) WriteInt(v uint) error { +// WriteInt writes an int +func (w *Writer) WriteInt(v int) error { return w.WriteUint64(uint64(v)) } @@ -118,17 +126,17 @@ func (w *Writer) WriteInt8(v int8) error { } // WriteInt16 writes an int16 -func (w *Writer) WriteInt16(v uint16) error { +func (w *Writer) WriteInt16(v int16) error { return w.WriteUint16(uint16(v)) } // WriteInt32 writes an int32 -func (w *Writer) WriteInt32(v uint32) error { +func (w *Writer) WriteInt32(v int32) error { return w.WriteUint32(uint32(v)) } // WriteInt64 writes an int64 -func (w *Writer) WriteInt64(v uint64) error { +func (w *Writer) WriteInt64(v int64) error { return w.WriteUint64(uint64(v)) } @@ -173,7 +181,7 @@ func (w *Writer) WriteString(v string) error { if err := w.WriteUvarint(uint64(len(v))); err != nil { return err } - return w.Write(toBytes(v)) + return w.write(toBytes(v)) } // WriteBytes writes a byte slice prefixed with a variable-size integer. @@ -181,7 +189,7 @@ func (w *Writer) WriteBytes(v []byte) error { if err := w.WriteUvarint(uint64(len(v))); err != nil { return err } - return w.Write(v) + return w.write(v) } // --------------------------- Other Types --------------------------- @@ -192,7 +200,7 @@ func (w *Writer) WriteBool(v bool) error { if v { w.scratch[0] = 1 } - return w.Write(w.scratch[:1]) + return w.write(w.scratch[:1]) } // WriteComplex64 a 64-bit complex number diff --git a/writer_test.go b/writer_test.go index aa2a1b6..7acb33d 100644 --- a/writer_test.go +++ b/writer_test.go @@ -4,6 +4,7 @@ package iostream import ( + "bytes" "fmt" "testing" "time" @@ -175,6 +176,12 @@ func TestWriteFailuresString(t *testing.T) { }, nil, 0) } +func TestWriteMethod(t *testing.T) { + w := NewWriter(bytes.NewBuffer(nil)) + _, err := w.Write(nil) + assert.NoError(t, err) +} + // assertWrite asserts a single write operation func assertWrite(t *testing.T, name string, fn func(*Writer) error, expect []byte) { assertWriteN(t, name, fn, expect, 99999)