| Nicolas Geoffray | 3eb6475 | 2025-06-11 14:48:25 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2025 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 | import java.io.ByteArrayInputStream; |
| 18 | import java.io.InvalidClassException; |
| 19 | import java.io.ObjectInputStream; |
| 20 | |
| 21 | public class Main { |
| 22 | |
| 23 | public static void main(String[] args) throws Exception { |
| 24 | deserializeHexToConcurrentHashMap(); |
| 25 | } |
| 26 | |
| 27 | public static byte[] hexStringToByteArray(String hexString) { |
| 28 | if (hexString == null || hexString.isEmpty()) { |
| 29 | return new byte[0]; |
| 30 | } |
| 31 | if (hexString.length() % 2 != 0) { |
| 32 | throw new IllegalArgumentException("Hex string must have an even number of characters."); |
| 33 | } |
| 34 | int len = hexString.length(); |
| 35 | byte[] data = new byte[len / 2]; |
| 36 | for (int i = 0; i < len; i += 2) { |
| 37 | int highNibble = Character.digit(hexString.charAt(i), 16); |
| 38 | int lowNibble = Character.digit(hexString.charAt(i + 1), 16); |
| 39 | if (highNibble == -1 || lowNibble == -1) { |
| 40 | throw new IllegalArgumentException( |
| 41 | "Invalid hex character in string: " + hexString.charAt(i) + hexString.charAt(i + 1)); |
| 42 | } |
| 43 | data[i / 2] = (byte) ((highNibble << 4) + lowNibble); |
| 44 | } |
| 45 | return data; |
| 46 | } |
| 47 | |
| 48 | public static void deserializeHexToConcurrentHashMap() throws Exception { |
| 49 | byte[] bytes = hexStringToByteArray("ACED0005737200266A6176612E7574696C2E636F6E63757272656E742E436F6E63757272656E74486173684D61706499DE129D87293D0300007870737200146A6176612E746578742E44617465466F726D6174642CA1E4C22615FC0200007870737200146A6176612E746578742E44617465466F726D6174642CA1E4C22615FC020000787070707878000000"); |
| 50 | ByteArrayInputStream bis = new ByteArrayInputStream(bytes); |
| 51 | ObjectInputStream ois = new ObjectInputStream(bis); |
| 52 | try { |
| 53 | Object deserializedObject = ois.readObject(); |
| 54 | throw new Error("Expected InvalidClassException"); |
| 55 | } catch (InvalidClassException e) { |
| 56 | // expected |
| 57 | if (!(e.getCause() instanceof InstantiationException)) { |
| 58 | throw new Error("Expected InstantiationException"); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | } |