aboutsummaryrefslogtreecommitdiffstats
path: root/pktline/encoder.go
diff options
context:
space:
mode:
authorMáximo Cuadros Ortiz <mcuadros@gmail.com>2015-04-05 23:34:43 +0200
committerMáximo Cuadros Ortiz <mcuadros@gmail.com>2015-04-06 04:12:04 +0200
commit5d7303c49ac984a9fec60523f2d5297682e16646 (patch)
tree53ac3a7eae7e271e58cc37ab1b7d2c27f3f2a9e5 /pktline/encoder.go
downloadgo-git-5d7303c49ac984a9fec60523f2d5297682e16646.tar.gz
some refactor in folders and crawler
Diffstat (limited to 'pktline/encoder.go')
-rw-r--r--pktline/encoder.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/pktline/encoder.go b/pktline/encoder.go
new file mode 100644
index 0000000..b439c54
--- /dev/null
+++ b/pktline/encoder.go
@@ -0,0 +1,56 @@
+package pktline
+
+import (
+ "errors"
+ "fmt"
+ "strings"
+)
+
+var (
+ ErrOverflow = errors.New("unexepected string length")
+)
+
+type Encoder struct {
+ lines []string
+}
+
+func NewEncoder() *Encoder {
+ return &Encoder{make([]string, 0)}
+}
+
+func (e *Encoder) AddLine(line string) error {
+ le, err := EncodeFromString(line + "\n")
+ if err != nil {
+ return err
+ }
+
+ e.lines = append(e.lines, le)
+ return nil
+}
+
+func (e *Encoder) AddFlush() {
+ e.lines = append(e.lines, "0000")
+}
+
+func (e *Encoder) GetReader() *strings.Reader {
+ data := strings.Join(e.lines, "")
+
+ return strings.NewReader(data)
+}
+
+func EncodeFromString(line string) (string, error) {
+ return Encode([]byte(line))
+}
+
+func Encode(line []byte) (string, error) {
+ if line == nil {
+ return "0000", nil
+ }
+
+ l := len(line) + HEADER_LENGTH
+ if l > MAX_LENGTH {
+ return "", ErrOverflow
+ }
+
+ return fmt.Sprintf("%04x%s", l, line), nil
+}