(Object array, Object first, Object second)
| 1255 | } |
| 1256 | |
| 1257 | public Object arraySlice(Object array, Object first, Object second) { |
| 1258 | int firstInt = toInt(first); |
| 1259 | |
| 1260 | // ----- byte[] case ----- |
| 1261 | if (array instanceof byte[]) { |
| 1262 | byte[] byteArray = (byte[]) array; |
| 1263 | |
| 1264 | if (second == null) { |
| 1265 | // handle negative first |
| 1266 | int index = firstInt; |
| 1267 | if (firstInt < 0) { |
| 1268 | index = byteArray.length + firstInt; |
| 1269 | if (index < 0) { |
| 1270 | index = 0; |
| 1271 | } |
| 1272 | } |
| 1273 | // slice [index..end) and return as List<Byte> |
| 1274 | List<Byte> result = new ArrayList<>(byteArray.length - index); |
| 1275 | for (int i = index; i < byteArray.length; i++) { |
| 1276 | result.add(byteArray[i]); |
| 1277 | } |
| 1278 | return result; |
| 1279 | } else { |
| 1280 | int secondInt = toInt(second); |
| 1281 | // slice [firstInt..secondInt) and return as List<Byte> |
| 1282 | int from = firstInt; |
| 1283 | int to = secondInt; |
| 1284 | if (from < 0 || to < from || to > byteArray.length) { |
| 1285 | throw new IndexOutOfBoundsException( |
| 1286 | "Invalid slice range: " + from + ".." + to + " for length " + byteArray.length |
| 1287 | ); |
| 1288 | } |
| 1289 | List<Byte> result = new ArrayList<>(to - from); |
| 1290 | for (int i = from; i < to; i++) { |
| 1291 | result.add(byteArray[i]); |
| 1292 | } |
| 1293 | return result; |
| 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | // ----- List<?> case ----- |
| 1298 | if (!(array instanceof List<?>)) { |
| 1299 | throw new IllegalArgumentException("array must be byte[] or List, got: " + array.getClass()); |
| 1300 | } |
| 1301 | |
| 1302 | // ArrayCache mutates from the WS thread. subList captures modCount |
| 1303 | // non-atomically — under JMM that read can be stale, so the wrapping |
| 1304 | // `new ArrayList<>(view)` silently null-pads or skips CME. Materialize |
| 1305 | // a stable copy under the cache monitor first. |
| 1306 | if (array instanceof io.github.ccxt.ws.ArrayCache cache) { |
| 1307 | array = cache.snapshot(); |
| 1308 | } |
| 1309 | |
| 1310 | List<?> parsedArray = (List<?>) array; |
| 1311 | |
| 1312 | if (second == null) { |
| 1313 | if (firstInt < 0) { |
| 1314 | int index = parsedArray.size() + firstInt; |
no test coverage detected