/* DESCRIPTION utils.go provides buffer utilities used by jpeg.go. 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 { i int b []byte } func newPutBuffer(b []byte) *putBuffer { return &putBuffer{b: b} } func (p *putBuffer) Write(b []byte) (int, error) { copy(p.b[p.i:], b) p.i += len(b) return len(b), nil } func (p *putBuffer) writeTo(d io.Writer) (int, error) { n, err := d.Write(p.b[0:p.i]) p.i -= n return n, err } func (p *putBuffer) put16(v uint16) { binary.BigEndian.PutUint16(p.b[p.i:], v) p.i += 2 } func (p *putBuffer) put8(v uint8) { p.b[p.i] = byte(v) p.i++ } func (p *putBuffer) putBuf(src []byte) { copy(p.b[p.i:], src) p.i += len(src) } func (p *putBuffer) put16At(v uint16, i int) { binary.BigEndian.PutUint16(p.b[i:], v) } func (p *putBuffer) reset() { p.i = 0 } func (p *putBuffer) len() int { return p.i } type byteStream struct { bytes []byte i int } func newByteStream(b []byte) *byteStream { return &byteStream{bytes: b} } func (b *byteStream) get24() int { v := int(b.bytes[b.i])<<16 | int(b.bytes[b.i+1])<<8 | int(b.bytes[b.i+2]) b.i += 3 return v } func (b *byteStream) get8() int { v := int(b.bytes[b.i]) b.i++ return v } func (b *byteStream) get16() int { v := int(binary.BigEndian.Uint16(b.bytes[b.i:])) b.i += 2 return v } func (b *byteStream) getBuf(n int) []byte { v := b.bytes[b.i : b.i+n] b.i += n return v } func (b *byteStream) remaining() int { return len(b.bytes) - b.i } func (b *byteStream) writeTo(w io.Writer, n int) error { _n, err := w.Write(b.bytes[b.i : b.i+n]) b.i += _n if err != nil { return err } return nil }