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