You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
657 B
37 lines
657 B
3 years ago
|
package util
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"fmt"
|
||
|
"io"
|
||
|
)
|
||
|
|
||
|
// Writes data to wr, if len(data) is less than lenNeeded - adds null bytes until written lenNeeded bytes
|
||
|
func WriteToExtent(wr io.Writer, data []byte, lenNeeded int) error {
|
||
|
if len(data) > lenNeeded {
|
||
|
return fmt.Errorf("length of given data bytes is bigger than length needed")
|
||
|
}
|
||
|
|
||
|
buff := new(bytes.Buffer)
|
||
|
for i := 0; i < lenNeeded; i++ {
|
||
|
if i < len(data) {
|
||
|
err := buff.WriteByte(data[i])
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
} else {
|
||
|
err := buff.WriteByte(0)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
_, err := wr.Write(buff.Bytes())
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|