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
971 B

11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
  1. package binary
  2. import (
  3. "io"
  4. )
  5. func WriteByteSlice(w io.Writer, bz []byte, n *int64, err *error) {
  6. WriteUInt32(w, uint32(len(bz)), n, err)
  7. WriteTo(w, bz, n, err)
  8. }
  9. func ReadByteSlice(r io.Reader, n *int64, err *error) []byte {
  10. length := ReadUInt32(r, n, err)
  11. if *err != nil {
  12. return nil
  13. }
  14. buf := make([]byte, int(length))
  15. ReadFull(r, buf, n, err)
  16. return buf
  17. }
  18. //-----------------------------------------------------------------------------
  19. func WriteByteSlices(w io.Writer, bzz [][]byte, n *int64, err *error) {
  20. WriteUInt32(w, uint32(len(bzz)), n, err)
  21. for _, bz := range bzz {
  22. WriteByteSlice(w, bz, n, err)
  23. if *err != nil {
  24. return
  25. }
  26. }
  27. }
  28. func ReadByteSlices(r io.Reader, n *int64, err *error) [][]byte {
  29. length := ReadUInt32(r, n, err)
  30. if *err != nil {
  31. return nil
  32. }
  33. bzz := make([][]byte, length)
  34. for i := uint32(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. }