writerat.go 559 Bytes
Newer Older
Jeromy's avatar
Jeromy committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
package ipns

import "io"

type WriteAtBuf interface {
	io.WriterAt
	Bytes() []byte
}

type writerAt struct {
	buf []byte
}

func NewWriterAtFromBytes(b []byte) WriteAtBuf {
	return &writerAt{b}
}

// TODO: make this better in the future, this is just a quick hack for now
func (wa *writerAt) WriteAt(p []byte, off int64) (int, error) {
	if off+int64(len(p)) > int64(len(wa.buf)) {
		wa.buf = append(wa.buf, make([]byte, (int(off)+len(p))-len(wa.buf))...)
	}
	copy(wa.buf[off:], p)
	return len(p), nil
}

func (wa *writerAt) Bytes() []byte {
	return wa.buf
}