package codec

import (
	"bytes"
	"encoding/gob"
	"io"
)

type GobCodec struct{}

func (codec *GobCodec) EncodeTo(w io.Writer, i interface{}) (err error) {
	return gob.NewEncoder(w).Encode(i)
}

func (codec *GobCodec) DecodeFrom(r io.Reader, i interface{}) (err error) {
	return gob.NewDecoder(r).Decode(i)
}

func (codec *GobCodec) Encode(i interface{}) (b []byte, err error) {
	writer := &bytes.Buffer{}
	if err = gob.NewEncoder(writer).Encode(i); err == nil {
		b = writer.Bytes()
	}
	return
}

func (codec *GobCodec) Decode(b []byte, i interface{}) (err error) {
	return gob.NewDecoder(bytes.NewReader(b)).Decode(i)
}
