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.
39 lines
865 B
39 lines
865 B
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) { |
|
// write given data |
|
err := buff.WriteByte(data[i]) |
|
if err != nil { |
|
return fmt.Errorf("could not write byte: %s", err) |
|
} |
|
} else { |
|
// write null bytes |
|
err := buff.WriteByte(0) |
|
if err != nil { |
|
return fmt.Errorf("could not write byte: %s", err) |
|
} |
|
} |
|
} |
|
|
|
// write constructed buffer`s bytes |
|
_, err := wr.Write(buff.Bytes()) |
|
if err != nil { |
|
return fmt.Errorf("could not write to writer: %s", err) |
|
} |
|
|
|
return nil |
|
}
|
|
|