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.

58 lines
1.1 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
10 years ago
  1. package binary
  2. import (
  3. "io"
  4. )
  5. func WriteByteSlice(bz []byte, w io.Writer, n *int64, err *error) {
  6. WriteVarint(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 := ReadVarint(r, n, err)
  11. if *err != nil {
  12. return nil
  13. }
  14. if MaxBinaryReadSize < *n+int64(length) {
  15. *err = ErrMaxBinaryReadSizeReached
  16. return nil
  17. }
  18. buf := make([]byte, length)
  19. ReadFull(buf, r, n, err)
  20. return buf
  21. }
  22. //-----------------------------------------------------------------------------
  23. func WriteByteSlices(bzz [][]byte, w io.Writer, n *int64, err *error) {
  24. WriteVarint(len(bzz), w, n, err)
  25. for _, bz := range bzz {
  26. WriteByteSlice(bz, w, n, err)
  27. if *err != nil {
  28. return
  29. }
  30. }
  31. }
  32. func ReadByteSlices(r io.Reader, n *int64, err *error) [][]byte {
  33. length := ReadVarint(r, n, err)
  34. if *err != nil {
  35. return nil
  36. }
  37. if MaxBinaryReadSize < *n+int64(length) {
  38. *err = ErrMaxBinaryReadSizeReached
  39. return nil
  40. }
  41. bzz := make([][]byte, length)
  42. for i := 0; i < length; i++ {
  43. bz := ReadByteSlice(r, n, err)
  44. if *err != nil {
  45. return nil
  46. }
  47. bzz[i] = bz
  48. }
  49. return bzz
  50. }