CopyNext reads the next object from m without decoding it and writes it to w. It avoids unnecessary copies internally.
(w io.Writer)
| 172 | // CopyNext reads the next object from m without decoding it and writes it to w. |
| 173 | // It avoids unnecessary copies internally. |
| 174 | func (m *Reader) CopyNext(w io.Writer) (int64, error) { |
| 175 | sz, o, err := getNextSize(m.R) |
| 176 | if err != nil { |
| 177 | return 0, err |
| 178 | } |
| 179 | |
| 180 | var n int64 |
| 181 | // Opportunistic optimization: if we can fit the whole thing in the m.R |
| 182 | // buffer, then just get a pointer to that, and pass it to w.Write, |
| 183 | // avoiding an allocation. |
| 184 | if int(sz) >= 0 && int(sz) <= m.R.BufferSize() { |
| 185 | var nn int |
| 186 | var buf []byte |
| 187 | buf, err = m.R.Next(int(sz)) |
| 188 | if err != nil { |
| 189 | if err == io.ErrUnexpectedEOF { |
| 190 | err = ErrShortBytes |
| 191 | } |
| 192 | return 0, err |
| 193 | } |
| 194 | nn, err = w.Write(buf) |
| 195 | n += int64(nn) |
| 196 | } else { |
| 197 | // Fall back to io.CopyN. |
| 198 | // May avoid allocating if w is a ReaderFrom (e.g. bytes.Buffer) |
| 199 | n, err = io.CopyN(w, m.R, int64(sz)) |
| 200 | if err == io.ErrUnexpectedEOF { |
| 201 | err = ErrShortBytes |
| 202 | } |
| 203 | } |
| 204 | if err != nil { |
| 205 | return n, err |
| 206 | } else if n < int64(sz) { |
| 207 | return n, io.ErrShortWrite |
| 208 | } |
| 209 | |
| 210 | if done, err := m.recursiveCall(); err != nil { |
| 211 | return n, err |
| 212 | } else { |
| 213 | defer done() |
| 214 | } |
| 215 | // for maps and slices, read elements |
| 216 | for range o { |
| 217 | var n2 int64 |
| 218 | n2, err = m.CopyNext(w) |
| 219 | if err != nil { |
| 220 | return n, err |
| 221 | } |
| 222 | n += n2 |
| 223 | } |
| 224 | return n, nil |
| 225 | } |
| 226 | |
| 227 | // SetMaxRecursionDepth sets the maximum recursion depth. |
| 228 | func (m *Reader) SetMaxRecursionDepth(d int) { |