예제 #1
0
func Save(blobpath string, c btncrypt.Cipher, bs blobstore.BlobStore, cb EncodeCallback) error {
	var buf bytes.Buffer
	zw := zlib.NewWriter(&buf)
	enc := gob.NewEncoder(zw)

	es := []error{}
	if err := cb(enc); err != nil {
		es = append(es, fmt.Errorf("Failed to encode state: %v", err))
	}
	if err := zw.Close(); err != nil {
		es = append(es, fmt.Errorf("Failed to close zlib Writer: %v", err))
	}

	if err := util.ToErrors(es); err != nil {
		return err
	}

	w, err := bs.OpenWriter(blobpath)
	if err != nil {
		return err
	}
	cw, err := chunkstore.NewChunkWriter(w, c, chunkstore.ChunkHeader{
		PayloadLen:     uint32(buf.Len()),
		PayloadVersion: 1,
		OrigFilename:   blobpath,
		OrigOffset:     0,
	})
	if err != nil {
		return err
	}

	if _, err := cw.Write(buf.Bytes()); err != nil {
		es = append(es, fmt.Errorf("Failed to write to ChunkWriter: %v", err))
	}

	if err := cw.Close(); err != nil {
		es = append(es, fmt.Errorf("Failed to close ChunkWriter: %v", err))
	}
	if err := w.Close(); err != nil {
		es = append(es, fmt.Errorf("Failed to close blobhandle: %v", err))
	}

	if err := util.ToErrors(es); err != nil {
		return err
	}
	return nil
}
예제 #2
0
func genFrameByChunkWriter(t *testing.T, p []byte) []byte {
	buf := new(bytes.Buffer)
	cw, err := chunkstore.NewChunkWriter(buf, TestCipher(), chunkstore.ChunkHeader{PayloadLen: uint32(len(p))})
	if err != nil {
		t.Errorf("Failed to create chunk writer: %v", err)
	}

	if _, err := cw.Write(p); err != nil {
		t.Errorf("Failed to write chunk payload: %v", err)
		return nil
	}

	if err := cw.Close(); err != nil {
		t.Errorf("Failed to close ChunkWriter: %v", err)
		return nil
	}

	return buf.Bytes()
}