This repository has been archived by the owner on Aug 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from luxas/frame_utils
Add a set of small Frame utils
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package serializer | ||
|
||
import "io" | ||
|
||
// FrameList is a list of frames (byte arrays), used for convenience functions | ||
type FrameList [][]byte | ||
|
||
// ReadFrameList is a convenience method that reads all available frames from the FrameReader | ||
// into a returned FrameList | ||
func ReadFrameList(fr FrameReader) (FrameList, error) { | ||
// TODO: Create an unit test for this function | ||
var frameList [][]byte | ||
for { | ||
// Read until we get io.EOF or an error | ||
frame, err := fr.ReadFrame() | ||
if err == io.EOF { | ||
break | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
// Append all frames to the returned list | ||
frameList = append(frameList, frame) | ||
} | ||
return frameList, nil | ||
} | ||
|
||
// WriteFrameList is a convenience method that writes a set of frames to a FrameWriter | ||
func WriteFrameList(fw FrameWriter, frameList FrameList) error { | ||
// TODO: Create an unit test for this function | ||
// Loop all frames in the list, and write them individually to the FrameWriter | ||
for _, frame := range frameList { | ||
if _, err := fw.Write(frame); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |