Skip to content

Commit

Permalink
Wrap reader and writer impl
Browse files Browse the repository at this point in the history
  • Loading branch information
kelindar committed Dec 11, 2021
1 parent d6b3829 commit bc0eee7
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 16 deletions.
8 changes: 8 additions & 0 deletions reader.go
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions reader_test.go
Expand Up @@ -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)
Expand Down
40 changes: 24 additions & 16 deletions writer.go
Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 ---------------------------
Expand All @@ -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))
}

Expand All @@ -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))
}

Expand Down Expand Up @@ -173,15 +181,15 @@ 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.
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 ---------------------------
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions writer_test.go
Expand Up @@ -4,6 +4,7 @@
package iostream

import (
"bytes"
"fmt"
"testing"
"time"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit bc0eee7

Please sign in to comment.