bytes_vec.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package go_zrfu
  2. import (
  3. "io"
  4. "unsafe"
  5. )
  6. var _ = io.ReadWriter(&BytesVec{})
  7. type BytesVec struct {
  8. lib *ZRFUDyLib
  9. h uint64
  10. }
  11. func NewBytesVec(lib *ZRFUDyLib) (*BytesVec, error) {
  12. v := &BytesVec{
  13. lib: lib,
  14. h: 0,
  15. }
  16. var hbuf uint64
  17. hptr := uintptr(unsafe.Pointer(&hbuf))
  18. _, err := lib.procCall(lib.procBVNew, hptr)
  19. if err != nil {
  20. return nil, err
  21. }
  22. v.h = hbuf
  23. return v, nil
  24. }
  25. func (b BytesVec) Read(p []byte) (n int, err error) {
  26. hptr := uintptr(unsafe.Pointer(&b.h))
  27. l := len(p)
  28. bufptr := uintptr(unsafe.Pointer(&p[0]))
  29. r, err := b.lib.procCall(b.lib.procBVRead, hptr, bufptr, uintptr(l))
  30. if err != nil {
  31. return 0, err
  32. }
  33. if r == 0 {
  34. return 0, io.EOF
  35. }
  36. return int(r), nil
  37. }
  38. func (b BytesVec) Write(p []byte) (n int, err error) {
  39. hptr := uintptr(unsafe.Pointer(&b.h))
  40. l := len(p)
  41. bufptr := uintptr(unsafe.Pointer(&p[0]))
  42. r, err := b.lib.procCall(b.lib.procBVWrite, hptr, bufptr, uintptr(l))
  43. if err != nil {
  44. return 0, err
  45. }
  46. return int(r), nil
  47. }
  48. func (b BytesVec) Close() error {
  49. hptr := uintptr(unsafe.Pointer(&b.h))
  50. _, err := b.lib.procCall(b.lib.procBVDel, hptr)
  51. if err != nil {
  52. return err
  53. }
  54. return nil
  55. }
  56. func (b BytesVec) Len() (int, error) {
  57. hptr := uintptr(unsafe.Pointer(&b.h))
  58. l, err := b.lib.procCall(b.lib.procBVLen, hptr)
  59. if err != nil {
  60. return 0, err
  61. }
  62. return int(l), nil
  63. }
  64. func (b BytesVec) Handle() uint64 {
  65. return b.h
  66. }
  67. func (b BytesVec) HandlePtr() uintptr {
  68. return uintptr(unsafe.Pointer(&b.h))
  69. }