Golang bufio.Writer类(方法)实例源码

下面列出了Golang bufio.Writer 类(方法)源码代码实例,从而了解它的用法。

作者:Netfli    项目:ren   
func writeSuccessResponseHeader(w *bufio.Writer, opcode uint8, keyLength, extraLength,
	totalBodyLength int, opaque uint32, flush bool) error {

	header := resHeadPool.Get().(ResponseHeader)

	header.Magic = MagicResponse
	header.Opcode = opcode
	header.KeyLength = uint16(keyLength)
	header.ExtraLength = uint8(extraLength)
	header.DataType = uint8(0)
	header.Status = StatusSuccess
	header.TotalBodyLength = uint32(totalBodyLength)
	header.OpaqueToken = opaque
	header.CASToken = uint64(0)

	if err := writeResponseHeader(w, header); err != nil {
		resHeadPool.Put(header)
		return err
	}

	if flush {
		if err := w.Flush(); err != nil {
			resHeadPool.Put(header)
			return err
		}
	}

	metrics.IncCounterBy(common.MetricBytesWrittenRemote, resHeaderLen)
	resHeadPool.Put(header)

	return nil
}

作者:stella    项目:bridge-serve   
func writeBodyFixedSize(w *bufio.Writer, r io.Reader, size int64) error {
	if size > maxSmallFileSize {
		// w buffer must be empty for triggering
		// sendfile path in bufio.Writer.ReadFrom.
		if err := w.Flush(); err != nil {
			return err
		}
	}

	// Unwrap a single limited reader for triggering sendfile path
	// in net.TCPConn.ReadFrom.
	lr, ok := r.(*io.LimitedReader)
	if ok {
		r = lr.R
	}

	n, err := copyZeroAlloc(w, r)

	if ok {
		lr.N -= n
	}

	if n != size && err == nil {
		err = fmt.Errorf("copied %d bytes from body stream instead of %d bytes", n, size)
	}
	return err
}

作者:rcharge    项目:zilc   
func (r Resolver) outputResults(w *bufio.Writer, results gresults) {
	var zip, city, state, stateName, county, country, countryName string
	var latitude, longitude float32

	var comp addressComponent
	var err error
	for _, res := range results.Results {
		if comp, err = res.findAddressComponent(zipType); err == nil {
			zip = comp.LongName
		}
		if comp, err = res.findAddressComponent(cityType); err == nil {
			city = comp.LongName
		}
		if comp, err = res.findAddressComponent(countyType); err == nil {
			county = comp.LongName
		}
		if comp, err = res.findAddressComponent(stateType); err == nil {
			state = strings.ToUpper(comp.ShortName)
			stateName = comp.LongName
		}
		if comp, err = res.findAddressComponent(countryType); err == nil {
			country = strings.ToUpper(comp.ShortName)
			countryName = comp.LongName
		}
		latitude = res.Geometry.Location.Lat
		longitude = res.Geometry.Location.Lng
	}
	w.WriteString(fmt.Sprintf("%v,\"%v\",\"%v\",\"%v\",\"%v\",\"%v\",\"%v\",%v,%v\n", country, zip, city, state, stateName, county, countryName, latitude, longitude))
	w.Flush()
}

作者:cloudfoundry-incubato    项目:routing-perf-releas   
// NewStreamReader returns a reader, which replays all the data generated by sw.
//
// The returned reader may be passed to Response.SetBodyStream.
//
// Close must be called on the returned reader after all the required data
// has been read. Otherwise goroutine leak may occur.
//
// See also Response.SetBodyStreamWriter.
func NewStreamReader(sw StreamWriter) io.ReadCloser {
	pc := fasthttputil.NewPipeConns()
	pw := pc.Conn1()
	pr := pc.Conn2()

	var bw *bufio.Writer
	v := streamWriterBufPool.Get()
	if v == nil {
		bw = bufio.NewWriter(pw)
	} else {
		bw = v.(*bufio.Writer)
		bw.Reset(pw)
	}

	go func() {
		defer func() {
			if r := recover(); r != nil {
				defaultLogger.Printf("panic in StreamWriter: %s\nStack trace:\n%s", r, debug.Stack())
			}
		}()

		sw(bw)
		bw.Flush()
		pw.Close()

		streamWriterBufPool.Put(bw)
	}()

	return pr
}

作者:machinau    项目:g   
// authenticate authenticates ourselves with the X server.
// displayStr is the "12" out of ":12.0".
func authenticate(w *bufio.Writer, displayStr string) os.Error {
	key, value, err := readAuth(displayStr)
	if err != nil {
		return err
	}
	// Assume that the authentication protocol is "MIT-MAGIC-COOKIE-1".
	if len(key) != 18 || len(value) != 16 {
		return os.NewError("unsupported Xauth")
	}
	// 0x006c means little-endian. 0x000b, 0x0000 means X major version 11, minor version 0.
	// 0x0012 and 0x0010 means the auth key and value have lengths 18 and 16.
	// The final 0x0000 is padding, so that the string length is a multiple of 4.
	_, err = io.WriteString(w, "\x6c\x00\x0b\x00\x00\x00\x12\x00\x10\x00\x00\x00")
	if err != nil {
		return err
	}
	_, err = io.WriteString(w, key)
	if err != nil {
		return err
	}
	// Again, the 0x0000 is padding.
	_, err = io.WriteString(w, "\x00\x00")
	if err != nil {
		return err
	}
	_, err = io.WriteString(w, value)
	if err != nil {
		return err
	}
	err = w.Flush()
	if err != nil {
		return err
	}
	return nil
}

作者:xingskyc    项目:goi   
func (c *DefaultServerCodec) WriteResponse(wr *bufio.Writer, proto *Proto) (err error) {
	log.Debug("write proto: %v", proto)
	// packlen =header(16) + body
	if err = binary.Write(wr, binary.BigEndian, uint32(rawHeaderLen)+uint32(len(proto.Body))); err != nil {
		log.Error("packLen: binary.Write() error(%v)", err)
		return
	}
	if err = binary.Write(wr, binary.BigEndian, rawHeaderLen); err != nil {
		log.Error("headerLen: binary.Write() error(%v)", err)
		return
	}
	if err = binary.Write(wr, binary.BigEndian, proto.Ver); err != nil {
		log.Error("protoVer: binary.Write() error(%v)", err)
		return
	}
	if err = binary.Write(wr, binary.BigEndian, proto.Operation); err != nil {
		log.Error("operation: binary.Write() error(%v)", err)
		return
	}
	if err = binary.Write(wr, binary.BigEndian, proto.SeqId); err != nil {
		log.Error("seqId: binary.Write() error(%v)", err)
		return
	}
	if proto.Body != nil {
		if _, err = wr.Write(proto.Body); err != nil {
			log.Error("body: wr.Write() error(%v)", err)
			return
		}
	}
	return wr.Flush()
}

作者:mgostic    项目:fasthtt   
// Write writes response to w.
//
// Write doesn't flush response to w for performance reasons.
func (resp *Response) Write(w *bufio.Writer) error {
	var err error
	if resp.bodyStream != nil {
		contentLength := resp.Header.ContentLength()
		if contentLength >= 0 {
			if err = resp.Header.Write(w); err != nil {
				return err
			}
			if err = writeBodyFixedSize(w, resp.bodyStream, contentLength); err != nil {
				return err
			}
		} else {
			resp.Header.SetContentLength(-1)
			if err = resp.Header.Write(w); err != nil {
				return err
			}
			if err = writeBodyChunked(w, resp.bodyStream); err != nil {
				return err
			}
		}
		if bsc, ok := resp.bodyStream.(io.Closer); ok {
			err = bsc.Close()
		}
		return err
	}

	resp.Header.SetContentLength(len(resp.body))
	if err = resp.Header.Write(w); err != nil {
		return err
	}
	_, err = w.Write(resp.body)
	return err
}

作者:johnvilsac    项目:golang-stuf   
func doWriteRune(w *bufio.Writer) {
	if written, err := w.WriteRune(rune('o')); err != nil {
		log.Fatalf("failed writing a rune: %s", err)
	} else {
		log.Printf("Wrote rune in %d bytes", written)
	}
}

作者:johnvilsac    项目:golang-stuf   
func doWriteString(w *bufio.Writer) {
	if written, err := w.WriteString(", The Standard Library\n"); err != nil {
		log.Fatalf("failed writing string: %s", err)
	} else {
		log.Printf("Wrote string in %d bytes", written)
	}
}

作者:sguthri    项目:hgvm-lighting-grap   
func emit_allele_path_item(ofp *bufio.Writer) {

	for k := range g_allele_path_item {

		for i := 0; i < len(g_allele_path_item[k]); i++ {
			s := fmt.Sprintf("%d,%d,%d,%d,%d,%s\n",
				g_allele_path_item[k][i].AlleleId,
				g_allele_path_item[k][i].PathItemIndex,
				g_allele_path_item[k][i].SequenceId,
				g_allele_path_item[k][i].Start,
				g_allele_path_item[k][i].Length,
				g_allele_path_item[k][i].StrandIsForward)
			ofp.Write([]byte(s))
		}
	}

	return

	for allele_name := range g_allele_sequenceid_path {

		allele_id := g_allele_name_id_map[allele_name]

		for i := 0; i < len(g_allele_sequenceid_path[allele_name]); i++ {
			seqid := g_allele_sequenceid_path[allele_name][i]
			s := fmt.Sprintf("%d,%d,%d,%s,%s,%s\n", allele_id, i, seqid, "NULL", "NULL", "'TRUE'")
			ofp.Write([]byte(s))
		}

	}
}

作者:anilprasa    项目:fasthtt   
// Write writes request to w.
//
// Write doesn't flush request to w for performance reasons.
func (req *Request) Write(w *bufio.Writer) error {
	if len(req.Header.Host()) == 0 {
		uri := req.URI()
		host := uri.Host()
		if len(host) == 0 {
			return errRequestHostRequired
		}
		req.Header.SetHostBytes(host)
		req.Header.SetRequestURIBytes(uri.RequestURI())
	}

	body := req.body
	var err error
	if req.multipartForm != nil && len(req.body) == 0 {
		body, err = marshalMultipartForm(req.multipartForm, req.multipartFormBoundary)
		if err != nil {
			return fmt.Errorf("error when marshaling multipart form: %s", err)
		}
		req.Header.SetMultipartFormBoundary(req.multipartFormBoundary)
	}

	hasBody := !req.Header.noBody()
	if hasBody {
		req.Header.SetContentLength(len(body))
	}
	if err = req.Header.Write(w); err != nil {
		return err
	}
	if hasBody {
		_, err = w.Write(body)
	} else if len(body) > 0 {
		return fmt.Errorf("Non-zero body for non-POST request. body=%q", body)
	}
	return err
}

作者:ziute    项目:gogamm   
func writeln(w *bufio.Writer, s string) error {
	_, err := w.WriteString(s)
	if err != nil {
		return err
	}
	return newLine(w)
}

作者:ian    项目:g   
// Function IHex.Write writes the representation of the file to the writer `wr`.
func (ix *IHex) Write(wr *bufio.Writer) (err error) {
	for start, data := range ix.areas {
		i := 0

		for i < len(data) {
			var chunk []byte

			if i+16 <= len(data) {
				chunk = data[i : i+16]
			} else {
				chunk = data[i:]
			}

			_, err := wr.Write([]byte(makeLine(0x00, start, chunk)))
			if err != nil {
				return err
			}

			start += 16
		}
	}

	_, err = wr.Write([]byte(makeLine(0x01, 0, []byte{})))
	if err != nil {
		return err
	}

	return nil
}

作者:Netfli    项目:ren   
func writeErrorResponseHeader(w *bufio.Writer, opcode uint8, status uint16, opaque uint32) error {
	header := resHeadPool.Get().(ResponseHeader)

	header.Magic = MagicResponse
	header.Opcode = opcode
	header.KeyLength = uint16(0)
	header.ExtraLength = uint8(0)
	header.DataType = uint8(0)
	header.Status = status
	header.TotalBodyLength = uint32(0)
	header.OpaqueToken = opaque
	header.CASToken = uint64(0)

	if err := writeResponseHeader(w, header); err != nil {
		resHeadPool.Put(header)
		return err
	}

	if err := w.Flush(); err != nil {
		resHeadPool.Put(header)
		return err
	}

	metrics.IncCounterBy(common.MetricBytesWrittenRemote, resHeaderLen)
	resHeadPool.Put(header)

	return nil
}

作者:uir    项目:bombertc   
func (t *TcpPlayer) sendUpdates(w *bufio.Writer, wg *sync.WaitGroup) {
	defer wg.Done()

	enc := json.NewEncoder(w)

	lastTurnSent := -1
	for update := range t.update {
		t.stateL.Lock()
		t.state = update
		t.stateL.Unlock()

		if update.Turn == lastTurnSent {
			continue
		}

		if err := enc.Encode(update); err != nil {
			t.l.Errorf("[TCP] sending update to player, %v", err)
			return
		}

		if _, err := w.WriteRune('\n'); err != nil {
			t.l.Errorf("[TCP] sending EOL to player, %v", err)
			return
		}

		if err := w.Flush(); err != nil {
			t.l.Errorf("[TCP] flushing update to player, %v", err)
			return
		}
		lastTurnSent = update.Turn
	}
}

作者:hlanda    项目:s   
func writeBase64String(s string, b *bufio.Writer, f *Format) {
  b.WriteRune('|')
  w := base64.NewEncoder(base64.StdEncoding, b)
  w.Write([]byte(s))
  w.Close()
  b.WriteRune('|')
}

作者:hzmne    项目:etre   
// writeTo serializes the processing instruction to the writer.
func (p *ProcInst) writeTo(w *bufio.Writer) {
	w.WriteString("<?")
	w.WriteString(p.Target)
	w.WriteByte(' ')
	w.WriteString(p.Inst)
	w.WriteString("?>")
}

作者:Phili    项目:GoSMP   
// Write Bind Response PDU
func (pdu *PDUBindResp) write(w *bufio.Writer) (err os.Error) {
	// Write Header
	err = pdu.Header.write(w)
	if err != nil {
		err = os.NewError("Bind Response: Error writing Header")
		return
	}
	// Create byte array the size of the PDU
	p := make([]byte, pdu.Header.CmdLength-pdu.OptionalLen-16)
	pos := 0
	// Copy system id
	if len(pdu.SystemId) > 0 {
		copy(p[pos:len(pdu.SystemId)], []byte(pdu.SystemId))
		pos += len(pdu.SystemId)
	}
	pos++ // Null terminator
	// Write to buffer
	_, err = w.Write(p)
	if err != nil {
		err = os.NewError("Bind Response: Error writing to buffer")
		return
	}
	// Flush write buffer
	err = w.Flush()
	if err != nil {
		err = os.NewError("Bind Response: Error flushing write buffer")
	}
	// Optional params
	err = pdu.writeOptional(w)
	if err != nil {
		err = os.NewError("Bind Response: Error writing optional params")
	}
	return
}

作者:xingskyc    项目:goi   
func WriteProto(wr *bufio.Writer, proto *Proto) (err error) {
	// write
	if err = binary.Write(wr, binary.BigEndian, uint32(rawHeaderLen)+uint32(len(proto.Body))); err != nil {
		return
	}
	if err = binary.Write(wr, binary.BigEndian, rawHeaderLen); err != nil {
		return
	}
	if err = binary.Write(wr, binary.BigEndian, proto.Ver); err != nil {
		return
	}
	if err = binary.Write(wr, binary.BigEndian, proto.Operation); err != nil {
		return
	}
	if err = binary.Write(wr, binary.BigEndian, proto.SeqId); err != nil {
		return
	}
	if proto.Body != nil {
		log.Debug("cipher body: %v", proto.Body)
		if err = binary.Write(wr, binary.BigEndian, proto.Body); err != nil {
			return
		}
	}
	err = wr.Flush()
	return
}

作者:zhaohaiy    项目:git-lf   
func writeToStderr(msg string, errWriter *bufio.Writer) {
	if !strings.HasSuffix(msg, "\n") {
		msg = msg + "\n"
	}
	errWriter.WriteString(msg)
	errWriter.Flush()
}


问题


面经


文章

微信
公众号

扫码关注公众号