@@ -260,7 +260,7 @@ public T[] ToArray()
260260 {
261261 T [ ] newArray = new T [ Size ] ;
262262 int newArrayOffset = 0 ;
263- var segments = new ArraySegment < T > [ 2 ] { ArrayOne ( ) , ArrayTwo ( ) } ;
263+ var segments = ToArraySegments ( ) ;
264264 foreach ( ArraySegment < T > segment in segments )
265265 {
266266 Array . Copy ( segment . Array , segment . Offset , newArray , newArrayOffset , segment . Count ) ;
@@ -269,14 +269,31 @@ public T[] ToArray()
269269 return newArray ;
270270 }
271271
272+ /// <summary>
273+ /// Get the contents of the buffer as 2 ArraySegments.
274+ /// Respects the logical contents of the buffer, where
275+ /// each segment and items in each segment are ordered
276+ /// according to insertion.
277+ ///
278+ /// Fast: does not copy the array elements.
279+ /// Useful for methods like <c>Send(IList<ArraySegment<Byte>>)</c>.
280+ ///
281+ /// <remarks>Segments may be empty.</remarks>
282+ /// </summary>
283+ /// <returns>An IList with 2 segments corresponding to the buffer content.</returns>
284+ public IList < ArraySegment < T > > ToArraySegments ( )
285+ {
286+ return new [ ] { ArrayOne ( ) , ArrayTwo ( ) } ;
287+ }
288+
272289 #region IEnumerable<T> implementation
273290 /// <summary>
274291 /// Returns an enumerator that iterates through this buffer.
275292 /// </summary>
276293 /// <returns>An enumerator that can be used to iterate this collection.</returns>
277294 public IEnumerator < T > GetEnumerator ( )
278295 {
279- var segments = new ArraySegment < T > [ 2 ] { ArrayOne ( ) , ArrayTwo ( ) } ;
296+ var segments = ToArraySegments ( ) ;
280297 foreach ( ArraySegment < T > segment in segments )
281298 {
282299 for ( int i = 0 ; i < segment . Count ; i ++ )
0 commit comments