You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

687 lines
17 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package binary
  2. import (
  3. "encoding/hex"
  4. "encoding/json"
  5. "errors"
  6. "io"
  7. "reflect"
  8. "sync"
  9. "time"
  10. . "github.com/tendermint/tendermint/common"
  11. )
  12. type TypeInfo struct {
  13. Type reflect.Type // The type
  14. // Custom encoder/decoder
  15. // NOTE: Not used.
  16. BinaryEncoder Encoder
  17. BinaryDecoder Decoder
  18. // If Type is kind reflect.Interface
  19. ConcreteTypes map[byte]reflect.Type
  20. // If Type is concrete
  21. HasTypeByte bool
  22. TypeByte byte
  23. }
  24. // e.g. If o is struct{Foo}{}, return is the Foo interface type.
  25. func GetTypeFromStructDeclaration(o interface{}) reflect.Type {
  26. rt := reflect.TypeOf(o)
  27. if rt.NumField() != 1 {
  28. panic("Unexpected number of fields in struct-wrapped declaration of type")
  29. }
  30. return rt.Field(0).Type
  31. }
  32. // If o implements HasTypeByte, returns (true, typeByte)
  33. func GetTypeByteFromStruct(o interface{}) (hasTypeByte bool, typeByte byte) {
  34. if _, ok := o.(HasTypeByte); ok {
  35. return true, o.(HasTypeByte).TypeByte()
  36. } else {
  37. return false, byte(0x00)
  38. }
  39. }
  40. // Predeclaration of common types
  41. var (
  42. timeType = GetTypeFromStructDeclaration(struct{ time.Time }{})
  43. )
  44. const (
  45. rfc2822 = "Mon Jan 02 15:04:05 -0700 2006"
  46. )
  47. // If a type implements TypeByte, the byte is included
  48. // as the first byte for encoding and decoding.
  49. // This is primarily used to encode interfaces types.
  50. // The interface should be declared with RegisterInterface()
  51. type HasTypeByte interface {
  52. TypeByte() byte
  53. }
  54. // NOTE: do not access typeInfos directly, but call GetTypeInfo()
  55. var typeInfosMtx sync.Mutex
  56. var typeInfos = map[reflect.Type]*TypeInfo{}
  57. func GetTypeInfo(rt reflect.Type) *TypeInfo {
  58. typeInfosMtx.Lock()
  59. defer typeInfosMtx.Unlock()
  60. info := typeInfos[rt]
  61. if info == nil {
  62. info = &TypeInfo{Type: rt}
  63. RegisterType(info)
  64. }
  65. return info
  66. }
  67. // For use with the RegisterInterface declaration
  68. type ConcreteType struct {
  69. O interface{}
  70. }
  71. // Must use this to register an interface to properly decode the
  72. // underlying concrete type.
  73. func RegisterInterface(o interface{}, args ...interface{}) *TypeInfo {
  74. it := GetTypeFromStructDeclaration(o)
  75. if it.Kind() != reflect.Interface {
  76. panic("RegisterInterface expects an interface")
  77. }
  78. concreteTypes := make(map[byte]reflect.Type, 0)
  79. for _, arg := range args {
  80. switch arg.(type) {
  81. case ConcreteType:
  82. concreteTypeInfo := arg.(ConcreteType)
  83. concreteType := reflect.TypeOf(concreteTypeInfo.O)
  84. hasTypeByte, typeByte := GetTypeByteFromStruct(concreteTypeInfo.O)
  85. //fmt.Println(Fmt("HasTypeByte: %v typeByte: %X type: %X", hasTypeByte, typeByte, concreteType))
  86. if !hasTypeByte {
  87. panic(Fmt("Expected concrete type %v to implement HasTypeByte", concreteType))
  88. }
  89. if concreteTypes[typeByte] != nil {
  90. panic(Fmt("Duplicate TypeByte for type %v and %v", concreteType, concreteTypes[typeByte]))
  91. }
  92. concreteTypes[typeByte] = concreteType
  93. default:
  94. panic(Fmt("Unexpected argument type %v", reflect.TypeOf(arg)))
  95. }
  96. }
  97. typeInfo := &TypeInfo{
  98. Type: it,
  99. ConcreteTypes: concreteTypes,
  100. }
  101. typeInfos[it] = typeInfo
  102. return typeInfo
  103. }
  104. // Registers and possibly modifies the TypeInfo.
  105. // NOTE: not goroutine safe, so only call upon program init.
  106. func RegisterType(info *TypeInfo) *TypeInfo {
  107. // Also register the dereferenced struct if info.Type is a pointer.
  108. // Or, if info.Type is not a pointer, register the pointer.
  109. var rt, ptrRt reflect.Type
  110. if info.Type.Kind() == reflect.Ptr {
  111. rt, ptrRt = info.Type.Elem(), info.Type
  112. } else {
  113. rt, ptrRt = info.Type, reflect.PtrTo(info.Type)
  114. }
  115. // Register the type info
  116. typeInfos[rt] = info
  117. typeInfos[ptrRt] = info
  118. // See if the type implements HasTypeByte
  119. if rt.Kind() != reflect.Interface && rt.Implements(reflect.TypeOf((*HasTypeByte)(nil)).Elem()) {
  120. zero := reflect.Zero(rt)
  121. typeByte := zero.Interface().(HasTypeByte).TypeByte()
  122. if info.HasTypeByte && info.TypeByte != typeByte {
  123. panic(Fmt("Type %v expected TypeByte of %X", rt, typeByte))
  124. } else {
  125. info.HasTypeByte = true
  126. info.TypeByte = typeByte
  127. }
  128. } else if ptrRt.Implements(reflect.TypeOf((*HasTypeByte)(nil)).Elem()) {
  129. zero := reflect.Zero(ptrRt)
  130. typeByte := zero.Interface().(HasTypeByte).TypeByte()
  131. if info.HasTypeByte && info.TypeByte != typeByte {
  132. panic(Fmt("Type %v expected TypeByte of %X", ptrRt, typeByte))
  133. } else {
  134. info.HasTypeByte = true
  135. info.TypeByte = typeByte
  136. }
  137. }
  138. return info
  139. }
  140. func readReflect(rv reflect.Value, rt reflect.Type, r io.Reader, n *int64, err *error) {
  141. log.Debug("Read reflect", "type", rt)
  142. // Get typeInfo
  143. typeInfo := GetTypeInfo(rt)
  144. // Custom decoder
  145. if typeInfo.BinaryDecoder != nil {
  146. decoded := typeInfo.BinaryDecoder(r, n, err)
  147. rv.Set(reflect.ValueOf(decoded))
  148. return
  149. }
  150. // Create a new struct if rv is nil pointer.
  151. if rt.Kind() == reflect.Ptr && rv.IsNil() {
  152. newRv := reflect.New(rt.Elem())
  153. rv.Set(newRv)
  154. rv = newRv
  155. }
  156. // Dereference pointer
  157. // Still addressable, thus settable!
  158. if rv.Kind() == reflect.Ptr {
  159. rv, rt = rv.Elem(), rt.Elem()
  160. }
  161. // Read TypeByte prefix
  162. if typeInfo.HasTypeByte {
  163. typeByte := ReadByte(r, n, err)
  164. log.Debug("Read typebyte", "typeByte", typeByte)
  165. if typeByte != typeInfo.TypeByte {
  166. *err = errors.New(Fmt("Expected TypeByte of %X but got %X", typeInfo.TypeByte, typeByte))
  167. return
  168. }
  169. }
  170. switch rt.Kind() {
  171. case reflect.Interface:
  172. typeByte := ReadByte(r, n, err)
  173. if *err != nil {
  174. return
  175. }
  176. concreteType, ok := typeInfo.ConcreteTypes[typeByte]
  177. if !ok {
  178. panic(Fmt("TypeByte %X not registered for interface %v", typeByte, rt))
  179. }
  180. newRv := reflect.New(concreteType)
  181. readReflect(newRv.Elem(), concreteType, NewPrefixedReader([]byte{typeByte}, r), n, err)
  182. rv.Set(newRv.Elem())
  183. case reflect.Slice:
  184. elemRt := rt.Elem()
  185. if elemRt.Kind() == reflect.Uint8 {
  186. // Special case: Byteslices
  187. byteslice := ReadByteSlice(r, n, err)
  188. log.Debug("Read byteslice", "bytes", byteslice)
  189. rv.Set(reflect.ValueOf(byteslice))
  190. } else {
  191. // Read length
  192. length := int(ReadUvarint(r, n, err))
  193. log.Debug(Fmt("Read length: %v", length))
  194. sliceRv := reflect.MakeSlice(rt, length, length)
  195. // Read elems
  196. for i := 0; i < length; i++ {
  197. elemRv := sliceRv.Index(i)
  198. readReflect(elemRv, elemRt, r, n, err)
  199. }
  200. rv.Set(sliceRv)
  201. }
  202. case reflect.Struct:
  203. if rt == timeType {
  204. // Special case: time.Time
  205. t := ReadTime(r, n, err)
  206. log.Debug(Fmt("Read time: %v", t))
  207. rv.Set(reflect.ValueOf(t))
  208. } else {
  209. numFields := rt.NumField()
  210. for i := 0; i < numFields; i++ {
  211. field := rt.Field(i)
  212. if field.PkgPath != "" {
  213. continue
  214. }
  215. fieldRv := rv.Field(i)
  216. readReflect(fieldRv, field.Type, r, n, err)
  217. }
  218. }
  219. case reflect.String:
  220. str := ReadString(r, n, err)
  221. log.Debug(Fmt("Read string: %v", str))
  222. rv.SetString(str)
  223. case reflect.Int64:
  224. num := ReadUint64(r, n, err)
  225. log.Debug(Fmt("Read num: %v", num))
  226. rv.SetInt(int64(num))
  227. case reflect.Int32:
  228. num := ReadUint32(r, n, err)
  229. log.Debug(Fmt("Read num: %v", num))
  230. rv.SetInt(int64(num))
  231. case reflect.Int16:
  232. num := ReadUint16(r, n, err)
  233. log.Debug(Fmt("Read num: %v", num))
  234. rv.SetInt(int64(num))
  235. case reflect.Int8:
  236. num := ReadUint8(r, n, err)
  237. log.Debug(Fmt("Read num: %v", num))
  238. rv.SetInt(int64(num))
  239. case reflect.Int:
  240. num := ReadUvarint(r, n, err)
  241. log.Debug(Fmt("Read num: %v", num))
  242. rv.SetInt(int64(num))
  243. case reflect.Uint64:
  244. num := ReadUint64(r, n, err)
  245. log.Debug(Fmt("Read num: %v", num))
  246. rv.SetUint(uint64(num))
  247. case reflect.Uint32:
  248. num := ReadUint32(r, n, err)
  249. log.Debug(Fmt("Read num: %v", num))
  250. rv.SetUint(uint64(num))
  251. case reflect.Uint16:
  252. num := ReadUint16(r, n, err)
  253. log.Debug(Fmt("Read num: %v", num))
  254. rv.SetUint(uint64(num))
  255. case reflect.Uint8:
  256. num := ReadUint8(r, n, err)
  257. log.Debug(Fmt("Read num: %v", num))
  258. rv.SetUint(uint64(num))
  259. case reflect.Uint:
  260. num := ReadUvarint(r, n, err)
  261. log.Debug(Fmt("Read num: %v", num))
  262. rv.SetUint(uint64(num))
  263. default:
  264. panic(Fmt("Unknown field type %v", rt.Kind()))
  265. }
  266. }
  267. func writeReflect(rv reflect.Value, rt reflect.Type, w io.Writer, n *int64, err *error) {
  268. // Get typeInfo
  269. typeInfo := GetTypeInfo(rt)
  270. // Custom encoder, say for an interface type rt.
  271. if typeInfo.BinaryEncoder != nil {
  272. typeInfo.BinaryEncoder(rv.Interface(), w, n, err)
  273. return
  274. }
  275. // Dereference interface
  276. if rt.Kind() == reflect.Interface {
  277. rv = rv.Elem()
  278. rt = rv.Type()
  279. // If interface type, get typeInfo of underlying type.
  280. typeInfo = GetTypeInfo(rt)
  281. }
  282. // Dereference pointer
  283. if rt.Kind() == reflect.Ptr {
  284. rt = rt.Elem()
  285. rv = rv.Elem()
  286. }
  287. // Write TypeByte prefix
  288. if typeInfo.HasTypeByte {
  289. WriteByte(typeInfo.TypeByte, w, n, err)
  290. }
  291. switch rt.Kind() {
  292. case reflect.Slice:
  293. elemRt := rt.Elem()
  294. if elemRt.Kind() == reflect.Uint8 {
  295. // Special case: Byteslices
  296. byteslice := rv.Bytes()
  297. WriteByteSlice(byteslice, w, n, err)
  298. } else {
  299. // Write length
  300. length := rv.Len()
  301. WriteUvarint(uint(length), w, n, err)
  302. // Write elems
  303. for i := 0; i < length; i++ {
  304. elemRv := rv.Index(i)
  305. writeReflect(elemRv, elemRt, w, n, err)
  306. }
  307. }
  308. case reflect.Struct:
  309. if rt == timeType {
  310. // Special case: time.Time
  311. WriteTime(rv.Interface().(time.Time), w, n, err)
  312. } else {
  313. numFields := rt.NumField()
  314. for i := 0; i < numFields; i++ {
  315. field := rt.Field(i)
  316. if field.PkgPath != "" {
  317. continue
  318. }
  319. fieldRv := rv.Field(i)
  320. writeReflect(fieldRv, field.Type, w, n, err)
  321. }
  322. }
  323. case reflect.String:
  324. WriteString(rv.String(), w, n, err)
  325. case reflect.Int64:
  326. WriteInt64(rv.Int(), w, n, err)
  327. case reflect.Int32:
  328. WriteInt32(int32(rv.Int()), w, n, err)
  329. case reflect.Int16:
  330. WriteInt16(int16(rv.Int()), w, n, err)
  331. case reflect.Int8:
  332. WriteInt8(int8(rv.Int()), w, n, err)
  333. case reflect.Int:
  334. WriteVarint(int(rv.Int()), w, n, err)
  335. case reflect.Uint64:
  336. WriteUint64(rv.Uint(), w, n, err)
  337. case reflect.Uint32:
  338. WriteUint32(uint32(rv.Uint()), w, n, err)
  339. case reflect.Uint16:
  340. WriteUint16(uint16(rv.Uint()), w, n, err)
  341. case reflect.Uint8:
  342. WriteUint8(uint8(rv.Uint()), w, n, err)
  343. case reflect.Uint:
  344. WriteUvarint(uint(rv.Uint()), w, n, err)
  345. default:
  346. panic(Fmt("Unknown field type %v", rt.Kind()))
  347. }
  348. }
  349. //-----------------------------------------------------------------------------
  350. func readTypeByteJSON(o interface{}) (typeByte byte, rest interface{}, err error) {
  351. oSlice, ok := o.([]interface{})
  352. if !ok {
  353. err = errors.New(Fmt("Expected type [TypeByte,?] but got type %v", reflect.TypeOf(o)))
  354. return
  355. }
  356. if len(oSlice) != 2 {
  357. err = errors.New(Fmt("Expected [TypeByte,?] len 2 but got len %v", len(oSlice)))
  358. return
  359. }
  360. typeByte_, ok := oSlice[0].(float64)
  361. typeByte = byte(typeByte_)
  362. rest = oSlice[1]
  363. return
  364. }
  365. func readReflectJSON(rv reflect.Value, rt reflect.Type, o interface{}, err *error) {
  366. log.Debug("Read reflect json", "type", rt)
  367. // Get typeInfo
  368. typeInfo := GetTypeInfo(rt)
  369. // Create a new struct if rv is nil pointer.
  370. if rt.Kind() == reflect.Ptr && rv.IsNil() {
  371. newRv := reflect.New(rt.Elem())
  372. rv.Set(newRv)
  373. rv = newRv
  374. }
  375. // Dereference pointer
  376. // Still addressable, thus settable!
  377. if rv.Kind() == reflect.Ptr {
  378. rv, rt = rv.Elem(), rt.Elem()
  379. }
  380. // Read TypeByte prefix
  381. if typeInfo.HasTypeByte {
  382. typeByte, rest, err_ := readTypeByteJSON(o)
  383. if err_ != nil {
  384. *err = err_
  385. return
  386. }
  387. if typeByte != typeInfo.TypeByte {
  388. *err = errors.New(Fmt("Expected TypeByte of %X but got %X", typeInfo.TypeByte, byte(typeByte)))
  389. return
  390. }
  391. o = rest
  392. }
  393. switch rt.Kind() {
  394. case reflect.Interface:
  395. typeByte, _, err_ := readTypeByteJSON(o)
  396. if err_ != nil {
  397. *err = err_
  398. return
  399. }
  400. concreteType, ok := typeInfo.ConcreteTypes[typeByte]
  401. if !ok {
  402. panic(Fmt("TypeByte %X not registered for interface %v", typeByte, rt))
  403. }
  404. newRv := reflect.New(concreteType)
  405. readReflectJSON(newRv.Elem(), concreteType, o, err)
  406. rv.Set(newRv.Elem())
  407. case reflect.Slice:
  408. elemRt := rt.Elem()
  409. if elemRt.Kind() == reflect.Uint8 {
  410. // Special case: Byteslices
  411. oString, ok := o.(string)
  412. if !ok {
  413. *err = errors.New(Fmt("Expected string but got type %v", reflect.TypeOf(o)))
  414. return
  415. }
  416. byteslice, err_ := hex.DecodeString(oString)
  417. if err_ != nil {
  418. *err = err_
  419. return
  420. }
  421. log.Debug("Read byteslice", "bytes", byteslice)
  422. rv.Set(reflect.ValueOf(byteslice))
  423. } else {
  424. // Read length
  425. oSlice, ok := o.([]interface{})
  426. if !ok {
  427. *err = errors.New(Fmt("Expected array but got type %v", reflect.TypeOf(o)))
  428. return
  429. }
  430. length := len(oSlice)
  431. log.Debug(Fmt("Read length: %v", length))
  432. sliceRv := reflect.MakeSlice(rt, length, length)
  433. // Read elems
  434. for i := 0; i < length; i++ {
  435. elemRv := sliceRv.Index(i)
  436. readReflectJSON(elemRv, elemRt, oSlice[i], err)
  437. }
  438. rv.Set(sliceRv)
  439. }
  440. case reflect.Struct:
  441. if rt == timeType {
  442. // Special case: time.Time
  443. str, ok := o.(string)
  444. if !ok {
  445. *err = errors.New(Fmt("Expected string but got type %v", reflect.TypeOf(o)))
  446. return
  447. }
  448. log.Debug(Fmt("Read time: %v", str))
  449. t, err_ := time.Parse(rfc2822, str)
  450. if err_ != nil {
  451. *err = err_
  452. return
  453. }
  454. rv.Set(reflect.ValueOf(t))
  455. } else {
  456. oMap, ok := o.(map[string]interface{})
  457. if !ok {
  458. *err = errors.New(Fmt("Expected map but got type %v", reflect.TypeOf(o)))
  459. return
  460. }
  461. // TODO: ensure that all fields are set?
  462. for name, value := range oMap {
  463. field, ok := rt.FieldByName(name)
  464. if !ok {
  465. *err = errors.New(Fmt("Attempt to set unknown field %v", field.Name))
  466. return
  467. }
  468. // JAE: I don't think golang reflect lets us set unexported fields, but just in case:
  469. if field.PkgPath != "" {
  470. *err = errors.New(Fmt("Attempt to set unexported field %v", field.Name))
  471. return
  472. }
  473. fieldRv := rv.FieldByName(name)
  474. readReflectJSON(fieldRv, field.Type, value, err)
  475. }
  476. }
  477. case reflect.String:
  478. str, ok := o.(string)
  479. if !ok {
  480. *err = errors.New(Fmt("Expected string but got type %v", reflect.TypeOf(o)))
  481. return
  482. }
  483. log.Debug(Fmt("Read string: %v", str))
  484. rv.SetString(str)
  485. case reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8, reflect.Int:
  486. num, ok := o.(float64)
  487. if !ok {
  488. *err = errors.New(Fmt("Expected numeric but got type %v", reflect.TypeOf(o)))
  489. return
  490. }
  491. log.Debug(Fmt("Read num: %v", num))
  492. rv.SetInt(int64(num))
  493. case reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8, reflect.Uint:
  494. num, ok := o.(float64)
  495. if !ok {
  496. *err = errors.New(Fmt("Expected numeric but got type %v", reflect.TypeOf(o)))
  497. return
  498. }
  499. if num < 0 {
  500. *err = errors.New(Fmt("Expected unsigned numeric but got %v", num))
  501. return
  502. }
  503. log.Debug(Fmt("Read num: %v", num))
  504. rv.SetUint(uint64(num))
  505. default:
  506. panic(Fmt("Unknown field type %v", rt.Kind()))
  507. }
  508. }
  509. func writeReflectJSON(rv reflect.Value, rt reflect.Type, w io.Writer, n *int64, err *error) {
  510. // Get typeInfo
  511. typeInfo := GetTypeInfo(rt)
  512. // Dereference interface
  513. if rt.Kind() == reflect.Interface {
  514. rv = rv.Elem()
  515. rt = rv.Type()
  516. // If interface type, get typeInfo of underlying type.
  517. typeInfo = GetTypeInfo(rt)
  518. }
  519. // Dereference pointer
  520. if rt.Kind() == reflect.Ptr {
  521. rt = rt.Elem()
  522. rv = rv.Elem()
  523. }
  524. // Write TypeByte prefix
  525. if typeInfo.HasTypeByte {
  526. WriteTo([]byte(Fmt("[%v,", typeInfo.TypeByte)), w, n, err)
  527. }
  528. switch rt.Kind() {
  529. case reflect.Slice:
  530. elemRt := rt.Elem()
  531. if elemRt.Kind() == reflect.Uint8 {
  532. // Special case: Byteslices
  533. byteslice := rv.Bytes()
  534. WriteTo([]byte(Fmt("\"%X\"", byteslice)), w, n, err)
  535. //WriteByteSlice(byteslice, w, n, err)
  536. } else {
  537. WriteTo([]byte("["), w, n, err)
  538. // Write elems
  539. length := rv.Len()
  540. for i := 0; i < length; i++ {
  541. elemRv := rv.Index(i)
  542. writeReflectJSON(elemRv, elemRt, w, n, err)
  543. if i < length-1 {
  544. WriteTo([]byte(","), w, n, err)
  545. }
  546. }
  547. WriteTo([]byte("]"), w, n, err)
  548. }
  549. case reflect.Struct:
  550. if rt == timeType {
  551. // Special case: time.Time
  552. t := rv.Interface().(time.Time)
  553. str := t.Format(rfc2822)
  554. jsonBytes, err_ := json.Marshal(str)
  555. if err_ != nil {
  556. *err = err_
  557. return
  558. }
  559. WriteTo(jsonBytes, w, n, err)
  560. } else {
  561. WriteTo([]byte("{"), w, n, err)
  562. numFields := rt.NumField()
  563. wroteField := false
  564. for i := 0; i < numFields; i++ {
  565. field := rt.Field(i)
  566. if field.PkgPath != "" {
  567. continue
  568. }
  569. fieldRv := rv.Field(i)
  570. if wroteField {
  571. WriteTo([]byte(","), w, n, err)
  572. } else {
  573. wroteField = true
  574. }
  575. WriteTo([]byte(Fmt("\"%v\":", field.Name)), w, n, err)
  576. writeReflectJSON(fieldRv, field.Type, w, n, err)
  577. }
  578. WriteTo([]byte("}"), w, n, err)
  579. }
  580. case reflect.String:
  581. fallthrough
  582. case reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8, reflect.Uint:
  583. fallthrough
  584. case reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8, reflect.Int:
  585. jsonBytes, err_ := json.Marshal(rv.Interface())
  586. if err_ != nil {
  587. *err = err_
  588. return
  589. }
  590. WriteTo(jsonBytes, w, n, err)
  591. default:
  592. panic(Fmt("Unknown field type %v", rt.Kind()))
  593. }
  594. // Write TypeByte close bracket
  595. if typeInfo.HasTypeByte {
  596. WriteTo([]byte("]"), w, n, err)
  597. }
  598. }
  599. //-----------------------------------------------------------------------------