本文整理汇总了Golang中io.PipeWriter.CloseWithError方法的典型用法代码示例。如果您正苦于以下问题:Golang PipeWriter.CloseWithError方法的具体用法?Golang PipeWriter.CloseWithError怎么用?Golang PipeWriter.CloseWithError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.PipeWriter
的用法示例。
在下文中一共展示了PipeWriter.CloseWithError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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))
}
示例2: 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)))
}
示例3: 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))
}
}
示例4: newMultipartReader
func newMultipartReader(parts []typeReader) *multipartReader {
mp := &multipartReader{pipeOpen: true}
var pw *io.PipeWriter
mp.pr, pw = io.Pipe()
mpw := multipart.NewWriter(pw)
mp.ctype = "multipart/related; boundary=" + mpw.Boundary()
go func() {
for _, part := range parts {
w, err := mpw.CreatePart(typeHeader(part.typ))
if err != nil {
mpw.Close()
pw.CloseWithError(fmt.Errorf("googleapi: CreatePart failed: %v", err))
return
}
_, err = io.Copy(w, part.Reader)
if err != nil {
mpw.Close()
pw.CloseWithError(fmt.Errorf("googleapi: Copy failed: %v", err))
return
}
}
mpw.Close()
pw.Close()
}()
return mp
}
示例5: 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
}
}
}
示例6: 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)
}
示例7: 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
}
}
}
示例8: 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
}
示例9: reset
func (c *compressed) reset() {
var w *io.PipeWriter
c.r, w = io.Pipe()
c.wg.Add(1)
go func() {
defer c.wg.Done()
compressor := isolated.GetCompressor(w)
_, err := io.Copy(compressor, c.src)
if err2 := compressor.Close(); err == nil {
err = err2
}
w.CloseWithError(err)
}()
}
示例10: PipeWrite
func PipeWrite(pipeWriter *io.PipeWriter) {
var (
i = 0
err error
n int
)
data := []byte("Go语言学习园地")
for _, err = pipeWriter.Write(data); err == nil; n, err = pipeWriter.Write(data) {
i++
if i == 3 {
pipeWriter.CloseWithError(errors.New("输出3次后结束"))
}
}
fmt.Println("close 后输出的字节数:", n, " error:", err)
}
示例11: joinFilesInGoRoutine
func joinFilesInGoRoutine(fileInfos []os.FileInfo, writer *io.PipeWriter) {
for _, fileInfo := range fileInfos {
file, err := os.Open(fileInfo.Name())
defer file.Close()
for err != nil {
writer.CloseWithError(err)
return
}
_, err = io.Copy(writer, file)
if err != nil {
writer.CloseWithError(err)
return
}
}
writer.Close()
}
示例12: readDat
func readDat(filename string, c chan io.Reader) {
f, err := os.Open("testdata/webkit/" + filename)
if err != nil {
c <- pipeErr(err)
return
}
defer f.Close()
// Loop through the lines of the file. Each line beginning with "#" denotes
// a new section, which is returned as a separate io.Reader.
r := bufio.NewReader(f)
var pw *io.PipeWriter
for {
line, err := r.ReadSlice('\n')
if err != nil {
if pw != nil {
pw.CloseWithError(err)
pw = nil
} else {
c <- pipeErr(err)
}
return
}
if len(line) == 0 {
continue
}
if line[0] == '#' {
if pw != nil {
pw.Close()
}
var pr *io.PipeReader
pr, pw = io.Pipe()
c <- pr
continue
}
if line[0] != '|' {
// Strip the trailing '\n'.
line = line[:len(line)-1]
}
if pw != nil {
if _, err := pw.Write(line); err != nil {
pw.CloseWithError(err)
pw = nil
}
}
}
}
示例13: secReadLoop
// secReadLoop copies data from r into pw
// doing a nacl open decryption on the data in the process using shared as the key
func secReadLoop(r io.Reader, pw *io.PipeWriter, shared *[32]byte) {
var failed bool
// check for an error, stops the loop and
// closes the pipe with err to signal the reader we failed
var check = func(err error) {
if err != nil {
log.Println("secReadLoop err:", err)
if err2 := pw.CloseWithError(err); err2 != nil {
log.Println("CloseWithError failed", err2)
}
failed = true
}
}
for !failed { // until an error occurs
// read next ciphered message from the passed reader
msg := make([]byte, 32*1024)
n, err := io.ReadAtLeast(r, msg, 25)
// the closed conn check could be nicer but there is no way to access the abstracted TCPConn cleanly with the pipes involved
if err != nil && (err == io.EOF || strings.Contains(err.Error(), "use of closed network connection")) {
checkFatal(pw.Close())
return
}
check(err)
// slice of the unused rest of the buffer
msg = msg[:n]
// copy the nonce from the message
var nonce [24]byte
copy(nonce[:], msg[:24])
// cut of the nonce
msg = msg[24:]
// decrypt message
clearMsg, ok := box.OpenAfterPrecomputation([]byte{}, msg, &nonce, shared)
if !ok {
check(errors.New("open failed"))
}
// copy the decrypted message to our pipe
_, err = io.Copy(pw, bytes.NewReader(clearMsg))
check(err)
}
}
示例14: sqlplusWrite
func sqlplusWrite(pw *io.PipeWriter) {
var err error
defer func() {
pw.CloseWithError(err)
}()
err = sqlplusFormatOutput(pw)
if err != nil {
return
}
for _, p := range sqlplusParsers {
_, err = io.WriteString(pw, p.query)
if err != nil {
return
}
}
err = sqlplusExit(pw)
}
示例15: sendSnapFile
func sendSnapFile(snapPath string, snapFile *os.File, pw *io.PipeWriter, mw *multipart.Writer, action *actionData) {
defer snapFile.Close()
if action.SnapOptions == nil {
action.SnapOptions = &SnapOptions{}
}
errs := []error{
mw.WriteField("action", action.Action),
mw.WriteField("name", action.Name),
mw.WriteField("snap-path", action.SnapPath),
mw.WriteField("channel", action.Channel),
mw.WriteField("devmode", strconv.FormatBool(action.DevMode)),
}
for _, err := range errs {
if err != nil {
pw.CloseWithError(err)
return
}
}
fw, err := mw.CreateFormFile("snap", filepath.Base(snapPath))
if err != nil {
pw.CloseWithError(err)
return
}
_, err = io.Copy(fw, snapFile)
if err != nil {
pw.CloseWithError(err)
return
}
mw.Close()
pw.Close()
}