本文整理匯總了Golang中io.ReadCloser類的典型用法代碼示例。如果您正苦於以下問題:Golang ReadCloser類的具體用法?Golang ReadCloser怎麽用?Golang ReadCloser使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了ReadCloser類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: collectToOutputChannel
func collectToOutputChannel(name string, in io.ReadCloser, ch chan<- commandOutput) {
go func() {
buf, err := ioutil.ReadAll(in)
ch <- commandOutput{name, buf, err}
in.Close()
}()
}
示例2: CopyBundleExcept
// CopyBundleExcept copies all the blobs in the bundle src, except for those in
// the list, into the current place in the bundle writer.
func (bw *BundleWriter) CopyBundleExcept(src int, except []BlobID) error {
r, err := OpenBundle(bw.store, sugar(bw.item.ID, src))
if err != nil {
return err
}
defer r.Close()
var badnames = make([]string, 1+len(except))
badnames[0] = "item-info.json"
for i, id := range except {
badnames[i+1] = fmt.Sprintf("blob/%d", id)
}
for _, fname := range r.Files() {
if contains(badnames, fname) {
continue
}
var rc io.ReadCloser
rc, err = r.Open(fname)
if err != nil {
return err
}
// TODO(dbrower): check for errors
blob := bw.item.blobByID(extractBlobID(fname))
err = bw.WriteBlob(blob, rc)
rc.Close()
if err != nil {
return err
}
}
return nil
}
示例3: readPayload
// readPayload reads the HTTP response in chunks, making the read buffer available
// to MeekConn.Read() calls after each chunk; the intention is to allow bytes to
// flow back to the reader as soon as possible instead of buffering the entire payload.
func (meek *MeekConn) readPayload(receivedPayload io.ReadCloser) (totalSize int64, err error) {
defer receivedPayload.Close()
totalSize = 0
for {
reader := io.LimitReader(receivedPayload, READ_PAYLOAD_CHUNK_LENGTH)
// Block until there is capacity in the receive buffer
var receiveBuffer *bytes.Buffer
select {
case receiveBuffer = <-meek.emptyReceiveBuffer:
case receiveBuffer = <-meek.partialReceiveBuffer:
case <-meek.broadcastClosed:
return 0, nil
}
// Note: receiveBuffer size may exceed FULL_RECEIVE_BUFFER_LENGTH by up to the size
// of one received payload. The FULL_RECEIVE_BUFFER_LENGTH value is just a threshold.
n, err := receiveBuffer.ReadFrom(reader)
meek.replaceReceiveBuffer(receiveBuffer)
if err != nil {
return 0, ContextError(err)
}
totalSize += n
if n == 0 {
break
}
}
return totalSize, nil
}
示例4: Display
func (omxHandler) Display(ctx *context.T, mimetype string, r io.ReadCloser) (func(), error) {
defer r.Close()
tmp, err := ioutil.TempFile("", "")
if err != nil {
return nil, err
}
if _, err := io.Copy(tmp, r); err != nil {
os.Remove(tmp.Name())
return nil, err
}
tmp.Close()
args := []string{
"-b",
tmp.Name(),
}
vlog.Infof("Running: omxplayer %s", strings.Join(args, " "))
cmd := exec.Command("omxplayer", args...)
cmd.Stdin = r
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
return nil, err
}
return func() {
if err := cmd.Process.Kill(); err != nil {
vlog.Errorf("Could not kill omx: %v", err)
}
cmd.Wait()
os.Remove(tmp.Name())
}, nil
}
示例5: loadWav
// loadWav reads a valid wave file into a header and a bunch audio data into bytes.
// Invalid files return a nil header and an empty data slice.
// FUTURE: Handle the info block.
func (l *loader) loadWav(file io.ReadCloser) (wh *WavHdr, bytes []byte, err error) {
wh = &WavHdr{}
if err = binary.Read(file, binary.LittleEndian, wh); err != nil {
return nil, []byte{}, fmt.Errorf("Invalid .wav audio file: %s", err)
}
// check that it really is a WAVE file.
riff, wave := string(wh.RiffId[:]), string(wh.WaveId[:])
if riff != "RIFF" || wave != "WAVE" {
return nil, []byte{}, fmt.Errorf("Invalid .wav audio file")
}
// read the audio data.
bytesRead := uint32(0)
data := []byte{}
inbuff := make([]byte, wh.DataSize)
for bytesRead < wh.DataSize {
inbytes, readErr := file.Read(inbuff)
if readErr != nil {
return nil, []byte{}, fmt.Errorf("Corrupt .wav audio file")
}
data = append(data, inbuff...)
bytesRead += uint32(inbytes)
}
if bytesRead != wh.DataSize {
return nil, []byte{}, fmt.Errorf("Invalid .wav audio file %d %d", bytesRead, wh.DataSize)
}
return wh, data, nil
}
示例6: followFile
// followFile outputs the contents of the file to stdout relative to the end of
// the file.
func (l *LogsCommand) followFile(client *api.Client, alloc *api.Allocation,
follow bool, task, logType, origin string, offset int64) (io.ReadCloser, error) {
cancel := make(chan struct{})
frames, err := client.AllocFS().Logs(alloc, follow, task, logType, origin, offset, cancel, nil)
if err != nil {
return nil, err
}
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
// Create a reader
var r io.ReadCloser
frameReader := api.NewFrameReader(frames, cancel)
frameReader.SetUnblockTime(500 * time.Millisecond)
r = frameReader
go func() {
<-signalCh
// End the streaming
r.Close()
}()
return r, nil
}
示例7: doRequest
func (c *ArchiveInsertCall) doRequest(alt string) (*http.Response, error) {
var body io.Reader = nil
c.urlParams_.Set("alt", alt)
urls := googleapi.ResolveRelative(c.s.BasePath, "{groupId}/archive")
if c.media_ != nil || c.resumableBuffer_ != nil {
urls = strings.Replace(urls, "https://www.googleapis.com/", "https://www.googleapis.com/upload/", 1)
protocol := "multipart"
if c.resumableBuffer_ != nil {
protocol = "resumable"
}
c.urlParams_.Set("uploadType", protocol)
}
urls += "?" + c.urlParams_.Encode()
body = new(bytes.Buffer)
ctype := "application/json"
if c.media_ != nil {
var combined io.ReadCloser
combined, ctype = gensupport.CombineBodyMedia(body, ctype, c.media_, c.mediaType_)
defer combined.Close()
body = combined
}
req, _ := http.NewRequest("POST", urls, body)
googleapi.Expand(req.URL, map[string]string{
"groupId": c.groupId,
})
if c.resumableBuffer_ != nil {
req.Header.Set("X-Upload-Content-Type", c.mediaType_)
}
req.Header.Set("Content-Type", ctype)
req.Header.Set("User-Agent", c.s.userAgent())
if c.ctx_ != nil {
return ctxhttp.Do(c.ctx_, c.s.client, req)
}
return c.s.client.Do(req)
}
示例8: startPty
func (container *Container) startPty() error {
stdoutMaster, stdoutSlave, err := pty.Open()
if err != nil {
return err
}
container.ptyStdoutMaster = stdoutMaster
container.cmd.Stdout = stdoutSlave
stderrMaster, stderrSlave, err := pty.Open()
if err != nil {
return err
}
container.ptyStderrMaster = stderrMaster
container.cmd.Stderr = stderrSlave
// Copy the PTYs to our broadcasters
go func() {
defer container.stdout.CloseWriters()
Debugf("[startPty] Begin of stdout pipe")
io.Copy(container.stdout, stdoutMaster)
Debugf("[startPty] End of stdout pipe")
}()
go func() {
defer container.stderr.CloseWriters()
Debugf("[startPty] Begin of stderr pipe")
io.Copy(container.stderr, stderrMaster)
Debugf("[startPty] End of stderr pipe")
}()
// stdin
var stdinSlave io.ReadCloser
if container.Config.OpenStdin {
var stdinMaster io.WriteCloser
stdinMaster, stdinSlave, err = pty.Open()
if err != nil {
return err
}
container.ptyStdinMaster = stdinMaster
container.cmd.Stdin = stdinSlave
// FIXME: The following appears to be broken.
// "cannot set terminal process group (-1): Inappropriate ioctl for device"
// container.cmd.SysProcAttr = &syscall.SysProcAttr{Setctty: true, Setsid: true}
go func() {
defer container.stdin.Close()
Debugf("[startPty] Begin of stdin pipe")
io.Copy(stdinMaster, container.stdin)
Debugf("[startPty] End of stdin pipe")
}()
}
if err := container.cmd.Start(); err != nil {
return err
}
stdoutSlave.Close()
stderrSlave.Close()
if stdinSlave != nil {
stdinSlave.Close()
}
return nil
}
示例9: UntarOne
// UntarOne writes the contents up a single file to dst
func UntarOne(name string, dst io.Writer, src io.ReadCloser) error {
// ungzipped, err := gzip.NewReader(src)
// if err != nil {
// return err
// }
tarball := tar.NewReader(src)
defer src.Close()
// defer tarball.Close()
for {
hdr, err := tarball.Next()
if err == io.EOF {
// finished the tar
break
}
if err != nil {
return err
}
if hdr.Name != name {
continue
}
// We found the file we care about
_, err = io.Copy(dst, tarball)
break
}
return nil
}
示例10: DownloadDiffs
// DownloadDiffs downloads a filename by differences into destfile
func DownloadDiffs(destfile string, diffs *Diffs) (downloaded int64, hash string, err error) {
file, err := os.OpenFile(destfile, os.O_CREATE|os.O_WRONLY, 0750) // For write access
if err != nil {
return
}
defer file.Close()
h := NewHasher()
var source io.ReadCloser
sink := io.MultiWriter(file, h)
localHnd := &LocalHashNDump{"."}
remoteHnd := &RemoteHashNDump{diffs.Server}
done := int64(0)
for _, diff := range diffs.Diffs {
if diff.Different {
source, _, err = remoteHnd.Dump(diffs.Filename, diff.Offset, diff.Size)
} else {
source, _, err = localHnd.Dump(diffs.Alike, diff.Offset, diff.Size)
}
if err != nil {
return downloaded, "", err
}
n, err := io.CopyN(sink, source, diff.Size)
source.Close()
if err != nil {
return downloaded, "", err
}
if n != diff.Size {
return downloaded, "", fmt.Errorf("Expected to copy %v but copied %v instead!", diff.Size, n)
}
downloaded += n
done += n
}
return downloaded, fmt.Sprintf("%x", h.Sum(nil)), nil
}
示例11: readJSONStream
func (client *DockerClient) readJSONStream(stream io.ReadCloser, decode func(*json.Decoder) decodingResult, stopChan <-chan struct{}) <-chan decodingResult {
resultChan := make(chan decodingResult)
go func() {
decoder := json.NewDecoder(stream)
stopped := make(chan struct{})
go func() {
<-stopChan
stream.Close()
stopped <- struct{}{}
}()
defer close(resultChan)
for {
decodeResult := decode(decoder)
select {
case <-stopped:
return
default:
resultChan <- decodeResult
if decodeResult.err != nil {
stream.Close()
return
}
}
}
}()
return resultChan
}
示例12: LoadSchema
func LoadSchema(uri string, localCopy bool) (sd *Schema, err error) {
var protocol, localPath string
var rc io.ReadCloser
if pos := strings.Index(uri, protSep); pos < 0 {
protocol = "http" + protSep
} else {
protocol = uri[:pos+len(protSep)]
uri = uri[pos+len(protSep):]
}
if localCopy {
if localPath = filepath.Join(PkgGen.BaseCodePath, uri); !ufs.FileExists(localPath) {
if err = ufs.EnsureDirExists(filepath.Dir(localPath)); err == nil {
err = unet.DownloadFile(protocol+uri, localPath)
}
}
if err == nil {
if sd, err = loadSchemaFile(localPath, uri); sd != nil {
sd.loadLocalPath = localPath
}
}
} else if rc, err = unet.OpenRemoteFile(protocol + uri); err == nil {
defer rc.Close()
sd, err = loadSchema(rc, uri, "")
}
return
}
示例13: assembleTarTo
func (ls *layerStore) assembleTarTo(graphID string, metadata io.ReadCloser, size *int64, w io.Writer) error {
type diffPathDriver interface {
DiffPath(string) (string, func() error, error)
}
diffDriver, ok := ls.driver.(diffPathDriver)
if !ok {
diffDriver = &naiveDiffPathDriver{ls.driver}
}
defer metadata.Close()
// get our relative path to the container
fsPath, releasePath, err := diffDriver.DiffPath(graphID)
if err != nil {
return err
}
defer releasePath()
metaUnpacker := storage.NewJSONUnpacker(metadata)
upackerCounter := &unpackSizeCounter{metaUnpacker, size}
fileGetter := storage.NewPathFileGetter(fsPath)
logrus.Debugf("Assembling tar data for %s from %s", graphID, fsPath)
return asm.WriteOutputTarStream(fileGetter, upackerCounter, w)
}
示例14: Decode
func (this *PacketCodecZlib) Decode(reader io.Reader) (packet Packet, err error) {
rawBytes := reader.(Byteser).Bytes() // FIXME assuming the caller is a Byteser is a bad idea
length, err := ReadVarInt(reader)
if err != nil {
return
}
if length < 0 {
err = errors.New(fmt.Sprintf("Decode, Compressed length is below zero: %d", length))
return
}
if length == 0 {
packet, err = this.codec.Decode(reader)
} else {
zlibBytes := reader.(Byteser).Bytes() // FIXME assuming the caller is a Byteser is a bad idea
var zlibReader io.ReadCloser
zlibReader, err = NewZlibToggleReaderBuffer(rawBytes, zlibBytes)
if err != nil {
return
}
packet, err = this.codec.Decode(zlibReader)
if err != nil {
return
}
zlibReader.Close()
}
return
}
示例15: Put
func (fs *GDriveFileSystem) Put(p string, bytes io.ReadCloser) webdav.StatusCode {
defer bytes.Close()
parent := path.Dir(p)
base := path.Base(p)
parentId := fs.getFileId(parent, true)
if parentId == "" {
log.Errorf("ERROR: Parent not found")
return webdav.StatusCode(http.StatusConflict) // 409
}
parentRef := &drive.ParentReference{
Id: parentId,
IsRoot: "parent" == "/",
}
f := &drive.File{
Title: base,
Parents: []*drive.ParentReference{parentRef},
}
_, err := fs.client.Files.Insert(f).Media(bytes).Do()
if err != nil {
log.Errorf("can't put: %v", err)
return webdav.StatusCode(500)
}
fs.invalidatePath(p)
fs.invalidatePath(parent)
return webdav.StatusCode(201)
}