1
0
mirror of https://github.com/Mrs4s/MiraiGo.git synced 2025-05-04 19:17:38 +08:00

feat(jce): new api *JceReader.ReadBytes.

This commit is contained in:
wdvxdr 2021-08-13 15:29:07 +08:00
parent 3ed9b8d698
commit 1abbeb148a
No known key found for this signature in database
GPG Key ID: 703F8C071DE7A1B6
2 changed files with 33 additions and 0 deletions

View File

@ -276,6 +276,27 @@ func (r *JceReader) ReadString(tag int) string {
}
}
func (r *JceReader) ReadBytes(tag int) []byte {
if !r.skipToTag(tag) {
return nil
}
hd, _ := r.readHead()
switch hd.Type {
case 9:
s := r.ReadInt32(0)
b := make([]byte, s)
for i := 0; i < int(s); i++ {
b[i] = r.ReadByte(0)
}
return b
case 13:
r.readHead()
return r.readBytes(int(r.ReadInt32(0)))
default:
return nil
}
}
// ReadAny Read any type via tag, unsupported JceStruct
func (r *JceReader) ReadAny(tag int) interface{} {
if !r.skipToTag(tag) {

View File

@ -81,3 +81,15 @@ func BenchmarkRequestDataVersion2_ReadFrom(b *testing.B) {
result.ReadFrom(NewJceReader(src))
}
}
func TestJceReader_ReadBytes(t *testing.T) {
b := make([]byte, 1024)
rand.Read(b)
w := NewJceWriter()
w.WriteBytes(b, 0)
r := NewJceReader(w.Bytes())
rb := r.ReadBytes(0)
assert.Equal(t, b, rb)
}