本文整理汇总了Golang中io.ReadCloser函数的典型用法代码示例。如果您正苦于以下问题:Golang ReadCloser函数的具体用法?Golang ReadCloser怎么用?Golang ReadCloser使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ReadCloser函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: read
// read ignores the cache, and returns an io.ReadCloser representing the
// decompressed data for the given key, streamed from the disk. Clients should
// acquire a read lock on the Diskv and check the cache themselves before
// calling read.
func (d *Diskv) read(key string) (io.ReadCloser, error) {
filename := d.completeFilename(key)
fi, err := os.Stat(filename)
if err != nil {
return nil, err
}
if fi.IsDir() {
return nil, os.ErrNotExist
}
f, err := os.Open(filename)
if err != nil {
return nil, err
}
r := newSiphon(f, d, key)
var rc = io.ReadCloser(ioutil.NopCloser(r))
if d.Compression != nil {
rc, err = d.Compression.Reader(r)
if err != nil {
return nil, err
}
}
return rc, nil
}
示例2: readWithRLock
// read ignores the cache, and returns an io.ReadCloser representing the
// decompressed data for the given key, streamed from the disk. Clients should
// acquire a read lock on the Diskv and check the cache themselves before
// calling read.
func (d *Diskv) readWithRLock(key string) (io.ReadCloser, error) {
filename := d.completeFilename(key)
fi, err := os.Stat(filename)
if err != nil {
return nil, err
}
if fi.IsDir() {
return nil, os.ErrNotExist
}
f, err := os.Open(filename)
if err != nil {
return nil, err
}
var r io.Reader
if d.CacheSizeMax > 0 {
r = newSiphon(f, d, key)
} else {
r = &closingReader{f}
}
var rc = io.ReadCloser(ioutil.NopCloser(r))
if d.Compression != nil {
rc, err = d.Compression.Reader(r)
if err != nil {
return nil, err
}
}
return rc, nil
}
示例3: RsyncSend
// RsyncSend sets up the sending half of an rsync, to recursively send the
// directory pointed to by path over the websocket.
func RsyncSend(path string, conn *websocket.Conn, readWrapper func(io.ReadCloser) io.ReadCloser) error {
cmd, dataSocket, stderr, err := rsyncSendSetup(path)
if dataSocket != nil {
defer dataSocket.Close()
}
if err != nil {
return err
}
readPipe := io.ReadCloser(dataSocket)
if readWrapper != nil {
readPipe = readWrapper(dataSocket)
}
readDone, writeDone := shared.WebsocketMirror(conn, dataSocket, readPipe)
output, err := ioutil.ReadAll(stderr)
if err != nil {
shared.LogDebugf("problem reading rsync stderr %s", err)
}
err = cmd.Wait()
if err != nil {
shared.LogDebugf("problem with rsync send of %s: %s: %s", path, err, string(output))
}
<-readDone
<-writeDone
return err
}
示例4: Read
func (r *progressReader) Read(p []byte) (n int, err error) {
read, err := io.ReadCloser(r.reader).Read(p)
r.readProgress += read
updateEvery := 4096
if r.readTotal > 0 {
// Only update progress for every 1% read
if increment := int(0.01 * float64(r.readTotal)); increment > updateEvery {
updateEvery = increment
}
}
if r.readProgress-r.lastUpdate > updateEvery || err != nil {
if r.readTotal > 0 {
fmt.Fprintf(r.output, r.template, r.readProgress, r.readTotal, fmt.Sprintf("%.0f%%", float64(r.readProgress)/float64(r.readTotal)*100))
} else {
fmt.Fprintf(r.output, r.template, r.readProgress, "?", "n/a")
}
r.lastUpdate = r.readProgress
}
// Send newline when complete
if err != nil {
r.output.Write(r.sf.FormatStatus(""))
}
return read, err
}
示例5: makeReadCloser
// create a ReadCloser that reads from r and closes c.
func makeReadCloser(r io.Reader, c io.Closer) io.ReadCloser {
rc := struct {
io.Reader
io.Closer
}{r, c}
return io.ReadCloser(rc)
}
示例6: stubReader
func stubReader() io.ReadCloser {
file, err := os.Open("_support/notification.json")
if err != nil {
panic(err) // should never happen
}
return io.ReadCloser(file)
}
示例7: Load
// Load the JSON config file
func Load(configFile string, p Parser) {
var err error
var input = io.ReadCloser(os.Stdin)
if input, err = os.Open(configFile); err != nil {
log.Fatalln(err)
}
// Read the config file
jsonBytes, err := ioutil.ReadAll(input)
input.Close()
if err != nil {
log.Fatalln(err)
}
// Parse the config
if err := p.ParseJSON(jsonBytes); err != nil {
log.Fatalln("Could not parse %q: %v", configFile, err)
}
}
示例8: send
func (s *zfsMigrationSourceDriver) send(conn *websocket.Conn, zfsName string, zfsParent string, readWrapper func(io.ReadCloser) io.ReadCloser) error {
fields := strings.SplitN(s.container.Name(), shared.SnapshotDelimiter, 2)
args := []string{"send", fmt.Sprintf("%s/containers/%[email protected]%s", s.zfs.zfsPool, fields[0], zfsName)}
if zfsParent != "" {
args = append(args, "-i", fmt.Sprintf("%s/containers/%[email protected]%s", s.zfs.zfsPool, s.container.Name(), zfsParent))
}
cmd := exec.Command("zfs", args...)
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
readPipe := io.ReadCloser(stdout)
if readWrapper != nil {
readPipe = readWrapper(stdout)
}
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
if err := cmd.Start(); err != nil {
return err
}
<-shared.WebsocketSendStream(conn, readPipe, 4*1024*1024)
output, err := ioutil.ReadAll(stderr)
if err != nil {
shared.LogError("problem reading zfs send stderr", log.Ctx{"err": err})
}
err = cmd.Wait()
if err != nil {
shared.LogError("problem with zfs send", log.Ctx{"output": string(output)})
}
return err
}
示例9: Read
func (r *progressReader) Read(p []byte) (n int, err error) {
read, err := io.ReadCloser(r.reader).Read(p)
r.readProgress += read
// Only update progress for every 1% read
updateEvery := int(0.01 * float64(r.readTotal))
if r.readProgress-r.lastUpdate > updateEvery || r.readProgress == r.readTotal {
fmt.Fprintf(r.output, "%d/%d (%.0f%%)\r",
r.readProgress,
r.readTotal,
float64(r.readProgress)/float64(r.readTotal)*100)
r.lastUpdate = r.readProgress
}
// Send newline when complete
if err == io.EOF {
fmt.Fprintf(r.output, "\n")
}
return read, err
}
示例10: Read
func (r *progressReader) Read(p []byte) (n int, err error) {
read, err := io.ReadCloser(r.reader).Read(p)
r.progress.Current += read
updateEvery := 1024 * 512 //512kB
if r.progress.Total > 0 {
// Update progress for every 1% read if 1% < 512kB
if increment := int(0.01 * float64(r.progress.Total)); increment < updateEvery {
updateEvery = increment
}
}
if r.progress.Current-r.lastUpdate > updateEvery || err != nil {
r.output.Write(r.sf.FormatProgress(r.ID, r.action, &r.progress))
r.lastUpdate = r.progress.Current
}
// Send newline when complete
if r.newLine && err != nil {
r.output.Write(r.sf.FormatStatus("", ""))
}
return read, err
}
示例11: Read
func (r *progressReader) Read(p []byte) (n int, err error) {
read, err := io.ReadCloser(r.reader).Read(p)
r.readProgress += read
updateEvery := 1024 * 512 //512kB
if r.readTotal > 0 {
// Update progress for every 1% read if 1% < 512kB
if increment := int(0.01 * float64(r.readTotal)); increment < updateEvery {
updateEvery = increment
}
}
if r.readProgress-r.lastUpdate > updateEvery || err != nil {
if r.readTotal > 0 {
fmt.Fprintf(r.output, r.template, HumanSize(int64(r.readProgress)), HumanSize(int64(r.readTotal)), fmt.Sprintf("%.0f%%", float64(r.readProgress)/float64(r.readTotal)*100))
} else {
fmt.Fprintf(r.output, r.template, r.readProgress, "?", "n/a")
}
r.lastUpdate = r.readProgress
}
return read, err
}
示例12: UntarFile
//UntarFile extracts archived file.
func UntarFile(file *os.File, destinationDir string) error {
isGzipped, err := IsGzip(file)
if err != nil {
return err
}
fileReader := io.ReadCloser(file)
if isGzipped {
fileReader, err = gzip.NewReader(file)
if err != nil {
return err
}
defer fileReader.Close()
}
err = UntarStream(fileReader, destinationDir)
if err != nil {
return err
}
return nil
}
示例13: send
func (s *btrfsMigrationSourceDriver) send(conn *websocket.Conn, btrfsPath string, btrfsParent string, readWrapper func(io.ReadCloser) io.ReadCloser) error {
args := []string{"send", btrfsPath}
if btrfsParent != "" {
args = append(args, "-p", btrfsParent)
}
cmd := exec.Command("btrfs", args...)
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
readPipe := io.ReadCloser(stdout)
if readWrapper != nil {
readPipe = readWrapper(stdout)
}
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
if err := cmd.Start(); err != nil {
return err
}
<-shared.WebsocketSendStream(conn, readPipe, 4*1024*1024)
output, err := ioutil.ReadAll(stderr)
if err != nil {
shared.LogError("problem reading btrfs send stderr", log.Ctx{"err": err})
}
err = cmd.Wait()
if err != nil {
shared.LogError("problem with btrfs send", log.Ctx{"output": string(output)})
}
return err
}
示例14: getConfigJSON
//Return the json content of files
func (c *Config) getConfigJSON(configFolder string, configFile string) ([]byte, error) {
var err error
var input = io.ReadCloser(os.Stdin)
configPath, err := filepath.Abs(configFolder + "/" + configFile + ".json")
if err != nil {
log.Fatalln("Could not parse %q: %v", configPath, err)
}
if input, err = os.Open(configPath); err != nil {
log.Fatalln(err)
}
// Read the config file
jsonBytes, err := ioutil.ReadAll(input)
input.Close()
if err != nil {
log.Fatalln(err)
}
return jsonBytes, err
}
示例15: main
func main() {
flagExample := flag.Bool("example", false, "just dump out an example versioninfo.json to stdout")
flagOut := flag.String("o", "resource.syso", "output file name")
flagIcon := flag.String("icon", "", "icon file name")
flagComment := flag.String("comment", "", "StringFileInfo.Comments")
flagCompany := flag.String("company", "", "StringFileInfo.CompanyName")
flagDescription := flag.String("description", "", "StringFileInfo.FileDescription")
flagFileVersion := flag.String("file-version", "", "StringFileInfo.FileVersion")
flagInternalName := flag.String("internal-name", "", "StringFileInfo.InternalName")
flagCopyright := flag.String("copyright", "", "StringFileInfo.LegalCopyright")
flagTrademark := flag.String("trademark", "", "StringFileInfo.LegalTrademarks")
flagOriginalName := flag.String("original-name", "", "StringFileInfo.OriginalFilename")
flagPrivateBuild := flag.String("private-build", "", "StringFileInfo.PrivateBuild")
flagProductName := flag.String("product-name", "", "StringFileInfo.ProductName")
flagProductVersion := flag.String("product-version", "", "StringFileInfo.ProductVersion")
flagSpecialBuild := flag.String("special-build", "", "StringFileInfo.SpecialBuild")
flagTranslation := flag.Int("translation", 0, "translation ID")
flagCharset := flag.Int("charset", 0, "charset ID")
flagVerMajor := flag.Int("ver-major", -1, "FileVersion.Major")
flagVerMinor := flag.Int("ver-minor", -1, "FileVersion.Minor")
flagVerPatch := flag.Int("ver-patch", -1, "FileVersion.Patch")
flagVerBuild := flag.Int("ver-build", -1, "FileVersion.Build")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [flags] <versioninfo.json>\n\nPossible flags:\n", os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
if *flagExample {
io.WriteString(os.Stdout, example)
return
}
configFile := flag.Arg(0)
if configFile == "" {
configFile = "versioninfo.json"
}
var err error
var input = io.ReadCloser(os.Stdin)
if configFile != "-" {
if input, err = os.Open(configFile); err != nil {
log.Printf("Cannot open %q: %v", configFile, err)
os.Exit(1)
}
}
// Read the config file
jsonBytes, err := ioutil.ReadAll(input)
input.Close()
if err != nil {
log.Printf("Error reading %q: %v", configFile, err)
os.Exit(1)
}
// Create a new container
vi := &goversioninfo.VersionInfo{}
// Parse the config
if err := vi.ParseJSON(jsonBytes); err != nil {
log.Printf("Could not parse the .json file: %v", err)
os.Exit(2)
}
// Override from flags
if *flagIcon != "" {
vi.IconPath = *flagIcon
}
if *flagComment != "" {
vi.StringFileInfo.Comments = *flagComment
}
if *flagCompany != "" {
vi.StringFileInfo.CompanyName = *flagCompany
}
if *flagDescription != "" {
vi.StringFileInfo.FileDescription = *flagDescription
}
if *flagFileVersion != "" {
vi.StringFileInfo.FileVersion = *flagFileVersion
}
if *flagInternalName != "" {
vi.StringFileInfo.InternalName = *flagInternalName
}
if *flagCopyright != "" {
vi.StringFileInfo.LegalCopyright = *flagCopyright
}
if *flagTrademark != "" {
vi.StringFileInfo.LegalTrademarks = *flagTrademark
}
if *flagOriginalName != "" {
vi.StringFileInfo.OriginalFilename = *flagOriginalName
}
if *flagPrivateBuild != "" {
vi.StringFileInfo.PrivateBuild = *flagPrivateBuild
}
if *flagProductName != "" {
vi.StringFileInfo.ProductName = *flagProductName
}
//.........这里部分代码省略.........