-
Notifications
You must be signed in to change notification settings - Fork 568
Expand file tree
/
Copy pathJavaArray.cs
More file actions
119 lines (97 loc) · 2.55 KB
/
JavaArray.cs
File metadata and controls
119 lines (97 loc) · 2.55 KB
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
namespace Android.Runtime {
[Register ("mono/android/runtime/JavaArray", DoNotGenerateAcw=true)]
public sealed class JavaArray<
[DynamicallyAccessedMembers (Constructors)]
T
> : Java.Lang.Object, IList<T> {
public JavaArray (IntPtr handle, JniHandleOwnership transfer)
: base (handle, transfer)
{
}
public int Count {
get { return JNIEnv.GetArrayLength (Handle); }
}
public bool IsReadOnly {
get { return false; }
}
public T this [int index] {
get {
if (index < 0 || index >= Count)
throw new ArgumentOutOfRangeException ("index");
return JNIEnv.GetArrayItem<T> (Handle, index);
}
set { JNIEnv.SetArrayItem<T> (Handle, index, value); }
}
public void Add (T item)
{
throw new InvalidOperationException ();
}
public void Clear ()
{
throw new InvalidOperationException ();
}
public bool Contains (T item)
{
return IndexOf (item) >= 0;
}
public void CopyTo (T[] array, int array_index)
{
var items = JNIEnv.GetArray<T> (Handle)!;
for (int i = 0; i < Count; i++)
array [array_index + i] = items [i];
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator ()
{
return GetEnumerator ();
}
public IEnumerator<T> GetEnumerator ()
{
var items = JNIEnv.GetArray<T> (Handle);
for (int i = 0; i < items!.Length; i++)
yield return items [i];
}
public int IndexOf (T item)
{
var items = JNIEnv.GetArray<T> (Handle)!;
return Array.IndexOf (items, item);
}
public void Insert (int index, T item)
{
throw new InvalidOperationException ();
}
public bool Remove (T item)
{
throw new InvalidOperationException ();
}
public void RemoveAt (int index)
{
throw new InvalidOperationException ();
}
public static JavaArray<T>? FromJniHandle (IntPtr handle, JniHandleOwnership transfer)
{
if (handle == IntPtr.Zero)
return null;
var existing = Java.Lang.Object.PeekObject (handle) as JavaArray<T>;
if (existing != null) {
JNIEnv.DeleteRef (handle, transfer);
return existing;
}
return new JavaArray<T>(handle, transfer);
}
public static IntPtr ToLocalJniHandle (IList<T>? value)
{
if (value == null)
return IntPtr.Zero;
var c = value as JavaArray<T>;
if (c != null)
return JNIEnv.ToLocalJniHandle (c);
var a = new T [value.Count];
value.CopyTo (a, 0);
return JNIEnv.NewArray (a, typeof (T));
}
}
}