1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 // A Writer implements convenience methods for writing
15 // requests or responses to a text protocol network connection.
21 // NewWriter returns a new Writer writing to w.
22 func NewWriter(w *bufio.Writer) *Writer {
26 var crnl = []byte{'\r', '\n'}
27 var dotcrnl = []byte{'.', '\r', '\n'}
29 // PrintfLine writes the formatted output followed by \r\n.
30 func (w *Writer) PrintfLine(format string, args ...interface{}) os.Error {
32 fmt.Fprintf(w.W, format, args...)
37 // DotWriter returns a writer that can be used to write a dot-encoding to w.
38 // It takes care of inserting leading dots when necessary,
39 // translating line-ending \n into \r\n, and adding the final .\r\n line
40 // when the DotWriter is closed. The caller should close the
41 // DotWriter before the next call to a method on w.
43 // See the documentation for Reader's DotReader method for details about dot-encoding.
44 func (w *Writer) DotWriter() io.WriteCloser {
46 w.dot = &dotWriter{w: w}
50 func (w *Writer) closeDot() {
52 w.dot.Close() // sets w.dot = nil
56 type dotWriter struct {
62 wstateBeginLine = iota // beginning of line; initial state; must be zero
63 wstateCR // wrote \r (possibly at end of line)
64 wstateData // writing data in middle of line
67 func (d *dotWriter) Write(b []byte) (n int, err os.Error) {
86 d.state = wstateBeginLine
92 d.state = wstateBeginLine
95 if err = bw.WriteByte(c); err != nil {
103 func (d *dotWriter) Close() os.Error {
115 case wstateBeginLine: