Files
forever-files/fileUtilities/parity.go
2023-07-18 18:21:12 -06:00

56 lines
1.0 KiB
Go

package fileUtilities
import (
"fmt"
"io"
)
func parityStream(in1, in2 io.Reader, out io.Writer) error {
var err error
byteSize := 1024
done1 := false
done2 := false
for !done1 && !done2 {
// get bytes from in1 and in2 and write the parity to buf
// if either in1 or in2 is done, write the remaining bytes from the other to buf
in1Bytes := make([]byte, byteSize)
in2Bytes := make([]byte, byteSize)
read1 := 0
read2 := 0
if !done1 {
read1, err = in1.Read(in1Bytes)
if err != nil {
if err == io.EOF {
done1 = true
} else {
return err
}
}
}
if !done2 {
read2, err = in2.Read(in2Bytes)
if err != nil {
if err == io.EOF {
done2 = true
} else {
return err
}
}
}
maxRead := read1
if read2 > maxRead {
maxRead = read2
}
parityBytes := make([]byte, maxRead)
for i := 0; i < maxRead; i++ {
parityBytes[i] = in1Bytes[i] ^ in2Bytes[i]
}
_, err := out.Write(parityBytes)
if err != nil {
return fmt.Errorf("error writing to buffer: %w", err)
}
}
return nil
}