本文整理汇总了Golang中io.PipeWriter类的典型用法代码示例。如果您正苦于以下问题:Golang PipeWriter类的具体用法?Golang PipeWriter怎么用?Golang PipeWriter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PipeWriter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: teardownPeer
func teardownPeer(t *testing.T, c *Client,
in *io.PipeReader, out *io.PipeWriter) {
// in.Close()
out.Close()
c.shares.halt()
os.RemoveAll(c.DownloadRoot)
}
示例2: HandleRead
func HandleRead(filename string, w *io.PipeWriter) {
fmt.Printf("Filename : %v \n", []byte(filename))
var exists bool
d, err := localConf.fs.Get("tftp/" + filename[0:len(filename)-1])
defer d.Close()
fmt.Println(d, err)
if err == nil {
exists = true
}
if exists {
// copy all the data into a buffer
data, err := ioutil.ReadAll(d)
if err != nil {
fmt.Fprintf(os.Stderr, "Copy Error : %v\n", err)
}
buf := bytes.NewBuffer(data)
c, e := io.Copy(w, buf)
d.Close()
if e != nil {
fmt.Fprintf(os.Stderr, "Can't send %s: %v\n", filename, e)
} else {
fmt.Fprintf(os.Stderr, "Sent %s (%d bytes)\n", filename, c)
}
w.Close()
} else {
w.CloseWithError(fmt.Errorf("File not exists: %s", filename))
}
}
示例3: Wait
// We overload the Wait() method to enable subprocess termination if a
// timeout has been exceeded.
func (mc *ManagedCmd) Wait() (err error) {
go func() {
mc.done <- mc.Cmd.Wait()
}()
if mc.timeout_duration != 0 {
select {
case <-mc.Stopchan:
err = fmt.Errorf("CommandChain was stopped with error: [%s]", mc.kill())
case <-time.After(mc.timeout_duration):
err = fmt.Errorf("CommandChain timedout with error: [%s]", mc.kill())
case err = <-mc.done:
}
} else {
select {
case <-mc.Stopchan:
err = fmt.Errorf("CommandChain was stopped with error: [%s]", mc.kill())
case err = <-mc.done:
}
}
var writer *io.PipeWriter
var ok bool
writer, ok = mc.Stdout.(*io.PipeWriter)
if ok {
writer.Close()
}
writer, ok = mc.Stderr.(*io.PipeWriter)
if ok {
writer.Close()
}
return err
}
示例4: decode
// decode decompresses bytes from r and writes them to pw.
// read specifies how to decode bytes into codes.
// litWidth is the width in bits of literal codes.
func decode(r io.Reader, read func(*decoder) (uint16, os.Error), litWidth int, pw *io.PipeWriter) {
br, ok := r.(io.ByteReader)
if !ok {
br = bufio.NewReader(r)
}
pw.CloseWithError(decode1(pw, br, read, uint(litWidth)))
}
示例5: mergeMultipart
func (donut API) mergeMultipart(parts *CompleteMultipartUpload, uploadID string, fullObjectWriter *io.PipeWriter) {
for _, part := range parts.Part {
recvMD5 := part.ETag
object, ok := donut.multiPartObjects[uploadID].Get(part.PartNumber)
if ok == false {
fullObjectWriter.CloseWithError(probe.WrapError(probe.NewError(InvalidPart{})))
return
}
calcMD5Bytes := md5.Sum(object)
// complete multi part request header md5sum per part is hex encoded
recvMD5Bytes, err := hex.DecodeString(strings.Trim(recvMD5, "\""))
if err != nil {
fullObjectWriter.CloseWithError(probe.WrapError(probe.NewError(InvalidDigest{Md5: recvMD5})))
return
}
if !bytes.Equal(recvMD5Bytes, calcMD5Bytes[:]) {
fullObjectWriter.CloseWithError(probe.WrapError(probe.NewError(BadDigest{})))
return
}
if _, err := io.Copy(fullObjectWriter, bytes.NewReader(object)); err != nil {
fullObjectWriter.CloseWithError(probe.WrapError(probe.NewError(err)))
return
}
object = nil
}
fullObjectWriter.Close()
return
}
示例6: encode
func encode(r io.Reader, pw *io.PipeWriter) {
br, ok := r.(io.ByteReader)
if !ok {
br = bufio.NewReader(r)
}
pw.CloseWithError(encode1(pw, br))
}
示例7: StreamWriteMultipartForm
func StreamWriteMultipartForm(params map[string]string, fileField, path, boundary string, pw *io.PipeWriter, buf *bytes.Buffer) {
defer pw.Close()
mpw := multipart.NewWriter(pw)
mpw.SetBoundary(boundary)
if fileField != "" && path != "" {
fw, err := mpw.CreateFormFile(fileField, filepath.Base(path))
if err != nil {
log.Fatal(err)
return
}
if buf != nil {
_, err = io.Copy(fw, buf)
if err != nil {
log.Fatal(err)
return
}
}
}
for key, val := range params {
_ = mpw.WriteField(key, val)
}
err := mpw.Close()
if err != nil {
log.Fatal(err)
return
}
}
示例8: streamingUploadFile
// Streams upload directly from file -> mime/multipart -> pipe -> http-request
func streamingUploadFile(params map[string]string, paramName, path string, w *io.PipeWriter, file *os.File) {
defer file.Close()
defer w.Close()
writer := multipart.NewWriter(w)
part, err := writer.CreateFormFile(paramName, filepath.Base(path))
if err != nil {
log.Fatal(err)
return
}
_, err = io.Copy(part, file)
if err != nil {
log.Fatal(err)
return
}
for key, val := range params {
_ = writer.WriteField(key, val)
}
err = writer.Close()
if err != nil {
log.Fatal(err)
return
}
}
示例9: streamUploadBody
// Encode the file and request parameters in a multipart body.
// File contents are streamed into the request using an io.Pipe in a separated goroutine
func streamUploadBody(client *FlickrClient, photo io.Reader, body *io.PipeWriter, fileName string, boundary string) {
// multipart writer to fill the body
defer body.Close()
writer := multipart.NewWriter(body)
writer.SetBoundary(boundary)
// create the "photo" field
part, err := writer.CreateFormFile("photo", filepath.Base(fileName))
if err != nil {
log.Fatal(err)
return
}
// fill the photo field
_, err = io.Copy(part, photo)
if err != nil {
log.Fatal(err)
return
}
// dump other params
for key, val := range client.Args {
_ = writer.WriteField(key, val[0])
}
// close the form writer
err = writer.Close()
if err != nil {
log.Fatal(err)
return
}
}
示例10: startReading
// startReading starts a goroutine receiving the lines out of the reader
// in the background and passing them to a created string channel. This
// will used in the assertions.
func startReading(c *gc.C, tailer *tailer.Tailer, reader *io.PipeReader, writer *io.PipeWriter) chan string {
linec := make(chan string)
// Start goroutine for reading.
go func() {
defer close(linec)
reader := bufio.NewReader(reader)
for {
line, err := reader.ReadString('\n')
switch err {
case nil:
linec <- line
case io.EOF:
return
default:
c.Fail()
}
}
}()
// Close writer when tailer is stopped or has an error. Tailer using
// components can do it the same way.
go func() {
tailer.Wait()
writer.Close()
}()
return linec
}
示例11: encode
func encode(out *io.PipeWriter, in io.ReadCloser, enc string, level int) {
var (
e encoder
err error
)
defer func() {
if e != nil {
e.Close()
}
if err == nil {
err = io.EOF
}
out.CloseWithError(err)
in.Close()
}()
if level == flate.BestSpeed {
pool := encoderPool(enc)
pe := pool.Get()
if pe != nil {
e = pe.(encoder)
defer pool.Put(pe)
}
}
if e == nil {
e, err = newEncoder(enc, level)
if err != nil {
return
}
}
e.Reset(out)
b := make([]byte, bufferSize)
for {
n, rerr := in.Read(b)
if n > 0 {
_, err = e.Write(b[:n])
if err != nil {
break
}
err = e.Flush()
if err != nil {
break
}
}
if rerr != nil {
err = rerr
break
}
}
}
示例12: streamProcess
func (s *GardenServer) streamProcess(logger lager.Logger, conn net.Conn, process garden.Process, stdinPipe *io.PipeWriter, connCloseCh chan struct{}) {
statusCh := make(chan int, 1)
errCh := make(chan error, 1)
go func() {
status, err := process.Wait()
if err != nil {
logger.Error("wait-failed", err, lager.Data{
"id": process.ID(),
})
errCh <- err
} else {
logger.Info("exited", lager.Data{
"status": status,
"id": process.ID(),
})
statusCh <- status
}
}()
for {
select {
case status := <-statusCh:
transport.WriteMessage(conn, &transport.ProcessPayload{
ProcessID: process.ID(),
ExitStatus: &status,
})
stdinPipe.Close()
return
case err := <-errCh:
e := err.Error()
transport.WriteMessage(conn, &transport.ProcessPayload{
ProcessID: process.ID(),
Error: &e,
})
stdinPipe.Close()
return
case <-s.stopping:
logger.Debug("detaching", lager.Data{
"id": process.ID(),
})
return
case <-connCloseCh:
return
}
}
}
示例13: PipeWriter
func PipeWriter(data map[string]interface{}) {
w := io.PipeWriter{}
defer w.Close()
w.Write([]byte("pipeWriter"))
// err := tpl.Execute(w, data)
// if err != nil {
// fmt.Println(err)
// }
}
示例14: checkRequestEnd
func checkRequestEnd(w *io.PipeWriter, c io.Reader) {
req, err := http.ReadRequest(bufio.NewReaderSize(io.TeeReader(c, w), h2FrameSize))
if err != nil {
w.CloseWithError(err)
return
}
defer req.Body.Close()
_, err = io.Copy(ioutil.Discard, req.Body)
w.CloseWithError(err)
}
示例15: streamInput
func (s *GardenServer) streamInput(decoder *json.Decoder, in *io.PipeWriter, process garden.Process, connCloseCh chan struct{}) {
for {
var payload transport.ProcessPayload
err := decoder.Decode(&payload)
if err != nil {
close(connCloseCh)
in.CloseWithError(errors.New("Connection closed"))
return
}
switch {
case payload.TTY != nil:
process.SetTTY(*payload.TTY)
case payload.Source != nil:
if payload.Data == nil {
in.Close()
return
} else {
_, err := in.Write([]byte(*payload.Data))
if err != nil {
return
}
}
case payload.Signal != nil:
s.logger.Info("stream-input-process-signal", lager.Data{"payload": payload})
switch *payload.Signal {
case garden.SignalKill:
err = process.Signal(garden.SignalKill)
if err != nil {
s.logger.Error("stream-input-process-signal-kill-failed", err, lager.Data{"payload": payload})
}
case garden.SignalTerminate:
err = process.Signal(garden.SignalTerminate)
if err != nil {
s.logger.Error("stream-input-process-signal-terminate-failed", err, lager.Data{"payload": payload})
}
default:
s.logger.Error("stream-input-unknown-process-payload-signal", nil, lager.Data{"payload": payload})
in.Close()
return
}
default:
s.logger.Error("stream-input-unknown-process-payload", nil, lager.Data{"payload": payload})
in.Close()
return
}
}
}