forked from jamesathey/FastAndroidCamera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFastJavaByteArray.cs
319 lines (278 loc) · 8 KB
/
FastJavaByteArray.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// <copyright company="APX Labs, Inc.">
// Copyright (c) APX Labs, Inc. All rights reserved.
// </copyright>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Java.Interop;
namespace ApxLabs.FastAndroidCamera
{
/// <summary>
/// A wrapper around a Java array that reads elements directly from the pointer instead of through expensive JNI calls.
/// </summary>
public sealed class FastJavaByteArray : IList<byte>, IDisposable
{
private JniObjectReference _javaRef;
#region Constructors
/// <summary>
/// Creates a new FastJavaByteArray with the given number of bytes reserved.
/// </summary>
/// <param name="length">Number of bytes to reserve</param>
public FastJavaByteArray(int length)
{
if (length <= 0)
throw new ArgumentOutOfRangeException();
JniObjectReference localRef = JniEnvironment.Arrays.NewByteArray(length);
if (!localRef.IsValid)
throw new OutOfMemoryException();
// Retain a global reference to the byte array.
_javaRef = localRef.NewGlobalRef();
Count = length;
bool isCopy = false;
unsafe
{
// Get the pointer to the byte array using the global Handle
Raw = (byte*)JniEnvironment.Arrays.GetByteArrayElements(_javaRef, &isCopy);
}
}
/// <summary>
/// Creates a FastJavaByteArray wrapper around an existing Java/JNI byte array
/// </summary>
/// <param name="handle">Native Java array handle</param>
/// <param name="readOnly">Whether to consider this byte array read-only</param>
public FastJavaByteArray(IntPtr handle, bool readOnly = true)
{
if (handle == IntPtr.Zero)
throw new ArgumentNullException("handle");
IsReadOnly = readOnly;
// Retain a global reference to the byte array.
_javaRef = new JniObjectReference(handle).NewGlobalRef();
Count = JniEnvironment.Arrays.GetArrayLength(_javaRef);
bool isCopy = false;
unsafe
{
// Get a pinned pointer to the byte array using the global Handle
Raw = (byte*)JniEnvironment.Arrays.GetByteArrayElements(_javaRef, &isCopy);
}
}
#endregion
#region Dispose Pattern
/// <summary>
/// Releases unmanaged resources and performs other cleanup operations before the
/// <see cref="T:ApxLabs.FastAndroidCamera.FastJavaByteArray"/> is reclaimed by garbage collection.
/// </summary>
~FastJavaByteArray()
{
Dispose(false);
}
/// <summary>
/// Releases all resource used by the <see cref="T:ApxLabs.FastAndroidCamera.FastJavaByteArray"/> object.
/// </summary>
/// <remarks>Call <see cref="Dispose"/> when you are finished using the
/// <see cref="T:ApxLabs.FastAndroidCamera.FastJavaByteArray"/>. The <see cref="Dispose"/> method leaves the
/// <see cref="T:ApxLabs.FastAndroidCamera.FastJavaByteArray"/> in an unusable state. After calling
/// <see cref="Dispose"/>, you must release all references to the
/// <see cref="T:ApxLabs.FastAndroidCamera.FastJavaByteArray"/> so the garbage collector can reclaim the memory that
/// the <see cref="T:ApxLabs.FastAndroidCamera.FastJavaByteArray"/> was occupying.</remarks>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (!_javaRef.IsValid)
return;
unsafe
{
// tell Java that we're done with this array
JniEnvironment.Arrays.ReleaseByteArrayElements(_javaRef, (sbyte*)Raw, JniReleaseArrayElementsMode.Default);
}
if (disposing)
{
JniObjectReference.Dispose(ref _javaRef);
}
}
#endregion
#region IList<byte> Properties
/// <summary>
/// Count of bytes
/// </summary>
public int Count { get; private set; }
/// <summary>
/// Gets a value indicating whether this byte array is read only.
/// </summary>
/// <value><c>true</c> if read only; otherwise, <c>false</c>.</value>
public bool IsReadOnly
{
get;
private set;
}
/// <summary>
/// Indexer
/// </summary>
/// <param name="index">Index of byte</param>
/// <returns>Byte at the given index</returns>
public byte this[int index]
{
get
{
if (index < 0 || index >= Count)
{
throw new ArgumentOutOfRangeException();
}
byte retval;
unsafe
{
retval = Raw[index];
}
return retval;
}
set
{
if (IsReadOnly)
{
throw new NotSupportedException("This FastJavaByteArray is read-only");
}
if (index < 0 || index >= Count)
{
throw new ArgumentOutOfRangeException();
}
unsafe
{
Raw[index] = value;
}
}
}
#endregion
#region IList<byte> Methods
/// <summary>
/// Adds a single byte to the list. Not supported
/// </summary>
/// <param name="item">byte to add</param>
public void Add(byte item)
{
throw new NotSupportedException("FastJavaByteArray is fixed length");
}
/// <summary>
/// Not supported
/// </summary>
public void Clear()
{
throw new NotSupportedException("FastJavaByteArray is fixed length");
}
/// <summary>
/// Returns true if the item is found int he array
/// </summary>
/// <param name="item">Item to find</param>
/// <returns>True if the item is found</returns>
public bool Contains(byte item)
{
return IndexOf(item) >= 0;
}
/// <summary>
/// Copies the contents of the FastJavaByteArray into a byte array
/// </summary>
/// <param name="array">The array to copy to.</param>
/// <param name="arrayIndex">The zero-based index into the destination array where CopyTo should start.</param>
public void CopyTo(byte[] array, int arrayIndex)
{
unsafe
{
Marshal.Copy(new IntPtr(Raw), array, arrayIndex, Math.Min(Count, array.Length - arrayIndex));
}
}
/// <summary>
/// Retreives enumerator
/// </summary>
/// <returns>Enumerator</returns>
[DebuggerHidden]
public IEnumerator<byte> GetEnumerator()
{
return new FastJavaByteArrayEnumerator(this);
}
/// <summary>
/// Retreives enumerator
/// </summary>
/// <returns>Enumerator</returns>
[DebuggerHidden]
IEnumerator IEnumerable.GetEnumerator()
{
return new FastJavaByteArrayEnumerator(this);
}
/// <summary>
/// Gets the first index of the given value
/// </summary>
/// <param name="item">Item to search for</param>
/// <returns>Index of found item</returns>
public int IndexOf(byte item)
{
for (int i = 0; i < Count; ++i)
{
byte current;
unsafe
{
current = Raw[i];
}
if (current == item)
return i;
}
return -1;
}
/// <summary>
/// Not supported
/// </summary>
/// <param name="index"></param>
/// <param name="item"></param>
public void Insert(int index, byte item)
{
throw new NotSupportedException("FastJavaByteArray is fixed length");
}
/// <summary>
/// Not supported
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public bool Remove(byte item)
{
throw new NotSupportedException("FastJavaByteArray is fixed length");
}
/// <summary>
/// Not supported
/// </summary>
/// <param name="index"></param>
public void RemoveAt(int index)
{
throw new NotSupportedException("FastJavaByteArray is fixed length");
}
#endregion
#region Public Properties
/// <summary>
/// Gets the raw pointer to the underlying data.
/// </summary>
public unsafe byte* Raw { get; private set; }
/// <summary>
/// Gets the handle of the Java reference to the array.
/// </summary>
/// <value>The handle.</value>
public IntPtr Handle
{
get { return _javaRef.Handle; }
}
#endregion
}
}