func (*sizeSuite) TestSizingReaderOkay(c *gc.C) { expected := "some data" stub := &testing.Stub{} reader := filetesting.NewStubReader(stub, expected) var st utils.SizeTracker sizingReader := io.TeeReader(reader, &st) data, err := ioutil.ReadAll(sizingReader) c.Assert(err, jc.ErrorIsNil) stub.CheckCallNames(c, "Read", "Read") c.Check(string(data), gc.Equals, expected) c.Check(st.Size(), gc.Equals, int64(len(expected))) }
func (*sizeSuite) TestSizingWriter(c *gc.C) { expected := "some data" stub := &testing.Stub{} writer, buffer := filetesting.NewStubWriter(stub) var st utils.SizeTracker sizingWriter := io.MultiWriter(writer, &st) n, err := sizingWriter.Write([]byte(expected)) c.Assert(err, jc.ErrorIsNil) stub.CheckCallNames(c, "Write") c.Check(n, gc.Equals, len(expected)) c.Check(buffer.String(), gc.Equals, expected) c.Check(st.Size(), gc.Equals, int64(len(expected))) }
func (*sizeSuite) TestSizingReaderMixedEOF(c *gc.C) { expected := "some data" stub := &testing.Stub{} reader := &filetesting.StubReader{ Stub: stub, ReturnRead: &fakeStream{ data: expected, }, } var st utils.SizeTracker sizingReader := io.TeeReader(reader, &st) data, err := ioutil.ReadAll(sizingReader) c.Assert(err, jc.ErrorIsNil) stub.CheckCallNames(c, "Read") // The EOF was mixed with the data. c.Check(string(data), gc.Equals, expected) c.Check(st.Size(), gc.Equals, int64(len(expected))) }
// GenerateContent returns a new Content for the given data stream. func GenerateContent(reader io.ReadSeeker) (Content, error) { var sizer utils.SizeTracker sizingReader := io.TeeReader(reader, &sizer) fp, err := charmresource.GenerateFingerprint(sizingReader) if err != nil { return Content{}, errors.Trace(err) } if _, err := reader.Seek(0, os.SEEK_SET); err != nil { return Content{}, errors.Trace(err) } size := sizer.Size() content := Content{ Data: reader, Size: size, Fingerprint: fp, } return content, nil }