| 6 | import java.util.List; |
| 7 | |
| 8 | public class Util { |
| 9 | |
| 10 | public static String getDefaultTestCmd(){ |
| 11 | String osName = System.getProperty("os.name"); |
| 12 | if (osName.startsWith("Mac")) { |
| 13 | return "open /System/Applications/Calculator.app"; |
| 14 | } |
| 15 | return "calc"; |
| 16 | } |
| 17 | |
| 18 | public static void writeObj2File(Object obj,String path) throws IOException { |
| 19 | System.out.println("set obj to "+ path); |
| 20 | FileOutputStream fileOut = new FileOutputStream(path); |
| 21 | ObjectOutputStream out = new ObjectOutputStream(fileOut); |
| 22 | out.writeObject(obj); |
| 23 | out.close(); |
| 24 | fileOut.close(); |
| 25 | } |
| 26 | |
| 27 | public static Object readObj4File(String path) throws IOException, ClassNotFoundException { |
| 28 | System.out.println("get obj for "+ path); |
| 29 | FileInputStream fileInput = new FileInputStream(path); |
| 30 | ObjectInputStream input = new ObjectInputStream(fileInput); |
| 31 | return input.readObject(); |
| 32 | } |
| 33 | |
| 34 | public static void runGadgets(Object obj)throws Exception{ |
| 35 | byte[] ser = serialize(obj); |
| 36 | deserialize(ser); |
| 37 | } |
| 38 | |
| 39 | public static byte[] serialize(final Object obj) throws IOException { |
| 40 | System.out.println("serialize obj: "+ obj.getClass().getName()); |
| 41 | final ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 42 | final ObjectOutputStream objOut = new ObjectOutputStream(out); |
| 43 | objOut.writeObject(obj); |
| 44 | return out.toByteArray(); |
| 45 | } |
| 46 | |
| 47 | public static Object deserialize(byte[] ser) throws IOException, ClassNotFoundException { |
| 48 | System.out.println("deserialize obj"); |
| 49 | final ByteArrayInputStream in = new ByteArrayInputStream(ser); |
| 50 | final ObjectInputStream objIn = new ObjectInputStream(in); |
| 51 | return objIn.readObject(); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | /** |
| 56 | * 字节数组转16进制 |
| 57 | * @param bytes 需要转换的byte数组 |
| 58 | * @return 转换后的Hex字符串 |
| 59 | */ |
| 60 | public static String bytesToHex(byte[] bytes) { |
| 61 | StringBuffer sb = new StringBuffer(); |
| 62 | for(int i = 0; i < bytes.length; i++) { |
| 63 | String hex = Integer.toHexString(bytes[i] & 0xFF); |
| 64 | if(hex.length() < 2){ |
| 65 | sb.append(0); |
nothing calls this directly
no outgoing calls
no test coverage detected