// writeObjectData - func (b bucket) writeObjectData(k, m uint8, writers []io.WriteCloser, objectData io.Reader, sumMD5, sum256, sum512 hash.Hash) (int, int, error) { encoder, err := newEncoder(k, m, "Cauchy") if err != nil { return 0, 0, iodine.New(err, nil) } chunkCount := 0 totalLength := 0 for chunk := range split.Stream(objectData, 10*1024*1024) { if chunk.Err != nil { return 0, 0, iodine.New(err, nil) } totalLength = totalLength + len(chunk.Data) encodedBlocks, err := encoder.Encode(chunk.Data) if err != nil { return 0, 0, iodine.New(err, nil) } sumMD5.Write(chunk.Data) sum256.Write(chunk.Data) sum512.Write(chunk.Data) for blockIndex, block := range encodedBlocks { errCh := make(chan error, 1) go func(writer io.Writer, reader io.Reader) { defer close(errCh) _, err := io.Copy(writers[blockIndex], bytes.NewReader(block)) errCh <- err }(writers[blockIndex], bytes.NewReader(block)) if err := <-errCh; err != nil { return 0, 0, iodine.New(err, nil) } } chunkCount = chunkCount + 1 } return chunkCount, totalLength, nil }
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) }