-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add stream encoder * docs: update usage.md
- Loading branch information
Showing
5 changed files
with
324 additions
and
15 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
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
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
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,48 @@ | ||
// Copyright 2023 The Fit SDK for Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package encoder | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/muktihari/fit/proto" | ||
) | ||
|
||
// StreamEncoder is one layer above Encoder to enable encoding in streaming fashion. | ||
// This will only valid when the Writer given to the Encoder should either implement io.WriterAt or io.WriteSeeker. | ||
// This can only be created using (*Encoder).StreamEncoder() method. | ||
type StreamEncoder struct { | ||
enc *Encoder | ||
fileHeader proto.FileHeader | ||
fileHeaderWritten bool | ||
} | ||
|
||
// WriteMessage writes message to the writer, it will auto write FileHeader when | ||
// - This method is invoked on the first time of use. | ||
// - This method is called right after SequenceCompleted method has been called. | ||
func (e *StreamEncoder) WriteMessage(mesg *proto.Message) error { | ||
if !e.fileHeaderWritten { | ||
e.fileHeader = e.enc.defaultFileHeader | ||
if err := e.enc.encodeHeader(&e.fileHeader); err != nil { | ||
return err | ||
} | ||
e.fileHeaderWritten = true | ||
} | ||
return e.enc.encodeMessage(e.enc.w, mesg) | ||
} | ||
|
||
// SequenceCompleted finalises the FIT File by updating its FileHeader's DataSize & CRC, as well as the File's CRC. | ||
// This will also reset variables so that the StreamEncoder can be used for the next sequence of FIT file. | ||
func (e *StreamEncoder) SequenceCompleted() error { | ||
if err := e.enc.encodeCRC(); err != nil { | ||
return fmt.Errorf("could not encode crc: %w", err) | ||
} | ||
if err := e.enc.updateHeader(&e.fileHeader); err != nil { | ||
return fmt.Errorf("could not update header: %w", err) | ||
} | ||
e.fileHeaderWritten = false | ||
e.enc.reset() | ||
return nil | ||
} |
Oops, something went wrong.