AutoReleaseBuffer.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using Best.HTTP.Shared.PlatformSupport.Text;
  3. namespace Best.HTTP.Shared.PlatformSupport.Memory
  4. {
  5. [Best.HTTP.Shared.PlatformSupport.IL2CPP.Il2CppEagerStaticClassConstructionAttribute]
  6. public struct AutoReleaseBuffer : IDisposable
  7. {
  8. public static readonly AutoReleaseBuffer Empty = new AutoReleaseBuffer(null);
  9. public byte[] Data;
  10. public int Offset;
  11. public int Count;
  12. public AutoReleaseBuffer(byte[] data)
  13. {
  14. this.Data = data;
  15. this.Offset = 0;
  16. this.Count = data != null ? data.Length : 0;
  17. }
  18. public AutoReleaseBuffer(BufferSegment segment)
  19. {
  20. this.Data = segment.Data;
  21. this.Offset = segment.Offset;
  22. this.Count = segment.Count;
  23. }
  24. public AutoReleaseBuffer(byte[] data, int offset, int count)
  25. {
  26. this.Data = data;
  27. this.Offset = offset;
  28. this.Count = count;
  29. }
  30. public BufferSegment Slice(int newOffset) => new BufferSegment(this.Data, newOffset, this.Count - (newOffset - this.Offset));
  31. public BufferSegment Slice(int offset, int count) => new BufferSegment(this.Data, offset, count);
  32. public override bool Equals(object obj)
  33. {
  34. if (obj == null || !(obj is BufferSegment))
  35. return false;
  36. return Equals((BufferSegment)obj);
  37. }
  38. public bool Equals(BufferSegment other) => this.Data == other.Data && this.Offset == other.Offset && this.Count == other.Count;
  39. public bool Equals(AutoReleaseBuffer other) => this.Data == other.Data && this.Offset == other.Offset && this.Count == other.Count;
  40. public override int GetHashCode() => (this.Data != null ? this.Data.GetHashCode() : 0) * 21 + this.Offset + this.Count;
  41. public static bool operator ==(AutoReleaseBuffer left, AutoReleaseBuffer right) => left.Equals(right);
  42. public static bool operator !=(AutoReleaseBuffer left, AutoReleaseBuffer right) => !left.Equals(right);
  43. public static bool operator ==(AutoReleaseBuffer left, BufferSegment right) => left.Equals(right);
  44. public static bool operator !=(AutoReleaseBuffer left, BufferSegment right) => !left.Equals(right);
  45. public static implicit operator byte[](AutoReleaseBuffer left) => left.Data;
  46. public static implicit operator BufferSegment(AutoReleaseBuffer left) => new BufferSegment(left.Data, left.Offset, left.Count);
  47. public override string ToString()
  48. {
  49. var sb = StringBuilderPool.Get(this.Count + 5);
  50. sb.Append("[AutoReleaseBuffer ");
  51. if (this.Count > 0)
  52. {
  53. sb.AppendFormat("Offset: {0:N0} ", this.Offset);
  54. sb.AppendFormat("Count: {0:N0} ", this.Count);
  55. sb.Append("Data: [");
  56. if (this.Count > 0)
  57. {
  58. int dumpCount = Math.Min(this.Count, BufferSegment.ToStringMaxDumpLength);
  59. sb.AppendFormat("{0:X2}", this.Data[this.Offset]);
  60. for (int i = 1; i < dumpCount; ++i)
  61. sb.AppendFormat(", {0:X2}", this.Data[this.Offset + i]);
  62. if (this.Count > dumpCount)
  63. sb.Append(", ...");
  64. }
  65. sb.Append("]]");
  66. }
  67. else
  68. sb.Append(']');
  69. return StringBuilderPool.ReleaseAndGrab(sb);
  70. }
  71. public void Dispose()
  72. {
  73. if (this.Data != null)
  74. BufferPool.Release(this.Data);
  75. this.Data = null;
  76. }
  77. }
  78. }