github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/fuse/ipns/writerat.go (about)

     1  package ipns
     2  
     3  import "io"
     4  
     5  type WriteAtBuf interface {
     6  	io.WriterAt
     7  	Bytes() []byte
     8  }
     9  
    10  type writerAt struct {
    11  	buf []byte
    12  }
    13  
    14  func NewWriterAtFromBytes(b []byte) WriteAtBuf {
    15  	return &writerAt{b}
    16  }
    17  
    18  // TODO: make this better in the future, this is just a quick hack for now
    19  func (wa *writerAt) WriteAt(p []byte, off int64) (int, error) {
    20  	if off+int64(len(p)) > int64(len(wa.buf)) {
    21  		wa.buf = append(wa.buf, make([]byte, (int(off)+len(p))-len(wa.buf))...)
    22  	}
    23  	copy(wa.buf[off:], p)
    24  	return len(p), nil
    25  }
    26  
    27  func (wa *writerAt) Bytes() []byte {
    28  	return wa.buf
    29  }