2 netstring -- netstring format serialization library
3 Copyright (C) 2015-2020 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
33 // Pay attention that bufio.Writer is used to write to it.
34 func NewWriter(w io.Writer) *Writer {
35 return &Writer{w: bufio.NewWriter(w)}
38 // Write size of the data going to be supplied to Write method.
39 // It returns number of length prefixed written (possibly just
41 func (w *Writer) WriteSize(size uint64) (n int, err error) {
43 return 0, errors.New("current chunk in not written")
46 return w.w.WriteString(strconv.FormatUint(size, 10) + ":")
49 // Write the chunk data. WriteSize must preceed this call.
50 // Write could be buffered for writing. Only when the last bytes are
51 // written, then terminator is appended and all the data flushed.
52 // Terminator is not taken in account of written bytes count!
53 func (w *Writer) Write(buf []byte) (written int, err error) {
54 if w.left == 0 && len(buf) > 0 {
55 return 0, errors.New("chunk in already written")
57 written, err = w.w.Write(buf)
61 w.left -= uint64(written)
63 _, err = w.w.Write([]byte{','})
72 // Write the whole chunk at once. It could be convenient to use instead
73 // of WriteSize/Write invocations
74 func (w *Writer) WriteChunk(buf []byte) (n int, err error) {
75 n, err = w.WriteSize(uint64(len(buf)))
80 nw, err = w.Write(buf)