aboutsummaryrefslogtreecommitdiff
path: root/hotline/file_name_with_info.go
diff options
context:
space:
mode:
authorJeff Halter <868228+jhalter@users.noreply.github.com>2024-06-08 20:36:04 -0700
committerJeff Halter <868228+jhalter@users.noreply.github.com>2024-06-09 14:05:15 -0700
commitb129b7cbc9fd9a9c11a77e5922861ef08893efa1 (patch)
tree1e3687d1c96fb54bcac1d9c13cdac4297976a758 /hotline/file_name_with_info.go
parent9c44621ee1ca9abfca79c593e80193afd9204c83 (diff)
Convert bespoke methods to io.Reader/io.Writer interfaces
Diffstat (limited to 'hotline/file_name_with_info.go')
-rw-r--r--hotline/file_name_with_info.go38
1 files changed, 20 insertions, 18 deletions
diff --git a/hotline/file_name_with_info.go b/hotline/file_name_with_info.go
index f230856..ac64c74 100644
--- a/hotline/file_name_with_info.go
+++ b/hotline/file_name_with_info.go
@@ -3,6 +3,8 @@ package hotline
import (
"bytes"
"encoding/binary"
+ "io"
+ "slices"
)
type FileNameWithInfo struct {
@@ -24,28 +26,28 @@ func (f *fileNameWithInfoHeader) nameLen() int {
return int(binary.BigEndian.Uint16(f.NameSize[:]))
}
-func (f *FileNameWithInfo) MarshalBinary() (data []byte, err error) {
- var buf bytes.Buffer
- err = binary.Write(&buf, binary.LittleEndian, f.fileNameWithInfoHeader)
- if err != nil {
- return data, err
- }
-
- _, err = buf.Write(f.name)
- if err != nil {
- return data, err
- }
-
- return buf.Bytes(), err
+// Read implements io.Reader for FileNameWithInfo
+func (f *FileNameWithInfo) Read(b []byte) (int, error) {
+ return copy(b,
+ slices.Concat(
+ f.Type[:],
+ f.Creator[:],
+ f.FileSize[:],
+ f.RSVD[:],
+ f.NameScript[:],
+ f.NameSize[:],
+ f.name,
+ ),
+ ), io.EOF
}
-func (f *FileNameWithInfo) UnmarshalBinary(data []byte) error {
- err := binary.Read(bytes.NewReader(data), binary.BigEndian, &f.fileNameWithInfoHeader)
+func (f *FileNameWithInfo) Write(p []byte) (int, error) {
+ err := binary.Read(bytes.NewReader(p), binary.BigEndian, &f.fileNameWithInfoHeader)
if err != nil {
- return err
+ return 0, err
}
headerLen := binary.Size(f.fileNameWithInfoHeader)
- f.name = data[headerLen : headerLen+f.nameLen()]
+ f.name = p[headerLen : headerLen+f.nameLen()]
- return err
+ return len(p), nil
}