Serialize converts the bitArray to a byte slice.
()
| 158 | |
| 159 | // Serialize converts the bitArray to a byte slice. |
| 160 | func (ba *bitArray) Serialize() ([]byte, error) { |
| 161 | w := new(bytes.Buffer) |
| 162 | |
| 163 | var identifier uint8 = 'B' |
| 164 | err := binary.Write(w, binary.LittleEndian, identifier) |
| 165 | if err != nil { |
| 166 | return nil, err |
| 167 | } |
| 168 | |
| 169 | err = binary.Write(w, binary.LittleEndian, ba.lowest) |
| 170 | if err != nil { |
| 171 | return nil, err |
| 172 | } |
| 173 | err = binary.Write(w, binary.LittleEndian, ba.highest) |
| 174 | if err != nil { |
| 175 | return nil, err |
| 176 | } |
| 177 | |
| 178 | var encodedanyset uint8 |
| 179 | if ba.anyset { |
| 180 | encodedanyset = 1 |
| 181 | } else { |
| 182 | encodedanyset = 0 |
| 183 | } |
| 184 | err = binary.Write(w, binary.LittleEndian, encodedanyset) |
| 185 | if err != nil { |
| 186 | return nil, err |
| 187 | } |
| 188 | |
| 189 | err = binary.Write(w, binary.LittleEndian, ba.blocks) |
| 190 | if err != nil { |
| 191 | return nil, err |
| 192 | } |
| 193 | return w.Bytes(), nil |
| 194 | } |
| 195 | |
| 196 | // Deserialize takes the incoming byte slice, and populates the bitArray |
| 197 | // with data in the bytes. Note that this will overwrite any capacity |