/* DESCRIPTION utils.go provides buffer utilities used by jpeg.go. TODO: make this exported in codecutil. AUTHOR Saxon Nelson-Milton LICENSE Copyright (C) 2019 the Australian Ocean Lab (AusOcean) It is free software: you can redistribute it and/or modify them under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. It is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with revid in gpl.txt. If not, see http://www.gnu.org/licenses. */ package mjpeg import ( "encoding/binary" "io" ) type putBuffer struct { len int bytes []byte } func newPutBuffer(b []byte) *putBuffer { return &putBuffer{bytes: b} } func (p *putBuffer) Write(b []byte) (int, error) { copy(p.bytes[p.len:], b) p.len += len(b) return len(b), nil } func (p *putBuffer) writeTo(d io.Writer) (int, error) { n, err := d.Write(p.bytes[0:p.len]) p.len -= n return n, err } func (p *putBuffer) put16(v uint16) { binary.BigEndian.PutUint16(p.bytes[p.len:], v) p.len += 2 } func (p *putBuffer) put8(v uint8) { p.bytes[p.len] = byte(v) p.len++ } func (p *putBuffer) putBytes(src []byte) { copy(p.bytes[p.len:], src) p.len += len(src) } func (p *putBuffer) put16At(v uint16, i int) { binary.BigEndian.PutUint16(p.bytes[i:], v) } func (p *putBuffer) reset() { p.len = 0 }