From b2cc3359a8033e52c306f77d9586b6eb70b2c776 Mon Sep 17 00:00:00 2001 From: zervo Date: Fri, 14 Feb 2025 16:55:10 +0100 Subject: [PATCH] Add SerializeBuffered function Add a SerializeBuffered function to provide full tree serialization support to non-seeking writers. --- SBHPFv1/buffered_serializer.go | 33 +++++++++++++++++++++++++++++++++ SBHPFv1/serializer.go | 5 +++-- SBHPFv1/streamed_serializer.go | 20 -------------------- 3 files changed, 36 insertions(+), 22 deletions(-) create mode 100644 SBHPFv1/buffered_serializer.go delete mode 100644 SBHPFv1/streamed_serializer.go diff --git a/SBHPFv1/buffered_serializer.go b/SBHPFv1/buffered_serializer.go new file mode 100644 index 0000000..3bedf9c --- /dev/null +++ b/SBHPFv1/buffered_serializer.go @@ -0,0 +1,33 @@ +package sbhpfv1 + +import ( + "io" + + "git.zervo.org/FLUX/GoSBHPF/pkg/seekablebuffer" +) + +// SerializeNodeBuffered serializes a node object into a SBHPF node. +// It is a wrapper around SerializeNode that temporarily stores the serialized nodes in a seekable memory buffer. +// This provides serialization compatibility with non-seekable writers, at the cost of increased memory usage. +func SerializeNodeBuffered(w io.Writer, node *Node) error { + buf := &seekablebuffer.Buffer{} + if err := SerializeNode(buf, node); err != nil { + return err + } + + _, err := io.Copy(w, buf) + return err +} + +// SerializeBuffered serializes a root node object into a complete SBHPF tree. +// It is a wrapper around Serialize that temporarily stores the serialized data in a seekable memory buffer. +// This provides serialization compatibility with non-seekable writers, at the cost of increased memory usage. +func SerializeBuffered(w io.Writer, node *Node) error { + buf := &seekablebuffer.Buffer{} + if err := Serialize(buf, node); err != nil { + return err + } + + _, err := io.Copy(w, buf) + return err +} diff --git a/SBHPFv1/serializer.go b/SBHPFv1/serializer.go index ebce352..059921a 100644 --- a/SBHPFv1/serializer.go +++ b/SBHPFv1/serializer.go @@ -6,8 +6,9 @@ import ( "io" ) -// Serialize writes a full binary property file from a root node object. +// Serialize serializes a root node object into a complete SBHPF tree. // This includes the start headers necessary for proper deserialization of raw files. +// NOTE: Writer must support seeking. If this is not possible, use the SerializeBuffered wrapper. func Serialize(w io.Writer, root *Node) error { // Write file header (version + feature flag). if err := binary.Write(w, binary.LittleEndian, uint8(FormatVersion)); err != nil { @@ -22,7 +23,7 @@ func Serialize(w io.Writer, root *Node) error { } // SerializeNode serializes a node object into a SBHPF node. -// NOTE: Writer must support seeking. If this is not possible, use SerializeNodeStream. +// NOTE: Writer must support seeking. If this is not possible, use the SerializeNodeBuffered wrapper. func SerializeNode(w io.Writer, node *Node) error { // Reserve space for node size ("Node Size" node header). sizePos := getWriterPosition(w) // Save current position. diff --git a/SBHPFv1/streamed_serializer.go b/SBHPFv1/streamed_serializer.go deleted file mode 100644 index f6d48e8..0000000 --- a/SBHPFv1/streamed_serializer.go +++ /dev/null @@ -1,20 +0,0 @@ -package sbhpfv1 - -import ( - "io" - - "git.zervo.org/FLUX/GoSBHPF/pkg/seekablebuffer" -) - -// SerializeNodeStream serializes a node object into a SBHPF node. -// It is a wrapper around SerializeNode that temporarily stores the serialized nodes in a seekable memory buffer. -// This is less efficient, but provides compatibility with writers that lack seeking support. -func SerializeNodeStream(w io.Writer, node *Node) error { - buf := &seekablebuffer.Buffer{} - if err := SerializeNode(buf, node); err != nil { - return err - } - - _, err := io.Copy(w, buf) - return err -}