forked from dlemstra/Magick.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathByteArrayWrapper.cs
183 lines (137 loc) · 3.64 KB
/
ByteArrayWrapper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
// Licensed under the Apache License, Version 2.0.
using System;
using System.IO;
#if NETSTANDARD2_1_OR_GREATER
using System.Buffers;
#endif
namespace ImageMagick;
internal sealed unsafe class ByteArrayWrapper
{
#if NETSTANDARD2_1_OR_GREATER
private static readonly ArrayPool<byte> _pool = ArrayPool<byte>.Create(1024 * 1024 * 64, 1024);
private byte[] _bytes = _pool.Rent(1024 * 1024);
#else
private byte[] _bytes = new byte[8192];
#endif
private int _offset;
private int _length;
#if NETSTANDARD2_1_OR_GREATER
public byte[] GetBytes()
{
var result = new byte[_length];
Array.Copy(_bytes, result, Math.Min(_bytes.Length, _length));
_pool.Return(_bytes, true);
return result;
}
~ByteArrayWrapper()
{
if (_bytes is not null)
{
_pool.Return(_bytes, true);
}
}
#else
public byte[] GetBytes()
{
ResizeBytes(_length);
return _bytes;
}
#endif
public long Read(IntPtr data, UIntPtr count, IntPtr user_data)
{
if (data == IntPtr.Zero)
{
return 0;
}
var total = (int)count;
if (total == 0)
{
return 0;
}
var length = Math.Min(total, _length - _offset);
if (length != 0)
{
var destination = (byte*)data.ToPointer();
fixed (byte* source = _bytes)
{
NativeMemory.Copy(source + _offset, destination, length);
}
_offset += length;
}
return length;
}
public long Seek(long offset, IntPtr whence, IntPtr user_data)
{
var newOffset = (int)((SeekOrigin)whence switch
{
SeekOrigin.Begin => offset,
SeekOrigin.Current => _offset + offset,
SeekOrigin.End => _length - offset,
_ => -1,
});
if (_offset == newOffset)
{
return _offset;
}
if (newOffset < 0)
{
return -1;
}
_offset = newOffset;
return _offset;
}
public long Tell(IntPtr user_data)
{
return _offset;
}
public long Write(IntPtr data, UIntPtr count, IntPtr user_data)
{
if (data == IntPtr.Zero)
{
return 0;
}
var total = (int)count;
if (total == 0)
{
return 0;
}
var newOffset = _offset + total;
EnsureLength(newOffset);
var source = (byte*)data.ToPointer();
fixed (byte* destination = _bytes)
{
NativeMemory.Copy(source, destination + _offset, total);
}
_offset = newOffset;
return total;
}
private void EnsureLength(int length)
{
if (length < _length)
{
return;
}
_length = length;
if (_length < _bytes.Length)
{
return;
}
var newLength = Math.Max(_bytes.Length * 2, _length);
ResizeBytes(newLength);
}
#if NETSTANDARD2_1_OR_GREATER
private void ResizeBytes(int length)
{
byte[] byte2 = _pool.Rent(length);
Array.Copy(_bytes, byte2, Math.Min(_bytes.Length, length));
_pool.Return(_bytes, true);
_bytes = byte2;
}
#else
private void ResizeBytes(int length)
{
Array.Resize(ref _bytes, length);
}
#endif
}