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.

62 lines
1.3 KiB

11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package binary
  2. import (
  3. . "github.com/tendermint/tendermint/common"
  4. "io"
  5. )
  6. const (
  7. ByteSliceChunk = 1024
  8. )
  9. func WriteByteSlice(bz []byte, w io.Writer, n *int64, err *error) {
  10. WriteUvarint(uint(len(bz)), w, n, err)
  11. WriteTo(bz, w, n, err)
  12. }
  13. func ReadByteSlice(r io.Reader, n *int64, err *error) []byte {
  14. length := int(ReadUvarint(r, n, err))
  15. if *err != nil {
  16. return nil
  17. }
  18. var buf, tmpBuf []byte
  19. // read one ByteSliceChunk at a time and append
  20. for i := 0; i*ByteSliceChunk < length; i++ {
  21. tmpBuf = make([]byte, MinInt(ByteSliceChunk, length-i*ByteSliceChunk))
  22. ReadFull(tmpBuf, r, n, err)
  23. if *err != nil {
  24. return nil
  25. }
  26. buf = append(buf, tmpBuf...)
  27. }
  28. return buf
  29. }
  30. //-----------------------------------------------------------------------------
  31. func WriteByteSlices(bzz [][]byte, w io.Writer, n *int64, err *error) {
  32. WriteUvarint(uint(len(bzz)), w, n, err)
  33. for _, bz := range bzz {
  34. WriteByteSlice(bz, w, n, err)
  35. if *err != nil {
  36. return
  37. }
  38. }
  39. }
  40. func ReadByteSlices(r io.Reader, n *int64, err *error) [][]byte {
  41. length := ReadUvarint(r, n, err)
  42. if *err != nil {
  43. return nil
  44. }
  45. bzz := make([][]byte, length)
  46. for i := uint(0); i < length; i++ {
  47. bz := ReadByteSlice(r, n, err)
  48. if *err != nil {
  49. return nil
  50. }
  51. bzz[i] = bz
  52. }
  53. return bzz
  54. }