forked from hprose/hprose-golang
/
raw_reader.go
164 lines (148 loc) · 3.79 KB
/
raw_reader.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**********************************************************\
| |
| hprose |
| |
| Official WebSite: http://www.hprose.com/ |
| http://www.hprose.org/ |
| |
\**********************************************************/
/**********************************************************\
* *
* io/raw_reader.go *
* *
* hprose raw reader for Go. *
* *
* LastModified: Oct 15, 2016 *
* Author: Ma Bingyao <andot@hprose.com> *
* *
\**********************************************************/
package io
import "errors"
// RawReader is the hprose raw reader
type RawReader struct {
ByteReader
}
// NewRawReader is a constructor for RawReader
func NewRawReader(buf []byte) (reader *RawReader) {
reader = new(RawReader)
reader.buf = buf
return
}
// ReadRaw from stream
func (r *RawReader) ReadRaw() (raw []byte) {
w := new(ByteWriter)
r.ReadRawTo(w)
raw = w.Bytes()
return
}
// ReadRawTo buffer from stream
func (r *RawReader) ReadRawTo(w *ByteWriter) {
r.readRaw(w, r.readByte())
}
func (r *RawReader) readRaw(w *ByteWriter, tag byte) {
w.writeByte(tag)
switch tag {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
TagNull, TagEmpty, TagTrue, TagFalse, TagNaN:
case TagInfinity:
w.writeByte(r.readByte())
case TagInteger, TagLong, TagDouble, TagRef:
r.readNumberRaw(w)
case TagDate, TagTime:
r.readDateTimeRaw(w)
case TagUTF8Char:
r.readUTF8CharRaw(w)
case TagBytes:
r.readBytesRaw(w)
case TagString:
r.readStringRaw(w)
case TagGUID:
r.readGUIDRaw(w)
case TagList, TagMap, TagObject:
r.readComplexRaw(w)
case TagClass:
r.readComplexRaw(w)
r.ReadRawTo(w)
case TagError:
r.ReadRawTo(w)
default:
unexpectedTag(tag, nil)
}
return
}
func (r *RawReader) readNumberRaw(w *ByteWriter) {
for {
tag := r.readByte()
w.writeByte(tag)
if tag == TagSemicolon {
return
}
}
}
func (r *RawReader) readDateTimeRaw(w *ByteWriter) {
for {
tag := r.readByte()
w.writeByte(tag)
if tag == TagSemicolon || tag == TagUTC {
return
}
}
}
func (r *RawReader) readUTF8CharRaw(w *ByteWriter) {
w.write(r.readUTF8Slice(1))
}
func (r *RawReader) readBytesRaw(w *ByteWriter) {
count := 0
tag := byte('0')
for {
count *= 10
count += int(tag - '0')
tag = r.readByte()
w.writeByte(tag)
if tag == TagQuote {
count++
w.write(r.Next(count))
return
}
}
}
func (r *RawReader) readStringRaw(w *ByteWriter) {
count := 0
tag := byte('0')
for {
count *= 10
count += int(tag - '0')
tag = r.readByte()
w.writeByte(tag)
if tag == TagQuote {
w.write(r.readUTF8Slice(count + 1))
return
}
}
}
func (r *RawReader) readGUIDRaw(w *ByteWriter) {
w.write(r.Next(38))
}
func (r *RawReader) readComplexRaw(w *ByteWriter) {
var tag byte
for tag != TagOpenbrace {
tag = r.readByte()
w.writeByte(tag)
}
tag = r.readByte()
for tag != TagClosebrace {
r.readRaw(w, tag)
tag = r.readByte()
}
w.writeByte(tag)
}
// private functions
func unexpectedTag(tag byte, expectTags []byte) {
if tag == 0 {
panic(errors.New("No byte found in stream"))
} else if expectTags == nil {
panic(errors.New("Unexpected serialize tag '" + string(rune(tag)) + "' in stream"))
} else {
panic(errors.New("Tag '" + string(expectTags) + "' expected, but '" + string(rune(tag)) + "' found in stream"))
}
}