当前位置: 首页>>代码示例>>Golang>>正文


Golang io.MultiReader函数代码示例

本文整理汇总了Golang中io.MultiReader函数的典型用法代码示例。如果您正苦于以下问题:Golang MultiReader函数的具体用法?Golang MultiReader怎么用?Golang MultiReader使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了MultiReader函数的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
}
开发者ID:rfistman,项目名称:camlistore,代码行数:66,代码来源:images.go

示例2: prepend

func prepend(dst string) (err error) {
	tmp, err := ioutil.TempFile(filepath.Split(dst))
	if err != nil {
		return err
	}
	rdst, err := os.Open(dst)
	if err != nil {
		return nonil(err, tmp.Close(), os.Remove(tmp.Name()))
	}
	var errCleanup error
	defer func() {
		switch errCleanup {
		case nil:
			if err = nonil(tmp.Close(), rdst.Close()); err != nil {
				os.Remove(tmp.Name())
			}
			// os.Rename fails under Windows if destination file exists.
			if err = os.Remove(dst); err != nil {
				os.Remove(tmp.Name())
			}
			if err = os.Rename(tmp.Name(), dst); err != nil {
				err = errors.New(err.Error() + " (prepended content is safe under " + tmp.Name() + ")")
			}
		default:
			nonil(tmp.Close(), rdst.Close(), os.Remove(tmp.Name()))
			if errCleanup != errNop {
				err = errCleanup
			}
		}
	}()
	var r io.Reader
	fi, err := os.Stdin.Stat()
	if err != nil {
		errCleanup = err
		return
	}
	switch {
	case src != "":
		f, err := os.Open(src)
		if err != nil {
			errCleanup = err
			return err
		}
		defer f.Close()
		r = f
	case fi.Mode()&os.ModeCharDevice == 0: // stackoverflow.com/questions/22744443
		r = io.MultiReader(bytes.NewReader(stdin.Bytes()), io.TeeReader(os.Stdin, &stdin))
	default:
		errCleanup = errNop
	}
	if unique {
		r = multiunique(nop(r), rdst)
	} else {
		r = io.MultiReader(nop(r), rdst)
	}
	_, errCleanup = io.Copy(tmp, r)
	return err
}
开发者ID:rjeczalik,项目名称:cmd,代码行数:58,代码来源:main.go

示例3: MIMETypeFromReader

// MIMETypeFromReader takes a reader, sniffs the beginning of it,
// and returns the mime (if sniffed, else "") and a new reader
// that's the concatenation of the bytes sniffed and the remaining
// reader.
func MIMETypeFromReader(r io.Reader) (mime string, reader io.Reader) {
	var buf bytes.Buffer
	_, err := io.Copy(&buf, io.LimitReader(r, 1024))
	mime = MIMEType(buf.Bytes())
	if err != nil {
		return mime, io.MultiReader(&buf, errReader{err})
	}
	return mime, io.MultiReader(&buf, r)
}
开发者ID:camlistore,项目名称:camlistore,代码行数:13,代码来源:magic.go

示例4: encrypter

// encrypter returns the encrypted reader passed on the keys and IV provided.
func encrypter(r io.Reader, aesKey, hmacKey, iv, header []byte) (io.Reader, error) {
	b, err := aes.NewCipher(aesKey)
	if err != nil {
		return nil, err
	}
	h := hmac.New(hashFunc, hmacKey)
	hr := &hashReadWriter{hash: h}
	sr := &cipher.StreamReader{R: r, S: cipher.NewCTR(b, iv)}
	return io.MultiReader(io.TeeReader(io.MultiReader(bytes.NewReader(header), sr), hr), hr), nil
}
开发者ID:nawawi,项目名称:drive,代码行数:11,代码来源:v1.go

示例5: gotree

func gotree(root string, printroot bool, spy memfs.FS, w io.Writer) (err error) {
	var (
		r      io.Reader
		pr, pw = io.Pipe()
		ch     = make(chan error, 1)
		ndir   int
		nfile  int
		fn     filepath.WalkFunc
	)
	if dir {
		fn = countdirdelfile(&ndir, spy)
	} else {
		fn = countdirfile(&ndir, &nfile)
	}
	if err = spy.Walk(string(os.PathSeparator), fn); err != nil {
		return
	}
	go func() {
		ch <- nonnil(memfs.Unix.Encode(spy, pw), pw.Close())
	}()
	switch {
	case dir && printroot:
		r = io.MultiReader(
			strings.NewReader(fmt.Sprintf("%s%c", root, os.PathSeparator)),
			pr,
			strings.NewReader(fmt.Sprintf("\n%d directories\n", ndir-1)),
		)
	case dir:
		r = io.MultiReader(
			pr,
			strings.NewReader(fmt.Sprintf("\n%d directories\n", ndir-1)),
		)
	case printroot:
		r = io.MultiReader(
			strings.NewReader(fmt.Sprintf("%s%c", root, os.PathSeparator)),
			pr,
			strings.NewReader(fmt.Sprintf("\n%d directories, %d files\n", ndir-1, nfile)),
		)
	default:
		r = io.MultiReader(
			pr,
			strings.NewReader(fmt.Sprintf("\n%d directories, %d files\n", ndir-1, nfile)),
		)
	}
	_, err = io.Copy(w, r)
	if e := <-ch; e != nil && err == nil {
		err = e
	}
	return
}
开发者ID:rjeczalik,项目名称:fs,代码行数:50,代码来源:main.go

示例6: tarUntar

func tarUntar(t *testing.T, origin string, options *TarOptions) ([]Change, error) {
	archive, err := TarWithOptions(origin, options)
	if err != nil {
		t.Fatal(err)
	}
	defer archive.Close()

	buf := make([]byte, 10)
	if _, err := archive.Read(buf); err != nil {
		return nil, err
	}
	wrap := io.MultiReader(bytes.NewReader(buf), archive)

	detectedCompression := DetectCompression(buf)
	compression := options.Compression
	if detectedCompression.Extension() != compression.Extension() {
		return nil, fmt.Errorf("Wrong compression detected. Actual compression: %s, found %s", compression.Extension(), detectedCompression.Extension())
	}

	tmp, err := ioutil.TempDir("", "docker-test-untar")
	if err != nil {
		return nil, err
	}
	defer os.RemoveAll(tmp)
	if err := Untar(wrap, tmp, nil); err != nil {
		return nil, err
	}
	if _, err := os.Stat(tmp); err != nil {
		return nil, err
	}

	return ChangesDirs(origin, tmp)
}
开发者ID:truedays,项目名称:docker,代码行数:33,代码来源:archive_test.go

示例7: Join

// Join the shards and write the data segment to dst.
//
// Only the data shards are considered.
//
// You must supply the exact output size you want.
// If there are to few shards given, ErrTooFewShards will be returned.
// If the total data size is less than outSize, ErrShortData will be returned.
func (r rsStream) Join(dst io.Writer, shards []io.Reader, outSize int64) error {
	// Do we have enough shards?
	if len(shards) < r.r.DataShards {
		return ErrTooFewShards
	}

	// Trim off parity shards if any
	shards = shards[:r.r.DataShards]
	for i := range shards {
		if shards[i] == nil {
			return StreamReadError{Err: ErrShardNoData, Stream: i}
		}
	}
	// Join all shards
	src := io.MultiReader(shards...)

	// Copy data to dst
	n, err := io.CopyN(dst, src, outSize)
	if err == io.EOF {
		return ErrShortData
	}
	if err != nil {
		return err
	}
	if n != outSize {
		return ErrShortData
	}
	return nil
}
开发者ID:xushiwei,项目名称:reedsolomon,代码行数:36,代码来源:streaming.go

示例8: smudgeCommand

func smudgeCommand(cmd *cobra.Command, args []string) {
	requireStdin("This command should be run by the Git 'smudge' filter")
	lfs.InstallHooks(false)

	// keeps the initial buffer from lfs.DecodePointer
	b := &bytes.Buffer{}
	r := io.TeeReader(os.Stdin, b)

	ptr, err := lfs.DecodePointer(r)
	if err != nil {
		mr := io.MultiReader(b, os.Stdin)
		_, err := io.Copy(os.Stdout, mr)
		if err != nil {
			Panic(err, "Error writing data to stdout:")
		}
		return
	}

	if smudgeInfo {
		localPath, err := lfs.LocalMediaPath(ptr.Oid)
		if err != nil {
			Exit(err.Error())
		}

		stat, err := os.Stat(localPath)
		if err != nil {
			Print("%d --", ptr.Size)
		} else {
			Print("%d %s", stat.Size(), localPath)
		}
		return
	}

	filename := smudgeFilename(args, err)
	cb, file, err := lfs.CopyCallbackFile("smudge", filename, 1, 1)
	if err != nil {
		Error(err.Error())
	}

	cfg := lfs.Config
	download := lfs.FilenamePassesIncludeExcludeFilter(filename, cfg.FetchIncludePaths(), cfg.FetchExcludePaths())

	if smudgeSkip || lfs.Config.GetenvBool("GIT_LFS_SKIP_SMUDGE", false) {
		download = false
	}

	err = ptr.Smudge(os.Stdout, filename, download, cb)
	if file != nil {
		file.Close()
	}

	if err != nil {
		ptr.Encode(os.Stdout)
		// Download declined error is ok to skip if we weren't requesting download
		if !(lfs.IsDownloadDeclinedError(err) && !download) {
			LoggedError(err, "Error accessing media: %s (%s)", filename, ptr.Oid)
			os.Exit(2)
		}
	}
}
开发者ID:ambf1436,项目名称:git-lfs,代码行数:60,代码来源:command_smudge.go

示例9: Stream

// Stream returns one channel that combines the stdout and stderr of the command
// as it is run on the remote machine, and another that sends true when the
// command is done. The sessions and channels will then be closed.
func (ssh_conf *ClientSSH) Stream(command string) (output chan string, done chan bool, err error) {
	// connect to remote host
	session, err := ssh_conf.connect()
	if err != nil {
		return output, done, err
	}
	// connect to both outputs (they are of type io.Reader)
	outReader, err := session.StdoutPipe()
	if err != nil {
		return output, done, err
	}
	errReader, err := session.StderrPipe()
	if err != nil {
		return output, done, err
	}
	// combine outputs, create a line-by-line scanner
	outputReader := io.MultiReader(outReader, errReader)
	err = session.Start(command)
	scanner := bufio.NewScanner(outputReader)
	// continuously send the command's output over the channel
	outputChan := make(chan string)
	done = make(chan bool)
	go func(scanner *bufio.Scanner, out chan string, done chan bool) {
		defer close(outputChan)
		defer close(done)
		for scanner.Scan() {
			outputChan <- scanner.Text()
		}
		// close all of our open resources
		done <- true
		session.Close()
	}(scanner, outputChan, done)
	return outputChan, done, err
}
开发者ID:rootless4real,项目名称:musicsaur,代码行数:37,代码来源:ssh.go

示例10: Run

func (s *Command) Run() error {
	if s.client == nil {
		return errors.New("Not connected")
	}

	session, err := s.client.NewSession()
	if err != nil {
		return err
	}

	var envVariables bytes.Buffer
	for _, keyValue := range s.Environment {
		envVariables.WriteString("export " + strconv.Quote(keyValue) + "\n")
	}

	session.Stdin = io.MultiReader(
		&envVariables,
		bytes.NewBuffer(s.Stdin),
	)
	session.Stdout = s.Stdout
	session.Stderr = s.Stderr
	err = session.Run(s.Command)
	session.Close()
	return err
}
开发者ID:mxq0923,项目名称:gitlab-ci-multi-runner,代码行数:25,代码来源:ssh_command.go

示例11: tunnel

func tunnel(conn net.Conn) {
	defer conn.Close()

	// We start with a JSON header, currenly only has the dest addr.
	hdrdec := json.NewDecoder(conn)
	var hdr header
	err := hdrdec.Decode(&hdr)
	if err != nil {
		log.Printf("Couldn't parse tunnelled connection header: %v", err)
		return
	}

	destc, err := net.Dial("tcp", hdr.Destaddr)
	if err != nil {
		log.Printf("Couldn't dial destination $v: %v", hdr.Destaddr, err)
		return
	}
	defer destc.Close()

	log.Printf("Now tunnelling %v to %v", conn.RemoteAddr(), destc.RemoteAddr())
	done := make(chan struct{})
	go func() {
		io.Copy(destc, io.MultiReader(hdrdec.Buffered(), conn))
		done <- struct{}{}
	}()
	io.Copy(conn, destc)
	<-done
}
开发者ID:saljam,项目名称:dnsproxy,代码行数:28,代码来源:main.go

示例12: copyToTemp

func copyToTemp(reader io.Reader, fileSize int64, cb progress.CopyCallback) (oid string, size int64, tmp *os.File, err error) {
	tmp, err = TempFile("")
	if err != nil {
		return
	}

	defer tmp.Close()

	oidHash := sha256.New()
	writer := io.MultiWriter(oidHash, tmp)

	if fileSize == 0 {
		cb = nil
	}

	by, ptr, err := DecodeFrom(reader)
	if err == nil && len(by) < 512 {
		err = errutil.NewCleanPointerError(err, ptr, by)
		return
	}

	multi := io.MultiReader(bytes.NewReader(by), reader)
	size, err = tools.CopyWithCallback(writer, multi, fileSize, cb)

	if err != nil {
		return
	}

	oid = hex.EncodeToString(oidHash.Sum(nil))
	return
}
开发者ID:tianguanghui,项目名称:git-lfs,代码行数:31,代码来源:pointer_clean.go

示例13: Reader

// Returns an io.Reader that yields the section name in square brackets as the
// first line. Subsequent lines are the underlying Entry structs serialized in
// order, each indented by a single horizontal tab rune '\t' and each separated
// by a new line rune '\n'.
func (section *Section) Reader() io.Reader {
	offset := 3
	readers := make([]io.Reader, len(section.Dict)*3+offset)
	readers[0] = bytes.NewReader([]byte{'['})
	readers[1] = strings.NewReader(section.Name)
	readers[2] = bytes.NewReader([]byte{']', '\n'})

	keys := make([]string, len(section.Dict))
	{
		i := 0
		for k, _ := range section.Dict {
			keys[i] = k
			i++
		}
	}
	sort.Strings(keys)

	for i, k := range keys {
		v := section.Dict[k]
		readers[i*3+offset+0] = bytes.NewReader([]byte{'\t'})
		readers[i*3+offset+1] = (&Entry{k, v}).Reader()
		readers[i*3+offset+2] = bytes.NewReader([]byte{'\n'})
	}

	return io.MultiReader(readers...)
}
开发者ID:kourge,项目名称:ggit,代码行数:30,代码来源:section.go

示例14: TeeRead

//TeeRead writes the data from the reader into the writer, and returns a reader
func TeeRead(w io.Writer, r io.Reader, maxMemory int64) (io.ReadCloser, error) {
	b := bytes.NewBuffer(nil)
	if maxMemory <= 0 {
		maxMemory = 1 << 20 // 1Mb
	}
	size, err := io.CopyN(io.MultiWriter(w, b), r, maxMemory+1)
	if err != nil && err != io.EOF {
		return nil, err
	}
	if size <= maxMemory {
		return ioutil.NopCloser(bytes.NewReader(b.Bytes())), nil
	}
	// too big, write to disk and flush buffer
	file, err := ioutil.TempFile("", "reader-")
	if err != nil {
		return nil, err
	}
	nm := file.Name()
	size, err = io.Copy(io.MultiWriter(w, file), io.MultiReader(b, r))
	if err != nil {
		file.Close()
		os.Remove(nm)
		return nil, err
	}
	file.Close()
	fh, err := os.Open(nm)
	return tempFile{File: fh}, err
}
开发者ID:daozhao,项目名称:s3weed,代码行数:29,代码来源:temp.go

示例15: NewReadSeeker

// NewReadSeeker returns a copy of the r io.Reader which can be Seeken and closed.
func NewReadSeeker(r io.Reader, maxMemory int64) (ReadSeekCloser, error) {
	b := bytes.NewBuffer(nil)
	if maxMemory <= 0 {
		maxMemory = 1 << 20 // 1Mb
	}
	size, err := io.CopyN(b, r, maxMemory+1)
	if err != nil && err != io.EOF {
		return nil, err
	}
	if size <= maxMemory {
		return &tempBuf{bytes.NewReader(b.Bytes())}, nil
	}
	// too big, write to disk and flush buffer
	file, err := ioutil.TempFile("", "reader-")
	if err != nil {
		return nil, err
	}
	nm := file.Name()
	size, err = io.Copy(file, io.MultiReader(b, r))
	if err != nil {
		file.Close()
		os.Remove(nm)
		return nil, err
	}
	file.Close()
	fh, err := os.Open(nm)
	return tempFile{File: fh}, err
}
开发者ID:daozhao,项目名称:s3weed,代码行数:29,代码来源:temp.go


注:本文中的io.MultiReader函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。