encodeChunk takes functions that output chunk-specific header and data to a writer, and then uses them to compute header and chunk sizes, as well as writing the whole chunk to a byte array, which is then returned.
(chunkType uint16, headerf func(w binary.Writer), dataf func(w binary.Writer))
| 169 | // encodeChunk takes functions that output chunk-specific header and data to a writer, and then uses them to |
| 170 | // compute header and chunk sizes, as well as writing the whole chunk to a byte array, which is then returned. |
| 171 | func encodeChunk(chunkType uint16, headerf func(w binary.Writer), dataf func(w binary.Writer)) []byte { |
| 172 | var headerBuffer bytes.Buffer |
| 173 | headerf(endian.Writer(&headerBuffer, device.LittleEndian)) |
| 174 | headerBytes := headerBuffer.Bytes() |
| 175 | |
| 176 | var dataBuffer bytes.Buffer |
| 177 | dataf(endian.Writer(&dataBuffer, device.LittleEndian)) |
| 178 | dataBytes := dataBuffer.Bytes() |
| 179 | |
| 180 | var chunkBuffer bytes.Buffer |
| 181 | w := endian.Writer(&chunkBuffer, device.LittleEndian) |
| 182 | w.Uint16(chunkType) |
| 183 | w.Uint16(uint16(len(headerBytes) + 8)) |
| 184 | w.Uint32(uint32(len(headerBytes) + len(dataBytes) + 8)) |
| 185 | w.Data(headerBytes) |
| 186 | w.Data(dataBytes) |
| 187 | |
| 188 | return chunkBuffer.Bytes() |
| 189 | } |