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.

48 lines
969 B

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. "io"
  4. )
  5. func WriteByteSlice(bz []byte, w io.Writer, n *int64, err *error) {
  6. WriteUvarint(uint(len(bz)), w, n, err)
  7. WriteTo(bz, w, n, err)
  8. }
  9. func ReadByteSlice(r io.Reader, n *int64, err *error) []byte {
  10. length := ReadUvarint(r, n, err)
  11. if *err != nil {
  12. return nil
  13. }
  14. buf := make([]byte, int(length))
  15. ReadFull(buf, r, n, err)
  16. return buf
  17. }
  18. //-----------------------------------------------------------------------------
  19. func WriteByteSlices(bzz [][]byte, w io.Writer, n *int64, err *error) {
  20. WriteUvarint(uint(len(bzz)), w, n, err)
  21. for _, bz := range bzz {
  22. WriteByteSlice(bz, w, n, err)
  23. if *err != nil {
  24. return
  25. }
  26. }
  27. }
  28. func ReadByteSlices(r io.Reader, n *int64, err *error) [][]byte {
  29. length := ReadUvarint(r, n, err)
  30. if *err != nil {
  31. return nil
  32. }
  33. bzz := make([][]byte, length)
  34. for i := uint(0); i < length; i++ {
  35. bz := ReadByteSlice(r, n, err)
  36. if *err != nil {
  37. return nil
  38. }
  39. bzz[i] = bz
  40. }
  41. return bzz
  42. }