本文整理匯總了Golang中io.TeeReader函數的典型用法代碼示例。如果您正苦於以下問題:Golang TeeReader函數的具體用法?Golang TeeReader怎麽用?Golang TeeReader使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了TeeReader函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: decode
// decoder reads an image from r and modifies the image as defined by opts.
// swapDimensions indicates the decoded image will be rotated after being
// returned, and when interpreting opts, the post-rotation dimensions should
// be considered.
// The decoded image is returned in im. The registered name of the decoder
// used is returned in format. If the image was not successfully decoded, err
// will be non-nil. If the decoded image was made smaller, needRescale will
// be true.
func decode(r io.Reader, opts *DecodeOpts, swapDimensions bool) (im image.Image, format string, err error, needRescale bool) {
if opts == nil {
// Fall-back to normal decode.
im, format, err = image.Decode(r)
return im, format, err, false
}
var buf bytes.Buffer
tr := io.TeeReader(r, &buf)
ic, format, err := image.DecodeConfig(tr)
if err != nil {
return nil, "", err, false
}
mr := io.MultiReader(&buf, r)
b := image.Rect(0, 0, ic.Width, ic.Height)
sw, sh, needRescale := opts.rescaleDimensions(b, swapDimensions)
if !needRescale {
im, format, err = image.Decode(mr)
return im, format, err, false
}
imageDebug(fmt.Sprintf("Resizing from %dx%d -> %dx%d", ic.Width, ic.Height, sw, sh))
if format == "cr2" {
// Replace mr with an io.Reader to the JPEG thumbnail embedded in a
// CR2 image.
if mr, err = cr2.NewReader(mr); err != nil {
return nil, "", err, false
}
format = "jpeg"
}
if format == "jpeg" && fastjpeg.Available() {
factor := fastjpeg.Factor(ic.Width, ic.Height, sw, sh)
if factor > 1 {
var buf bytes.Buffer
tr := io.TeeReader(mr, &buf)
im, err = fastjpeg.DecodeDownsample(tr, factor)
switch err.(type) {
case fastjpeg.DjpegFailedError:
log.Printf("Retrying with jpeg.Decode, because djpeg failed with: %v", err)
im, err = jpeg.Decode(io.MultiReader(&buf, mr))
case nil:
// fallthrough to rescale() below.
default:
return nil, format, err, false
}
return rescale(im, sw, sh), format, err, true
}
}
// Fall-back to normal decode.
im, format, err = image.Decode(mr)
if err != nil {
return nil, "", err, false
}
return rescale(im, sw, sh), format, err, needRescale
}
示例2: TestInline
func TestInline(t *testing.T) {
esc := `url("data:image/svg+xml;utf8,%3C%3Fxml%20version=%221.0%22%20encoding=%22utf-8%22%3F%3E%3C%21--%20Generator:%20Adobe%20Illustrator%2018.1.0,%20SVG%20Export%20Plug-In%20.%20SVG%20Version:%206.00%20Build%200%29%20%20--%3E%3Csvg%20version=%221.1%22%20id=%22Gopher%22%20xmlns=%22http://www.w3.org/2000/svg%22%20xmlns:xlink=%22http://www.w3.org/1999/xlink%22%20x=%220px%22%20y=%220px%22%09%20viewBox=%220%200%20215.6%20281.6%22%20enable-background=%22new%200%200%20215.6%20281.6%22%20xml:space=%22preserve%22%3E%3Cg%3E%09%3Cpath%20fill=%22%238CC5E7%22%20d=%22M207.3,44.6c-6.7-13.7-22.9-1.6-27-5.9c-21-21.6-46.4-27-66.3-28c0,0-9,0-11,0c-20,0.5-45.4,6.3-66.3,28%09%09c-4.1,4.3-20.4-7.8-27,5.9c-7.7,16,15.7,17.6,14.5,24.7c-2.3,12.8-0.8,31.8,1,50.5C28,151.5,4.3,227.4,53.6,257.9%09%09c9.3,5.8,34.4,9,56.2,9.5l0,0c0,0,0.1,0,0.1,0c0,0,0.1,0,0.1,0l0,0c21.8-0.5,43.9-3.7,53.2-9.5c49.4-30.5,25.7-106.4,28.6-138.1%09%09c1.7-18.7,3.2-37.7,1-50.5C191.6,62.2,215,60.5,207.3,44.6z%22/%3E%09%3Cg%3E%09%09%3Cpath%20fill=%22%23E0DEDC%22%20d=%22M143.2,54.3c-33.4,3.9-28.9,38.7-16,50c24,21,49,0,46.2-21.2C170.9,62.7,153.6,53.1,143.2,54.3z%22/%3E%09%09%3Ccircle%20fill=%22%23111212%22%20cx=%22145.5%22%20cy=%2284.3%22%20r=%2211.4%22/%3E%09%09%3Ccircle%20fill=%22%23FFFFFF%22%20cx=%22142.5%22%20cy=%2279.4%22%20r=%223.6%22/%3E%09%3C/g%3E%09%3Cg%3E%09%09%3Cpath%20fill=%22%23B8937F%22%20d=%22M108.5,107c-16,2.4-21.7,7-20.5,14.2c2,11.8,39.7,10.5,40.9,0.6C129.9,113.3,114.8,106.1,108.5,107z%22/%3E%09%09%3Cpath%20d=%22M98.2,111.8c-2.7,9.8,21.7,8.3,21.1,2c-0.3-3.7-3.6-8.4-12.3-8.2C103.6,105.7,99.4,107.2,98.2,111.8z%22/%3E%09%09%3Cpath%20fill=%22%23E0DEDC%22%20d=%22M99,127.7c-0.9,0.4-2.4,10.2,2.2,10.7c3.1,0.3,11.6,1.3,13.6,0c3.9-2.5,3.5-8.5,1.3-10%09%09%09C112.4,126,100,127.2,99,127.7z%22/%3E%09%3C/g%3E%09%3Cg%3E%09%09%3Cpath%20fill=%22%23E0DEDC%22%20d=%22M73.6,54.3c33.4,3.9,28.9,38.7,16,50c-24,21-49,0-46.2-21.2C46,62.7,63.3,53.1,73.6,54.3z%22/%3E%09%09%3Ccircle%20fill=%22%23111212%22%20cx=%2271.4%22%20cy=%2284.3%22%20r=%2211.4%22/%3E%09%09%3Ccircle%20fill=%22%23FFFFFF%22%20cx=%2274.4%22%20cy=%2279.4%22%20r=%223.6%22/%3E%09%3C/g%3E%09%3Cpath%20fill=%22%23B8937F%22%20d=%22M193.6,186.7c11,0.1,5.6-23.5-1.2-18.8c-3.3,2.3-3.9,7.6-3.9,12.1C188.5,182.5,190.5,186.6,193.6,186.7z%22/%3E%09%3Cpath%20fill=%22%23B8937F%22%20d=%22M23.3,186.7c-11,0.1-5.6-23.5,1.2-18.8c3.3,2.3,3.9,7.6,3.9,12.1C28.4,182.5,26.4,186.6,23.3,186.7z%22/%3E%09%3Cpath%20fill=%22%23B8937F%22%20d=%22M172.7,259.2c-6-8.9-11.4-2-20.1,2.4c-4.1,2.1,6.8,9.6,19,4C174.8,264.1,174.7,262.1,172.7,259.2z%22/%3E%09%3Cpath%20fill=%22%23B8937F%22%20d=%22M44.2,260.2c6-8.9,11.4-2,20.1,2.4c4.1,2.1-6.8,9.6-19,4C42.1,265.1,42.2,263.1,44.2,260.2z%22/%3E%09%3Cpath%20fill=%22%233C89BF%22%20d=%22M188.6,47c-0.6,2.1,2.1,1.8,3.1,8.3c0.4,2.4,9-3.5,5.5-7.8C194.3,43.9,189.1,44.9,188.6,47z%22/%3E%09%3Cpath%20fill=%22%233C89BF%22%20d=%22M28.3,47c0.6,2.1-2.1,1.8-3.1,8.3c-0.4,2.4-9-3.5-5.5-7.8C22.5,43.9,27.7,44.9,28.3,47z%22/%3E%3C/g%3E%3C/svg%3E")`
b64 := `url("data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4NCjwhLS0gR2VuZXJhdG9yOiBBZG9iZSBJbGx1c3RyYXRvciAxOC4xLjAsIFNWRyBFeHBvcnQgUGx1Zy1JbiAuIFNWRyBWZXJzaW9uOiA2LjAwIEJ1aWxkIDApICAtLT4NCjxzdmcgdmVyc2lvbj0iMS4xIiBpZD0iR29waGVyIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB4PSIwcHgiIHk9IjBweCINCgkgdmlld0JveD0iMCAwIDIxNS42IDI4MS42IiBlbmFibGUtYmFja2dyb3VuZD0ibmV3IDAgMCAyMTUuNiAyODEuNiIgeG1sOnNwYWNlPSJwcmVzZXJ2ZSI+DQo8Zz4NCgk8cGF0aCBmaWxsPSIjOENDNUU3IiBkPSJNMjA3LjMsNDQuNmMtNi43LTEzLjctMjIuOS0xLjYtMjctNS45Yy0yMS0yMS42LTQ2LjQtMjctNjYuMy0yOGMwLDAtOSwwLTExLDBjLTIwLDAuNS00NS40LDYuMy02Ni4zLDI4DQoJCWMtNC4xLDQuMy0y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NNDQuMiwyNjAuMmM2LTguOSwxMS40LTIsMjAuMSwyLjRjNC4xLDIuMS02LjgsOS42LTE5LDRDNDIuMSwyNjUuMSw0Mi4yLDI2My4xLDQ0LjIsMjYwLjJ6Ii8+DQoJPHBhdGggZmlsbD0iIzNDODlCRiIgZD0iTTE4OC42LDQ3Yy0wLjYsMi4xLDIuMSwxLjgsMy4xLDguM2MwLjQsMi40LDktMy41LDUuNS03LjhDMTk0LjMsNDMuOSwxODkuMSw0NC45LDE4OC42LDQ3eiIvPg0KCTxwYXRoIGZpbGw9IiMzQzg5QkYiIGQ9Ik0yOC4zLDQ3YzAuNiwyLjEtMi4xLDEuOC0zLjEsOC4zYy0wLjQsMi40LTktMy41LTUuNS03LjhDMjIuNSw0My45LDI3LjcsNDQuOSwyOC4zLDQ3eiIvPg0KPC9nPg0KPC9zdmc+")`
f, err := os.Open("test/gopher-front.svg")
if err != nil {
t.Error(err)
}
var fb, fbb, buf bytes.Buffer
tr := io.TeeReader(f, &fb)
Inline(tr, &buf)
if esc != buf.String() {
t.Errorf("got:\n%s\nwanted:\n%s", buf.String(), esc)
}
tr = io.TeeReader(&fb, &fbb)
buf.Reset()
Inline(tr, &buf, false)
if esc != buf.String() {
t.Errorf("got:\n%s\nwanted:\n%s", buf.String(), esc)
}
buf.Reset()
Inline(&fbb, &buf, true)
if b64 != buf.String() {
t.Errorf("got:\n%s\nwanted:\n%s", buf.String(), b64)
}
}
示例3: executeCommand
func (s Shell) executeCommand() (err error) {
outReader, err := s.Command.StdoutPipe()
if err != nil {
return
}
errReader, err := s.Command.StderrPipe()
if err != nil {
return
}
var bufout, buferr bytes.Buffer
outReader2 := io.TeeReader(outReader, &bufout)
errReader2 := io.TeeReader(errReader, &buferr)
s.Command.Start()
outScanner := bufio.NewScanner(outReader2)
for outScanner.Scan() {
fmt.Println(outScanner.Text())
}
errScanner := bufio.NewScanner(errReader2)
for errScanner.Scan() {
fmt.Println("[stderr]" + errScanner.Text())
}
s.Command.Wait()
return
}
示例4: Download
func (dm *downloadManager) Download(ctx context.Context, initialRootFS image.RootFS, layers []xfer.DownloadDescriptor, progressOutput progress.Output) (image.RootFS, func(), error) {
for _, l := range layers {
b, err := dm.blobStore.New()
if err != nil {
return initialRootFS, nil, err
}
defer b.Close()
rc, _, err := l.Download(ctx, progressOutput)
if err != nil {
return initialRootFS, nil, errors.Wrap(err, "failed to download")
}
defer rc.Close()
r := io.TeeReader(rc, b)
inflatedLayerData, err := archive.DecompressStream(r)
if err != nil {
return initialRootFS, nil, err
}
digester := digest.Canonical.New()
if _, err := archive.ApplyLayer(dm.tmpDir, io.TeeReader(inflatedLayerData, digester.Hash())); err != nil {
return initialRootFS, nil, err
}
initialRootFS.Append(layer.DiffID(digester.Digest()))
d, err := b.Commit()
if err != nil {
return initialRootFS, nil, err
}
dm.blobs = append(dm.blobs, d)
}
return initialRootFS, nil, nil
}
示例5: main
func main() {
src, err := os.Open("src.txt")
if err != nil {
panic(err)
}
defer src.Close()
dst1, err := os.Create("dst1.txt")
if err != nil {
panic(err)
}
defer dst1.Close()
dst2, err := os.Create("dst2.txt")
if err != nil {
panic(err)
}
defer dst2.Close()
dst3, err := os.Create("dst3.txt")
if err != nil {
panic(err)
}
defer dst3.Close()
rdr := io.TeeReader(src, dst1)
rdr = io.TeeReader(rdr, os.Stdout)
rdr = io.TeeReader(rdr, dst2)
io.Copy(dst3, rdr)
}
示例6: multiunique
func multiunique(src, dst io.Reader) io.Reader {
ur := &uniqueReader{}
ur.src = io.TeeReader(src, &ur.bufsrc)
ur.dst = io.TeeReader(dst, &ur.bufdst)
ur.r = io.MultiReader(&ur.bufsrc, src, &ur.bufdst, dst)
return ur
}
示例7: copyBlob
func (sh *SyncHandler) copyBlob(sb blob.SizedRef) (err error) {
cs := sh.newCopyStatus(sb)
defer func() { cs.setError(err) }()
br := sb.Ref
sh.mu.Lock()
sh.copying[br] = cs
sh.mu.Unlock()
if strings.Contains(storageDesc(sh.to), "bradfitz-camlistore-pt") {
//sh.logf("LIES NOT ACTUALLY COPYING")
//return nil
}
if sb.Size > constants.MaxBlobSize {
return fmt.Errorf("blob size %d too large; max blob size is %d", sb.Size, constants.MaxBlobSize)
}
cs.setStatus(statusFetching)
rc, fromSize, err := sh.from.FetchStreaming(br)
if err != nil {
return fmt.Errorf("source fetch: %v", err)
}
if fromSize != sb.Size {
rc.Close()
return fmt.Errorf("source fetch size mismatch: get=%d, enumerate=%d", fromSize, sb.Size)
}
buf := make([]byte, fromSize)
hash := br.Hash()
cs.setStatus(statusReading)
n, err := io.ReadFull(io.TeeReader(rc,
io.MultiWriter(
incrWriter{cs, &cs.nread},
hash,
)), buf)
rc.Close()
if err != nil {
return fmt.Errorf("Read error after %d/%d bytes: %v", n, fromSize, err)
}
if !br.HashMatches(hash) {
return fmt.Errorf("Read data has unexpected digest %x", hash.Sum(nil))
}
cs.setStatus(statusWriting)
newsb, err := sh.to.ReceiveBlob(br, io.TeeReader(bytes.NewReader(buf), incrWriter{cs, &cs.nwrite}))
if err != nil {
return fmt.Errorf("dest write: %v", err)
}
if newsb.Size != sb.Size {
return fmt.Errorf("write size mismatch: source_read=%d but dest_write=%d", sb.Size, newsb.Size)
}
return nil
}
示例8: ParsingHandler
func ParsingHandler(serverConn, clientConn net.Conn) error {
log.Printf("Proxying new connection from %v", clientConn.RemoteAddr())
s2cLog := util.NewLogger(fmt.Sprintf("%s->%s", serverConn.RemoteAddr(), clientConn.RemoteAddr()))
c2sLog := util.NewLogger(fmt.Sprintf("%s->%s", clientConn.RemoteAddr(), serverConn.RemoteAddr()))
go parser(io.TeeReader(serverConn, clientConn), serverConn, s2cLog)
go parser(io.TeeReader(clientConn, serverConn), clientConn, c2sLog)
return nil
}
示例9: walk
func walk(path string, info os.FileInfo, inErr error) (err error) {
if inErr != nil {
return inErr
}
if !info.Mode().IsRegular() {
return
}
shouldRemove := false
file, err := os.Open(path)
if err != nil {
return
}
defer func() {
file.Close()
if shouldRemove {
err = os.Remove(path)
}
}()
crc32Hash.Reset()
md5Hash.Reset()
sha1Hash.Reset()
md5TeeReader := io.TeeReader(file, md5Hash)
sha1TeeReader := io.TeeReader(md5TeeReader, sha1Hash)
io.CopyBuffer(crc32Hash, sha1TeeReader, copyBuffer)
var key FileSum
key.size = info.Size()
key.crc32Sum = crc32Hash.Sum32()
copy(key.md5Sum[:], md5Hash.Sum(nil))
copy(key.sha1Sum[:], sha1Hash.Sum(nil))
fmt.Printf("path: %s\r\nsize: %d bytes\r\ncrc32: %x\r\nmd5: %s\r\nsha1: %s\r\n\r\n",
path,
key.size,
key.crc32Sum,
hex.EncodeToString(key.md5Sum[:]),
hex.EncodeToString(key.sha1Sum[:]),
)
if pathx, ok := FileSumSet[key]; ok {
shouldRemove = true
fmt.Printf("%s 與 %s 重複, 將被移除\r\n", path, pathx)
} else {
FileSumSet[key] = path
}
return
}
示例10: main
func main() {
flag.Parse()
args := flag.Args()
conn, err := net.Dial("tcp", config.Addr)
fatalOnError(err)
defer conn.Close()
var tee io.Reader
var timeoutCh <-chan time.Time
if output {
if wc, ok := conn.(WriteCloser); ok {
err := wc.CloseWrite()
fatalOnError(err)
} else {
log.Fatalln("conn does not implement WriteCloser interface")
}
tee = io.TeeReader(conn, os.Stdout)
timeoutCh = time.After(config.Timeout)
} else {
var r io.Reader
if len(args) == 0 {
r = os.Stdin
} else {
var readers []io.Reader
for _, filename := range args {
file, err := os.Open(filename)
fatalOnError(err)
defer file.Close()
readers = append(readers, file)
}
r = io.MultiReader(readers...)
}
tee = io.TeeReader(r, conn)
}
errCh := make(chan error)
go func() {
_, err := ioutil.ReadAll(tee)
errCh <- err
}()
select {
case err = <-errCh:
break
case <-timeoutCh:
err = errors.New("Timed out")
}
fatalOnError(err)
}
示例11: runRoutine
// Internal constructor which takes an additional argument for
// tee interfaces to OS standard in/out/err in addition to writing
// it to the struct
func runRoutine(r *RoutineRun, store bool, tee bool) error {
startRoutine(r)
// Construct the command to be executed
c, err := r.Command()
if err != nil {
endRoutine(r, err)
return err
}
// Buffers for subcommand interfaces. These are necessary for storing
// on to the routine run. Note, these may not actually be used below.
rstdin := new(bytes.Buffer)
rstdout := new(bytes.Buffer)
rstderr := new(bytes.Buffer)
if store && tee {
// Reads from os.Stdin and sends it to c.Stdin as well as writing
// it to the stdin buffer
c.Stdin = io.TeeReader(os.Stdin, rstdin)
// Writes the commands stdout to os.Stdout and the buffer
c.Stdout = io.MultiWriter(os.Stdout, rstdout)
c.Stderr = io.MultiWriter(os.Stderr, rstderr)
} else if store {
// stdin must always come from the OS
c.Stdin = io.TeeReader(os.Stdin, rstdin)
c.Stdout = rstdout
c.Stderr = rstderr
} else if tee {
c.Stdin = os.Stdin
c.Stdout = os.Stdout
c.Stderr = os.Stderr
}
// Run command
log.Printf("Running routine `%s`\n", r.CommandString())
err = c.Run()
if store {
// Set flags, store std interfaces
r.Stdin = rstdin.String()
r.Stdout = rstdout.String()
r.Stderr = rstderr.String()
}
endRoutine(r, err)
return err
}
示例12: ServeHTTP
func (sa solarAPIAccept) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Log.Info().Log("msg", r.Method, "url", r.URL, "header", fmt.Sprintf("%#v", r.Header))
if r.Body != nil {
defer r.Body.Close()
}
var buf bytes.Buffer
var data solarV1CurrentInverter
if err := json.NewDecoder(io.TeeReader(r.Body, &buf)).Decode(&data); err != nil {
Log.Error().Log("msg", "decode", "message", buf.String(), "error", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
Log.Debug().Log("msg", "decoded", "data", fmt.Sprintf("%#v", data))
w.WriteHeader(200)
if err := sa.influxClient.Put("fronius energy",
[]dataPoint{
{Name: "pac", Time: data.Head.Timestamp,
Unit: data.Body.Pac.Unit,
Value: data.Body.Pac.Values["1"]},
{Name: "day", Time: data.Head.Timestamp,
Unit: data.Body.Day.Unit,
Value: data.Body.Day.Values["1"]},
{Name: "year", Time: data.Head.Timestamp,
Unit: data.Body.Year.Unit,
Value: data.Body.Year.Values["1"]},
{Name: "total", Time: data.Head.Timestamp,
Unit: data.Body.Total.Unit,
Value: data.Body.Total.Values["1"]},
}...); err != nil {
Log.Error().Log("msg", "write batch to db", "error", err)
}
}
示例13: rawCall
func rawCall(token *oauth.Credentials, method string, uri string, opt map[string]string, res interface{}) error {
param := make(url.Values)
for k, v := range opt {
param.Set(k, v)
}
oauthClient.SignParam(token, method, uri, param)
var resp *http.Response
var err error
if method == "GET" {
uri = uri + "?" + param.Encode()
resp, err = http.Get(uri)
} else {
resp, err = http.PostForm(uri, url.Values(param))
}
if err != nil {
return err
}
defer resp.Body.Close()
if res == nil {
return nil
}
if *debug {
return json.NewDecoder(io.TeeReader(resp.Body, os.Stdout)).Decode(&res)
}
return json.NewDecoder(resp.Body).Decode(&res)
}
示例14: WriteACI
func (ms *conversionStore) WriteACI(path string) (string, error) {
f, err := os.Open(path)
if err != nil {
return "", err
}
defer f.Close()
cr, err := aci.NewCompressedReader(f)
if err != nil {
return "", err
}
defer cr.Close()
h := sha512.New()
r := io.TeeReader(cr, h)
// read the file so we can get the hash
if _, err := io.Copy(ioutil.Discard, r); err != nil {
return "", fmt.Errorf("error reading ACI: %v", err)
}
im, err := aci.ManifestFromImage(f)
if err != nil {
return "", err
}
key := ms.HashToKey(h)
ms.acis[key] = &aciInfo{path: path, key: key, ImageManifest: im}
return key, nil
}
示例15: NewFileMeta
// NewFileMeta generates a FileMeta object from the reader, using the
// hash algorithms provided
func NewFileMeta(r io.Reader, hashAlgorithms ...string) (FileMeta, error) {
if len(hashAlgorithms) == 0 {
hashAlgorithms = []string{defaultHashAlgorithm}
}
hashes := make(map[string]hash.Hash, len(hashAlgorithms))
for _, hashAlgorithm := range hashAlgorithms {
var h hash.Hash
switch hashAlgorithm {
case "sha256":
h = sha256.New()
case "sha512":
h = sha512.New()
default:
return FileMeta{}, fmt.Errorf("Unknown Hash Algorithm: %s", hashAlgorithm)
}
hashes[hashAlgorithm] = h
r = io.TeeReader(r, h)
}
n, err := io.Copy(ioutil.Discard, r)
if err != nil {
return FileMeta{}, err
}
m := FileMeta{Length: n, Hashes: make(Hashes, len(hashes))}
for hashAlgorithm, h := range hashes {
m.Hashes[hashAlgorithm] = h.Sum(nil)
}
return m, nil
}