-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream.go
144 lines (122 loc) · 2.99 KB
/
stream.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//
// Copyright [2024] [https://github.com/gnolizuh]
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package amf
import (
"errors"
"io"
)
type Reader interface {
io.Reader
io.ByteScanner
Len() int
}
// An Encoder writes AMF values to an output stream.
type Encoder struct {
w io.Writer
v3 bool
err error
}
// NewEncoder returns a new encoder.
func NewEncoder() *Encoder {
return &Encoder{}
}
// WithWriter set Writer.
func (enc *Encoder) WithWriter(w io.Writer) *Encoder {
enc.w = w
return enc
}
// WithVersion set AMF codec version.
func (enc *Encoder) WithVersion(v Version) *Encoder {
enc.v3 = v == Version3
return enc
}
// Encode writes the AMF encoding of v to the stream,
// followed by a newline character.
//
// See the documentation for Marshal for details about the
// conversion of Go values to AMF.
func (enc *Encoder) Encode(vs ...any) error {
if enc.err != nil {
return enc.err
}
if enc.w == nil {
return errors.New("writer must be set")
}
e := newEncodeState()
defer encodeStatePool.Put(e)
var err error
for _, v := range vs {
err := e.marshal(v, encOpts{v3: enc.v3})
if err != nil {
return err
}
}
b := e.Bytes()
if _, err = enc.w.Write(b); err != nil {
enc.err = err
return err
}
return err
}
// A Decoder reads and decodes AMF values from an input stream.
type Decoder struct {
r Reader
v3 bool
buf []byte
d decodeState
err error
}
// NewDecoder returns a new decoder that reads from r.
//
// The decoder introduces its own buffering and may
// read data from r beyond the AMF values requested.
func NewDecoder() *Decoder {
return &Decoder{}
}
// WithReader set Reader.
func (dec *Decoder) WithReader(r Reader) *Decoder {
dec.r = r
return dec
}
// WithVersion set AMF codec version.
func (dec *Decoder) WithVersion(v Version) *Decoder {
dec.v3 = v == Version3
return dec
}
// Decode reads the next AMF-encoded value from its
// input and stores it in the value pointed to by v.
//
// See the documentation for Unmarshal for details about
// the conversion of AMF into a Go value.
func (dec *Decoder) Decode(vs ...any) error {
if dec.err != nil {
return dec.err
}
if dec.r == nil {
return errors.New("reader must be set")
}
var err error
dec.d.init(dec.r, dec.v3)
// Don't save err from unmarshal into dec.err:
// the connection is still usable since we read a complete AMF
// object from it before the error happened.
for _, v := range vs {
if err = dec.d.unmarshal(v, decOpts{v3: dec.v3}); err != nil {
return err
}
}
return nil
}