示例#1
0
// writeEncodedData -
func (b bucket) writeEncodedData(k, m uint8, writers []io.WriteCloser, objectData io.Reader, sumMD5, sum512 hash.Hash) (int, int, error) {
	chunks := split.Stream(objectData, 10*1024*1024)
	encoder, err := newEncoder(k, m, "Cauchy")
	if err != nil {
		return 0, 0, iodine.New(err, nil)
	}
	chunkCount := 0
	totalLength := 0
	for chunk := range chunks {
		if chunk.Err == nil {
			totalLength = totalLength + len(chunk.Data)
			encodedBlocks, _ := encoder.Encode(chunk.Data)
			sumMD5.Write(chunk.Data)
			sum512.Write(chunk.Data)
			for blockIndex, block := range encodedBlocks {
				_, err := io.Copy(writers[blockIndex], bytes.NewBuffer(block))
				if err != nil {
					return 0, 0, iodine.New(err, nil)
				}
			}
		}
		chunkCount = chunkCount + 1
	}
	return chunkCount, totalLength, nil
}
示例#2
0
func (s *MySuite) TestSplitStream(c *C) {
	var bytesBuffer bytes.Buffer
	bytesWriter := bufio.NewWriter(&bytesBuffer)
	for i := 0; i < 100; i++ {
		bytesWriter.Write([]byte(strconv.Itoa(i)))
	}
	bytesWriter.Flush()
	reader := bytes.NewReader(bytesBuffer.Bytes())
	ch := split.Stream(reader, 25)
	var resultsBuffer bytes.Buffer
	resultsWriter := bufio.NewWriter(&resultsBuffer)
	for chunk := range ch {
		resultsWriter.Write(chunk.Data)
	}
	resultsWriter.Flush()
	c.Assert(bytes.Compare(bytesBuffer.Bytes(), resultsBuffer.Bytes()), Equals, 0)
}