1 package byteio 2 3 // StickyReader will wrap an EndianReader and record all bytes read and errors 4 // received. 5 // Byte counts and errors will not be returned from any method (except Read so 6 // it still counts as an io.Reader), but can be retrieved from this type. 7 // All methods will be a no-op after an error has been returned, returning 0, 8 // unless that error is cleared on the type 9 type StickyReader struct { 10 Reader EndianReader 11 Err error 12 Count int64 13 } 14 15 // Read will do a simple byte read from the underlying io.Reader. 16 func (s *StickyReader) Read(b []byte) (int, error) { 17 if s.Err != nil { 18 return 0, s.Err 19 } 20 n, err := s.Reader.Read(b) 21 s.Err = err 22 s.Count += int64(n) 23 return n, err 24 } 25 26 // ReadUint8 will read a uint8 from the underlying reader 27 func (s *StickyReader) ReadUint8() uint8 { 28 if s.Err != nil { 29 return 0 30 } 31 i, n, err := s.Reader.ReadUint8() 32 s.Err = err 33 s.Count += int64(n) 34 return i 35 } 36 37 // ReadInt8 will read a int8 from the underlying reader 38 func (s *StickyReader) ReadInt8() int8 { 39 if s.Err != nil { 40 return 0 41 } 42 i, n, err := s.Reader.ReadInt8() 43 s.Err = err 44 s.Count += int64(n) 45 return i 46 } 47 48 // ReadUint16 will read a uint16 from the underlying reader 49 func (s *StickyReader) ReadUint16() uint16 { 50 if s.Err != nil { 51 return 0 52 } 53 i, n, err := s.Reader.ReadUint16() 54 s.Err = err 55 s.Count += int64(n) 56 return i 57 } 58 59 // ReadInt16 will read a int16 from the underlying reader 60 func (s *StickyReader) ReadInt16() int16 { 61 if s.Err != nil { 62 return 0 63 } 64 i, n, err := s.Reader.ReadInt16() 65 s.Err = err 66 s.Count += int64(n) 67 return i 68 } 69 70 // ReadUint32 will read a uint32 from the underlying reader 71 func (s *StickyReader) ReadUint32() uint32 { 72 if s.Err != nil { 73 return 0 74 } 75 i, n, err := s.Reader.ReadUint32() 76 s.Err = err 77 s.Count += int64(n) 78 return i 79 } 80 81 // ReadInt32 will read a int32 from the underlying reader 82 func (s *StickyReader) ReadInt32() int32 { 83 if s.Err != nil { 84 return 0 85 } 86 i, n, err := s.Reader.ReadInt32() 87 s.Err = err 88 s.Count += int64(n) 89 return i 90 } 91 92 // ReadUint64 will read a uint64 from the underlying reader 93 func (s *StickyReader) ReadUint64() uint64 { 94 if s.Err != nil { 95 return 0 96 } 97 i, n, err := s.Reader.ReadUint64() 98 s.Err = err 99 s.Count += int64(n) 100 return i 101 } 102 103 // ReadInt64 will read a int64 from the underlying reader 104 func (s *StickyReader) ReadInt64() int64 { 105 if s.Err != nil { 106 return 0 107 } 108 i, n, err := s.Reader.ReadInt64() 109 s.Err = err 110 s.Count += int64(n) 111 return i 112 } 113 114 // ReadFloat32 will read a float32 from the underlying reader 115 func (s *StickyReader) ReadFloat32() float32 { 116 if s.Err != nil { 117 return 0 118 } 119 i, n, err := s.Reader.ReadFloat32() 120 s.Err = err 121 s.Count += int64(n) 122 return i 123 } 124 125 // ReadFloat64 will read a float64 from the underlying reader 126 func (s *StickyReader) ReadFloat64() float64 { 127 if s.Err != nil { 128 return 0 129 } 130 i, n, err := s.Reader.ReadFloat64() 131 s.Err = err 132 s.Count += int64(n) 133 return i 134 } 135 136 // StickyWriter will wrap an EndianWriter and record all bytes written and 137 // errors received. 138 // Byte counts and errors will not be returned from any method (except Write, 139 // so it still counts as an io.Writer), but can be retrieved from this type. 140 // All methods will be a no-op after an error has been returned, unless that 141 // error is cleared on the type 142 type StickyWriter struct { 143 Writer EndianWriter 144 Err error 145 Count int64 146 } 147 148 // Write will do a simple byte write to the underlying io.Writer. 149 func (s *StickyWriter) Write(p []byte) (int, error) { 150 if s.Err != nil { 151 return 0, s.Err 152 } 153 n, err := s.Writer.Write(p) 154 s.Err = err 155 s.Count += int64(n) 156 return n, err 157 } 158 159 // WriteUint8 will write a uint8 to the underlying writer 160 func (s *StickyWriter) WriteUint8(i uint8) { 161 if s.Err != nil { 162 return 163 } 164 n, err := s.Writer.WriteUint8(i) 165 s.Err = err 166 s.Count += int64(n) 167 } 168 169 // WriteInt8 will write a int8 to the underlying writer 170 func (s *StickyWriter) WriteInt8(i int8) { 171 if s.Err != nil { 172 return 173 } 174 n, err := s.Writer.WriteInt8(i) 175 s.Err = err 176 s.Count += int64(n) 177 } 178 179 // WriteUint16 will write a uint16 to the underlying writer 180 func (s *StickyWriter) WriteUint16(i uint16) { 181 if s.Err != nil { 182 return 183 } 184 n, err := s.Writer.WriteUint16(i) 185 s.Err = err 186 s.Count += int64(n) 187 } 188 189 // WriteInt16 will write a int16 to the underlying writer 190 func (s *StickyWriter) WriteInt16(i int16) { 191 if s.Err != nil { 192 return 193 } 194 n, err := s.Writer.WriteInt16(i) 195 s.Err = err 196 s.Count += int64(n) 197 } 198 199 // WriteUint32 will write a uint32 to the underlying writer 200 func (s *StickyWriter) WriteUint32(i uint32) { 201 if s.Err != nil { 202 return 203 } 204 n, err := s.Writer.WriteUint32(i) 205 s.Err = err 206 s.Count += int64(n) 207 } 208 209 // WriteInt32 will write a int32 to the underlying writer 210 func (s *StickyWriter) WriteInt32(i int32) { 211 if s.Err != nil { 212 return 213 } 214 n, err := s.Writer.WriteInt32(i) 215 s.Err = err 216 s.Count += int64(n) 217 } 218 219 // WriteUint64 will write a uint64 to the underlying writer 220 func (s *StickyWriter) WriteUint64(i uint64) { 221 if s.Err != nil { 222 return 223 } 224 n, err := s.Writer.WriteUint64(i) 225 s.Err = err 226 s.Count += int64(n) 227 } 228 229 // WriteInt64 will write a int64 to the underlying writer 230 func (s *StickyWriter) WriteInt64(i int64) { 231 if s.Err != nil { 232 return 233 } 234 n, err := s.Writer.WriteInt64(i) 235 s.Err = err 236 s.Count += int64(n) 237 } 238 239 // WriteFloat32 will write a float32 to the underlying writer 240 func (s *StickyWriter) WriteFloat32(i float32) { 241 if s.Err != nil { 242 return 243 } 244 n, err := s.Writer.WriteFloat32(i) 245 s.Err = err 246 s.Count += int64(n) 247 } 248 249 // WriteFloat64 will write a float64 to the underlying writer 250 func (s *StickyWriter) WriteFloat64(i float64) { 251 if s.Err != nil { 252 return 253 } 254 n, err := s.Writer.WriteFloat64(i) 255 s.Err = err 256 s.Count += int64(n) 257 } 258