33
44using System ;
55using System . IO ;
6- #if ! NETSTANDARD2_0
7- using System . Buffers ;
8- #endif
96
107namespace ImageMagick ;
118
129internal sealed unsafe class ByteArrayWrapper : IDisposable
1310{
14- #if ! NETSTANDARD2_0
15- private static readonly ArrayPool < byte > _pool = ArrayPool < byte > . Create ( 1024 * 1024 * 64 , 128 ) ;
16-
17- private byte [ ] _bytes = _pool . Rent ( 8192 ) ;
18- #else
19- private byte [ ] _bytes = new byte [ 8192 ] ;
20- #endif
11+ private readonly PooledByteArray _bytes = new PooledByteArray ( 8192 ) ;
2112 private int _offset = 0 ;
22-
2313 private int _length = 0 ;
2414
25- #if ! NETSTANDARD2_0
2615 public void Dispose ( )
27- => _pool . Return ( _bytes ) ;
16+ => _bytes . Dispose ( ) ;
2817
2918 public byte [ ] GetBytes ( )
30- {
31- var result = new byte [ _length ] ;
32- Array . Copy ( _bytes , result , _length ) ;
33- return result ;
34- }
35-
36- #else
37- public void Dispose ( )
38- {
39- }
19+ => _bytes . ToUnpooledArray ( _length ) ;
4020
41- public byte [ ] GetBytes ( )
42- {
43- ResizeBytes ( _length ) ;
44- return _bytes ;
45- }
46-
47- #endif
4821 public long Read ( IntPtr data , UIntPtr count , IntPtr user_data )
4922 {
5023 if ( data == IntPtr . Zero )
@@ -58,7 +31,7 @@ public long Read(IntPtr data, UIntPtr count, IntPtr user_data)
5831 if ( length != 0 )
5932 {
6033 var destination = ( byte * ) data . ToPointer ( ) ;
61- fixed ( byte * source = _bytes )
34+ fixed ( byte * source = _bytes . Data )
6235 {
6336 NativeMemory . Copy ( source + _offset , destination , length ) ;
6437 }
@@ -108,7 +81,7 @@ public long Write(IntPtr data, UIntPtr count, IntPtr user_data)
10881 EnsureLength ( newOffset ) ;
10982
11083 var source = ( byte * ) data . ToPointer ( ) ;
111- fixed ( byte * destination = _bytes )
84+ fixed ( byte * destination = _bytes . Data )
11285 {
11386 NativeMemory . Copy ( source , destination + _offset , total ) ;
11487 }
@@ -132,16 +105,6 @@ private void EnsureLength(int length)
132105 ResizeBytes ( newLength ) ;
133106 }
134107
135- #if ! NETSTANDARD2_0
136- private void ResizeBytes ( int length )
137- {
138- var newBytes = _pool . Rent ( length ) ;
139- Array . Copy ( _bytes , newBytes , _bytes . Length ) ;
140- _pool . Return ( _bytes ) ;
141- _bytes = newBytes ;
142- }
143- #else
144108 private void ResizeBytes ( int length )
145- => Array . Resize ( ref _bytes , length ) ;
146- #endif
109+ => _bytes . Resize ( length ) ;
147110}
0 commit comments