@@ -64,6 +64,25 @@ public DirectBuffer(byte* pBuffer, int bufferLength, BufferOverflowDelegate buff
64
64
Wrap ( pBuffer , bufferLength ) ;
65
65
}
66
66
67
+ /// <summary>
68
+ /// Attach a view to a buffer owned by external code
69
+ /// </summary>
70
+ /// <param name="buffer">byte buffer</param>
71
+ public DirectBuffer ( ArraySegment < byte > buffer ) : this ( buffer , null )
72
+ {
73
+ }
74
+
75
+ /// <summary>
76
+ /// Attach a view to a buffer owned by external code
77
+ /// </summary>
78
+ /// <param name="buffer">byte buffer</param>
79
+ /// <param name="bufferOverflow">delegate to allow reallocation of buffer</param>
80
+ public DirectBuffer ( ArraySegment < byte > buffer , BufferOverflowDelegate bufferOverflow )
81
+ {
82
+ this . bufferOverflow = bufferOverflow ;
83
+ Wrap ( buffer ) ;
84
+ }
85
+
67
86
/// <summary>
68
87
/// Creates a DirectBuffer that can later be wrapped
69
88
/// </summary>
@@ -114,6 +133,24 @@ public void Wrap(byte* pBuffer, int bufferLength)
114
133
_needToFreeGCHandle = false ;
115
134
}
116
135
136
+ /// <summary>
137
+ /// Recycles an existing <see cref="DirectBuffer"/> from a byte buffer owned by external code
138
+ /// </summary>
139
+ /// <param name="buffer">buffer of bytes</param>
140
+ public void Wrap ( ArraySegment < byte > buffer )
141
+ {
142
+ if ( buffer == null ) throw new ArgumentNullException ( nameof ( buffer ) ) ;
143
+
144
+ FreeGCHandle ( ) ;
145
+
146
+ // pin the buffer so it does not get moved around by GC, this is required since we use pointers
147
+ _pinnedGCHandle = GCHandle . Alloc ( buffer . Array , GCHandleType . Pinned ) ;
148
+ _needToFreeGCHandle = true ;
149
+
150
+ _pBuffer = ( ( byte * ) _pinnedGCHandle . AddrOfPinnedObject ( ) . ToPointer ( ) ) + buffer . Offset ;
151
+ _capacity = buffer . Count ;
152
+ }
153
+
117
154
/// <summary>
118
155
/// Capacity of the underlying buffer
119
156
/// </summary>
0 commit comments