]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/file_header.go
Convert more bespoke methods to io.Reader/io.Writer interfaces
[rbdr/mobius] / hotline / file_header.go
index 61b8e288f4a8d79fb3ee3d42d51b43f9a993c08a..71a1c003044199913a553d14f6d41f50429a3521 100644 (file)
@@ -2,35 +2,36 @@ package hotline
 
 import (
        "encoding/binary"
+       "io"
        "slices"
 )
 
 type FileHeader struct {
-       Size     []byte // Total size of FileHeader payload
-       Type     []byte // 0 for file, 1 for dir
-       FilePath []byte // encoded file path
+       Size     [2]byte // Total size of FileHeader payload
+       Type     [2]byte // 0 for file, 1 for dir
+       FilePath []byte  // encoded file path
 }
 
 func NewFileHeader(fileName string, isDir bool) FileHeader {
        fh := FileHeader{
-               Size:     make([]byte, 2),
-               Type:     []byte{0x00, 0x00},
+               Type:     [2]byte{0x00, 0x00},
                FilePath: EncodeFilePath(fileName),
        }
        if isDir {
-               fh.Type = []byte{0x00, 0x01}
+               fh.Type = [2]byte{0x00, 0x01}
        }
 
        encodedPathLen := uint16(len(fh.FilePath) + len(fh.Type))
-       binary.BigEndian.PutUint16(fh.Size, encodedPathLen)
+       binary.BigEndian.PutUint16(fh.Size[:], encodedPathLen)
 
        return fh
 }
 
-func (fh *FileHeader) Payload() []byte {
-       return slices.Concat(
-               fh.Size,
-               fh.Type,
+func (fh *FileHeader) Read(p []byte) (int, error) {
+       return copy(p, slices.Concat(
+               fh.Size[:],
+               fh.Type[:],
                fh.FilePath,
-       )
+       ),
+       ), io.EOF
 }