github.com/deanMdreon/kafka-go@v0.4.32/crc32.go (about) 1 package kafka 2 3 import ( 4 "encoding/binary" 5 "hash/crc32" 6 ) 7 8 type crc32Writer struct { 9 table *crc32.Table 10 buffer [8]byte 11 crc32 uint32 12 } 13 14 func (w *crc32Writer) update(b []byte) { 15 w.crc32 = crc32.Update(w.crc32, w.table, b) 16 } 17 18 func (w *crc32Writer) writeInt8(i int8) { 19 w.buffer[0] = byte(i) 20 w.update(w.buffer[:1]) 21 } 22 23 func (w *crc32Writer) writeInt16(i int16) { 24 binary.BigEndian.PutUint16(w.buffer[:2], uint16(i)) 25 w.update(w.buffer[:2]) 26 } 27 28 func (w *crc32Writer) writeInt32(i int32) { 29 binary.BigEndian.PutUint32(w.buffer[:4], uint32(i)) 30 w.update(w.buffer[:4]) 31 } 32 33 func (w *crc32Writer) writeInt64(i int64) { 34 binary.BigEndian.PutUint64(w.buffer[:8], uint64(i)) 35 w.update(w.buffer[:8]) 36 } 37 38 func (w *crc32Writer) writeBytes(b []byte) { 39 n := len(b) 40 if b == nil { 41 n = -1 42 } 43 w.writeInt32(int32(n)) 44 w.update(b) 45 } 46 47 func (w *crc32Writer) Write(b []byte) (int, error) { 48 w.update(b) 49 return len(b), nil 50 } 51 52 func (w *crc32Writer) WriteString(s string) (int, error) { 53 w.update([]byte(s)) 54 return len(s), nil 55 }