Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.os; |
| 18 | |
Andreas Huber | 906a679 | 2016-09-22 18:14:17 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Steven Moreland | 0ff061a | 2019-03-04 17:56:30 -0800 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 21 | import android.annotation.SystemApi; |
Steven Moreland | 14b9eb6 | 2019-01-11 10:19:51 -0800 | [diff] [blame] | 22 | import android.annotation.TestApi; |
Andreas Huber | 906a679 | 2016-09-22 18:14:17 -0700 | [diff] [blame] | 23 | |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 24 | import libcore.util.NativeAllocationRegistry; |
| 25 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 26 | /** |
| 27 | * Represents fixed sized allocation of marshalled data used. Helper methods |
| 28 | * allow for access to the unmarshalled data in a variety of ways. |
| 29 | * |
| 30 | * @hide |
| 31 | */ |
| 32 | @SystemApi |
Steven Moreland | 14b9eb6 | 2019-01-11 10:19:51 -0800 | [diff] [blame] | 33 | @TestApi |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 34 | public class HwBlob { |
| 35 | private static final String TAG = "HwBlob"; |
| 36 | |
| 37 | private static final NativeAllocationRegistry sNativeRegistry; |
| 38 | |
| 39 | public HwBlob(int size) { |
| 40 | native_setup(size); |
| 41 | |
| 42 | sNativeRegistry.registerNativeAllocation( |
| 43 | this, |
| 44 | mNativeContext); |
| 45 | } |
| 46 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 47 | /** |
| 48 | * @param offset offset to unmarshall a boolean from |
| 49 | * @return the unmarshalled boolean value |
| 50 | * @throws IndexOutOfBoundsException when offset is out of this HwBlob |
| 51 | */ |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 52 | public native final boolean getBool(long offset); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 53 | /** |
| 54 | * @param offset offset to unmarshall a byte from |
| 55 | * @return the unmarshalled byte value |
| 56 | * @throws IndexOutOfBoundsException when offset is out of this HwBlob |
| 57 | */ |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 58 | public native final byte getInt8(long offset); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 59 | /** |
| 60 | * @param offset offset to unmarshall a short from |
| 61 | * @return the unmarshalled short value |
| 62 | * @throws IndexOutOfBoundsException when offset is out of this HwBlob |
| 63 | */ |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 64 | public native final short getInt16(long offset); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 65 | /** |
| 66 | * @param offset offset to unmarshall an int from |
| 67 | * @return the unmarshalled int value |
| 68 | * @throws IndexOutOfBoundsException when offset is out of this HwBlob |
| 69 | */ |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 70 | public native final int getInt32(long offset); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 71 | /** |
| 72 | * @param offset offset to unmarshall a long from |
| 73 | * @return the unmarshalled long value |
| 74 | * @throws IndexOutOfBoundsException when offset is out of this HwBlob |
| 75 | */ |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 76 | public native final long getInt64(long offset); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 77 | /** |
| 78 | * @param offset offset to unmarshall a float from |
| 79 | * @return the unmarshalled float value |
| 80 | * @throws IndexOutOfBoundsException when offset is out of this HwBlob |
| 81 | */ |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 82 | public native final float getFloat(long offset); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 83 | /** |
| 84 | * @param offset offset to unmarshall a double from |
| 85 | * @return the unmarshalled double value |
| 86 | * @throws IndexOutOfBoundsException when offset is out of this HwBlob |
| 87 | */ |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 88 | public native final double getDouble(long offset); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 89 | /** |
| 90 | * @param offset offset to unmarshall a string from |
| 91 | * @return the unmarshalled string value |
| 92 | * @throws IndexOutOfBoundsException when offset is out of this HwBlob |
| 93 | */ |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 94 | public native final String getString(long offset); |
Ytai Ben-Tsvi | 4659e18 | 2019-11-06 09:53:34 -0800 | [diff] [blame] | 95 | /** |
| 96 | * For embedded fields that follow a two-step approach for reading, first obtain their field |
| 97 | * handle using this method, and pass that field handle to the respective |
| 98 | * HwParcel.readEmbedded*() method. |
| 99 | * @param offset The field offset. |
| 100 | * @return The field handle. |
| 101 | */ |
| 102 | public native final long getFieldHandle(long offset); |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 103 | |
Andreas Huber | 0eb37e0 | 2017-10-31 11:51:50 -0700 | [diff] [blame] | 104 | /** |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 105 | * Copy the blobs data starting from the given byte offset into the range, copying |
| 106 | * a total of size elements. |
| 107 | * |
| 108 | * @param offset starting location in blob |
| 109 | * @param array destination array |
| 110 | * @param size total number of elements to copy |
| 111 | * @throws IllegalArgumentException array.length < size |
| 112 | * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jboolean)] out of the blob. |
Andreas Huber | 0eb37e0 | 2017-10-31 11:51:50 -0700 | [diff] [blame] | 113 | */ |
| 114 | public native final void copyToBoolArray(long offset, boolean[] array, int size); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 115 | /** |
| 116 | * Copy the blobs data starting from the given byte offset into the range, copying |
| 117 | * a total of size elements. |
| 118 | * |
| 119 | * @param offset starting location in blob |
| 120 | * @param array destination array |
| 121 | * @param size total number of elements to copy |
| 122 | * @throws IllegalArgumentException array.length < size |
| 123 | * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jbyte)] out of the blob. |
| 124 | */ |
Andreas Huber | 0eb37e0 | 2017-10-31 11:51:50 -0700 | [diff] [blame] | 125 | public native final void copyToInt8Array(long offset, byte[] array, int size); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 126 | /** |
| 127 | * Copy the blobs data starting from the given byte offset into the range, copying |
| 128 | * a total of size elements. |
| 129 | * |
| 130 | * @param offset starting location in blob |
| 131 | * @param array destination array |
| 132 | * @param size total number of elements to copy |
| 133 | * @throws IllegalArgumentException array.length < size |
| 134 | * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jshort)] out of the blob. |
| 135 | */ |
Andreas Huber | 0eb37e0 | 2017-10-31 11:51:50 -0700 | [diff] [blame] | 136 | public native final void copyToInt16Array(long offset, short[] array, int size); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 137 | /** |
| 138 | * Copy the blobs data starting from the given byte offset into the range, copying |
| 139 | * a total of size elements. |
| 140 | * |
| 141 | * @param offset starting location in blob |
| 142 | * @param array destination array |
| 143 | * @param size total number of elements to copy |
| 144 | * @throws IllegalArgumentException array.length < size |
| 145 | * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jint)] out of the blob. |
| 146 | */ |
Andreas Huber | 0eb37e0 | 2017-10-31 11:51:50 -0700 | [diff] [blame] | 147 | public native final void copyToInt32Array(long offset, int[] array, int size); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 148 | /** |
| 149 | * Copy the blobs data starting from the given byte offset into the range, copying |
| 150 | * a total of size elements. |
| 151 | * |
| 152 | * @param offset starting location in blob |
| 153 | * @param array destination array |
| 154 | * @param size total number of elements to copy |
| 155 | * @throws IllegalArgumentException array.length < size |
| 156 | * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jlong)] out of the blob. |
| 157 | */ |
Andreas Huber | 0eb37e0 | 2017-10-31 11:51:50 -0700 | [diff] [blame] | 158 | public native final void copyToInt64Array(long offset, long[] array, int size); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 159 | /** |
| 160 | * Copy the blobs data starting from the given byte offset into the range, copying |
| 161 | * a total of size elements. |
| 162 | * |
| 163 | * @param offset starting location in blob |
| 164 | * @param array destination array |
| 165 | * @param size total number of elements to copy |
| 166 | * @throws IllegalArgumentException array.length < size |
| 167 | * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jfloat)] out of the blob. |
| 168 | */ |
Andreas Huber | 0eb37e0 | 2017-10-31 11:51:50 -0700 | [diff] [blame] | 169 | public native final void copyToFloatArray(long offset, float[] array, int size); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 170 | /** |
| 171 | * Copy the blobs data starting from the given byte offset into the range, copying |
| 172 | * a total of size elements. |
| 173 | * |
| 174 | * @param offset starting location in blob |
| 175 | * @param array destination array |
| 176 | * @param size total number of elements to copy |
| 177 | * @throws IllegalArgumentException array.length < size |
| 178 | * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jdouble)] out of the blob. |
| 179 | */ |
Andreas Huber | 0eb37e0 | 2017-10-31 11:51:50 -0700 | [diff] [blame] | 180 | public native final void copyToDoubleArray(long offset, double[] array, int size); |
| 181 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 182 | /** |
| 183 | * Writes a boolean value at an offset. |
| 184 | * |
| 185 | * @param offset location to write value |
| 186 | * @param x value to write |
| 187 | * @throws IndexOutOfBoundsException when [offset, offset + sizeof(jboolean)] is out of range |
| 188 | */ |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 189 | public native final void putBool(long offset, boolean x); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 190 | /** |
| 191 | * Writes a byte value at an offset. |
| 192 | * |
| 193 | * @param offset location to write value |
| 194 | * @param x value to write |
| 195 | * @throws IndexOutOfBoundsException when [offset, offset + sizeof(jbyte)] is out of range |
| 196 | */ |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 197 | public native final void putInt8(long offset, byte x); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 198 | /** |
| 199 | * Writes a short value at an offset. |
| 200 | * |
| 201 | * @param offset location to write value |
| 202 | * @param x value to write |
| 203 | * @throws IndexOutOfBoundsException when [offset, offset + sizeof(jshort)] is out of range |
| 204 | */ |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 205 | public native final void putInt16(long offset, short x); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 206 | /** |
| 207 | * Writes a int value at an offset. |
| 208 | * |
| 209 | * @param offset location to write value |
| 210 | * @param x value to write |
| 211 | * @throws IndexOutOfBoundsException when [offset, offset + sizeof(jint)] is out of range |
| 212 | */ |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 213 | public native final void putInt32(long offset, int x); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 214 | /** |
| 215 | * Writes a long value at an offset. |
| 216 | * |
| 217 | * @param offset location to write value |
| 218 | * @param x value to write |
| 219 | * @throws IndexOutOfBoundsException when [offset, offset + sizeof(jlong)] is out of range |
| 220 | */ |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 221 | public native final void putInt64(long offset, long x); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 222 | /** |
| 223 | * Writes a float value at an offset. |
| 224 | * |
| 225 | * @param offset location to write value |
| 226 | * @param x value to write |
| 227 | * @throws IndexOutOfBoundsException when [offset, offset + sizeof(jfloat)] is out of range |
| 228 | */ |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 229 | public native final void putFloat(long offset, float x); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 230 | /** |
| 231 | * Writes a double value at an offset. |
| 232 | * |
| 233 | * @param offset location to write value |
| 234 | * @param x value to write |
| 235 | * @throws IndexOutOfBoundsException when [offset, offset + sizeof(jdouble)] is out of range |
| 236 | */ |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 237 | public native final void putDouble(long offset, double x); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 238 | /** |
| 239 | * Writes a string value at an offset. |
| 240 | * |
| 241 | * @param offset location to write value |
| 242 | * @param x value to write |
| 243 | * @throws IndexOutOfBoundsException when [offset, offset + sizeof(jstring)] is out of range |
| 244 | */ |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 245 | public native final void putString(long offset, String x); |
Nirav Atre | 9850dd9 | 2018-07-24 17:03:44 -0700 | [diff] [blame] | 246 | /** |
| 247 | * Writes a native handle (without duplicating the underlying file descriptors) at an offset. |
| 248 | * |
| 249 | * @param offset location to write value |
| 250 | * @param x a {@link NativeHandle} instance to write |
| 251 | * @throws IndexOutOfBoundsException when [offset, offset + sizeof(jobject)] is out of range |
| 252 | */ |
Steven Moreland | 0ff061a | 2019-03-04 17:56:30 -0800 | [diff] [blame] | 253 | public native final void putNativeHandle(long offset, @Nullable NativeHandle x); |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 254 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 255 | /** |
| 256 | * Put a boolean array contiguously at an offset in the blob. |
| 257 | * |
| 258 | * @param offset location to write values |
| 259 | * @param x array to write |
| 260 | * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jboolean)] out of the blob. |
| 261 | */ |
Andreas Huber | 0eb37e0 | 2017-10-31 11:51:50 -0700 | [diff] [blame] | 262 | public native final void putBoolArray(long offset, boolean[] x); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 263 | /** |
| 264 | * Put a byte array contiguously at an offset in the blob. |
| 265 | * |
| 266 | * @param offset location to write values |
| 267 | * @param x array to write |
| 268 | * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jbyte)] out of the blob. |
| 269 | */ |
Andreas Huber | 0eb37e0 | 2017-10-31 11:51:50 -0700 | [diff] [blame] | 270 | public native final void putInt8Array(long offset, byte[] x); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 271 | /** |
| 272 | * Put a short array contiguously at an offset in the blob. |
| 273 | * |
| 274 | * @param offset location to write values |
| 275 | * @param x array to write |
| 276 | * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jshort)] out of the blob. |
| 277 | */ |
Andreas Huber | 0eb37e0 | 2017-10-31 11:51:50 -0700 | [diff] [blame] | 278 | public native final void putInt16Array(long offset, short[] x); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 279 | /** |
| 280 | * Put a int array contiguously at an offset in the blob. |
| 281 | * |
| 282 | * @param offset location to write values |
| 283 | * @param x array to write |
| 284 | * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jint)] out of the blob. |
| 285 | */ |
Andreas Huber | 0eb37e0 | 2017-10-31 11:51:50 -0700 | [diff] [blame] | 286 | public native final void putInt32Array(long offset, int[] x); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 287 | /** |
| 288 | * Put a long array contiguously at an offset in the blob. |
| 289 | * |
| 290 | * @param offset location to write values |
| 291 | * @param x array to write |
| 292 | * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jlong)] out of the blob. |
| 293 | */ |
Andreas Huber | 0eb37e0 | 2017-10-31 11:51:50 -0700 | [diff] [blame] | 294 | public native final void putInt64Array(long offset, long[] x); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 295 | /** |
| 296 | * Put a float array contiguously at an offset in the blob. |
| 297 | * |
| 298 | * @param offset location to write values |
| 299 | * @param x array to write |
| 300 | * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jfloat)] out of the blob. |
| 301 | */ |
Andreas Huber | 0eb37e0 | 2017-10-31 11:51:50 -0700 | [diff] [blame] | 302 | public native final void putFloatArray(long offset, float[] x); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 303 | /** |
| 304 | * Put a double array contiguously at an offset in the blob. |
| 305 | * |
| 306 | * @param offset location to write values |
| 307 | * @param x array to write |
| 308 | * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jdouble)] out of the blob. |
| 309 | */ |
Andreas Huber | 0eb37e0 | 2017-10-31 11:51:50 -0700 | [diff] [blame] | 310 | public native final void putDoubleArray(long offset, double[] x); |
| 311 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 312 | /** |
| 313 | * Write another HwBlob into this blob at the specified location. |
| 314 | * |
| 315 | * @param offset location to write value |
| 316 | * @param blob data to write |
| 317 | * @throws IndexOutOfBoundsException if [offset, offset + blob's size] outside of the range of |
| 318 | * this blob. |
| 319 | */ |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 320 | public native final void putBlob(long offset, HwBlob blob); |
| 321 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 322 | /** |
Ytai Ben-Tsvi | 4659e18 | 2019-11-06 09:53:34 -0800 | [diff] [blame] | 323 | * Writes a HidlMemory instance (without duplicating the underlying file descriptors) at an |
| 324 | * offset. |
| 325 | * |
| 326 | * @param offset location to write value |
| 327 | * @param mem a {@link HidlMemory} instance to write |
| 328 | * @throws IndexOutOfBoundsException when [offset, offset + sizeof(jobject)] is out of range |
| 329 | */ |
| 330 | public final void putHidlMemory(long offset, @NonNull HidlMemory mem) { |
| 331 | putNativeHandle(offset + 0 /* offset of 'handle' field. */, mem.getHandle()); |
| 332 | putInt64(offset + 16 /* offset of 'size' field. */, mem.getSize()); |
| 333 | putString(offset + 24 /* offset of 'name' field. */, mem.getName()); |
| 334 | } |
| 335 | |
| 336 | /** |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 337 | * @return current handle of HwBlob for reference in a parcelled binder transaction |
| 338 | */ |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 339 | public native final long handle(); |
| 340 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 341 | /** |
| 342 | * Convert a primitive to a wrapped array for boolean. |
| 343 | * |
| 344 | * @param array from array |
| 345 | * @return transformed array |
| 346 | */ |
Andreas Huber | 906a679 | 2016-09-22 18:14:17 -0700 | [diff] [blame] | 347 | public static Boolean[] wrapArray(@NonNull boolean[] array) { |
| 348 | final int n = array.length; |
| 349 | Boolean[] wrappedArray = new Boolean[n]; |
| 350 | for (int i = 0; i < n; ++i) { |
| 351 | wrappedArray[i] = array[i]; |
| 352 | } |
| 353 | return wrappedArray; |
| 354 | } |
| 355 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 356 | /** |
| 357 | * Convert a primitive to a wrapped array for long. |
| 358 | * |
| 359 | * @param array from array |
| 360 | * @return transformed array |
| 361 | */ |
Andreas Huber | 906a679 | 2016-09-22 18:14:17 -0700 | [diff] [blame] | 362 | public static Long[] wrapArray(@NonNull long[] array) { |
| 363 | final int n = array.length; |
| 364 | Long[] wrappedArray = new Long[n]; |
| 365 | for (int i = 0; i < n; ++i) { |
| 366 | wrappedArray[i] = array[i]; |
| 367 | } |
| 368 | return wrappedArray; |
| 369 | } |
| 370 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 371 | /** |
| 372 | * Convert a primitive to a wrapped array for byte. |
| 373 | * |
| 374 | * @param array from array |
| 375 | * @return transformed array |
| 376 | */ |
Andreas Huber | 906a679 | 2016-09-22 18:14:17 -0700 | [diff] [blame] | 377 | public static Byte[] wrapArray(@NonNull byte[] array) { |
| 378 | final int n = array.length; |
| 379 | Byte[] wrappedArray = new Byte[n]; |
| 380 | for (int i = 0; i < n; ++i) { |
| 381 | wrappedArray[i] = array[i]; |
| 382 | } |
| 383 | return wrappedArray; |
| 384 | } |
| 385 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 386 | /** |
| 387 | * Convert a primitive to a wrapped array for short. |
| 388 | * |
| 389 | * @param array from array |
| 390 | * @return transformed array |
| 391 | */ |
Andreas Huber | 906a679 | 2016-09-22 18:14:17 -0700 | [diff] [blame] | 392 | public static Short[] wrapArray(@NonNull short[] array) { |
| 393 | final int n = array.length; |
| 394 | Short[] wrappedArray = new Short[n]; |
| 395 | for (int i = 0; i < n; ++i) { |
| 396 | wrappedArray[i] = array[i]; |
| 397 | } |
| 398 | return wrappedArray; |
| 399 | } |
| 400 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 401 | /** |
| 402 | * Convert a primitive to a wrapped array for int. |
| 403 | * |
| 404 | * @param array from array |
| 405 | * @return transformed array |
| 406 | */ |
Andreas Huber | 906a679 | 2016-09-22 18:14:17 -0700 | [diff] [blame] | 407 | public static Integer[] wrapArray(@NonNull int[] array) { |
| 408 | final int n = array.length; |
| 409 | Integer[] wrappedArray = new Integer[n]; |
| 410 | for (int i = 0; i < n; ++i) { |
| 411 | wrappedArray[i] = array[i]; |
| 412 | } |
| 413 | return wrappedArray; |
| 414 | } |
| 415 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 416 | /** |
| 417 | * Convert a primitive to a wrapped array for float. |
| 418 | * |
| 419 | * @param array from array |
| 420 | * @return transformed array |
| 421 | */ |
Andreas Huber | 906a679 | 2016-09-22 18:14:17 -0700 | [diff] [blame] | 422 | public static Float[] wrapArray(@NonNull float[] array) { |
| 423 | final int n = array.length; |
| 424 | Float[] wrappedArray = new Float[n]; |
| 425 | for (int i = 0; i < n; ++i) { |
| 426 | wrappedArray[i] = array[i]; |
| 427 | } |
| 428 | return wrappedArray; |
| 429 | } |
| 430 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 431 | /** |
| 432 | * Convert a primitive to a wrapped array for double. |
| 433 | * |
| 434 | * @param array from array |
| 435 | * @return transformed array |
| 436 | */ |
Andreas Huber | 906a679 | 2016-09-22 18:14:17 -0700 | [diff] [blame] | 437 | public static Double[] wrapArray(@NonNull double[] array) { |
| 438 | final int n = array.length; |
| 439 | Double[] wrappedArray = new Double[n]; |
| 440 | for (int i = 0; i < n; ++i) { |
| 441 | wrappedArray[i] = array[i]; |
| 442 | } |
| 443 | return wrappedArray; |
| 444 | } |
| 445 | |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 446 | // Returns address of the "freeFunction". |
| 447 | private static native final long native_init(); |
| 448 | |
| 449 | private native final void native_setup(int size); |
| 450 | |
| 451 | static { |
| 452 | long freeFunction = native_init(); |
| 453 | |
| 454 | sNativeRegistry = new NativeAllocationRegistry( |
| 455 | HwBlob.class.getClassLoader(), |
| 456 | freeFunction, |
| 457 | 128 /* size */); |
| 458 | } |
| 459 | |
| 460 | private long mNativeContext; |
| 461 | } |
| 462 | |
| 463 | |