test/testlibrary/com/oracle/java/testlibrary/Utils.java

Print this page




 297     /**
 298      * Returns file content as a list of strings
 299      *
 300      * @param file File to operate on
 301      * @return List of strings
 302      * @throws IOException
 303      */
 304     public static List<String> fileAsList(File file) throws IOException {
 305         assertTrue(file.exists() && file.isFile(),
 306                 file.getAbsolutePath() + " does not exist or not a file");
 307         List<String> output = new ArrayList<>();
 308         try (BufferedReader reader = new BufferedReader(new FileReader(file.getAbsolutePath()))) {
 309             while (reader.ready()) {
 310                 output.add(reader.readLine().replace(NEW_LINE, ""));
 311             }
 312         }
 313         return output;
 314     }
 315 
 316     /**





























 317      * @return Unsafe instance.
 318      */
 319     public static synchronized Unsafe getUnsafe() {
 320         if (unsafe == null) {
 321             try {
 322                 Field f = Unsafe.class.getDeclaredField("theUnsafe");
 323                 f.setAccessible(true);
 324                 unsafe = (Unsafe) f.get(null);
 325             } catch (NoSuchFieldException | IllegalAccessException e) {
 326                 throw new RuntimeException("Unable to get Unsafe instance.", e);
 327             }
 328         }
 329         return unsafe;
 330     }
 331     private static final char[] hexArray = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
 332 
 333     /**
 334      * Returns hex view of byte array
 335      *
 336      * @param bytes byte array to process




 297     /**
 298      * Returns file content as a list of strings
 299      *
 300      * @param file File to operate on
 301      * @return List of strings
 302      * @throws IOException
 303      */
 304     public static List<String> fileAsList(File file) throws IOException {
 305         assertTrue(file.exists() && file.isFile(),
 306                 file.getAbsolutePath() + " does not exist or not a file");
 307         List<String> output = new ArrayList<>();
 308         try (BufferedReader reader = new BufferedReader(new FileReader(file.getAbsolutePath()))) {
 309             while (reader.ready()) {
 310                 output.add(reader.readLine().replace(NEW_LINE, ""));
 311             }
 312         }
 313         return output;
 314     }
 315 
 316     /**
 317      * Return the contents of the named file as a single String,
 318      * or null if not found.
 319      * @param filename name of the file to read
 320      * @return String contents of file, or null if file not found.
 321      */
 322     public static String fileAsString(String filename) {
 323         StringBuilder result = new StringBuilder();
 324         try {
 325             File file = new File(filename);
 326             if (file.exists()) {
 327                 BufferedReader reader = new BufferedReader(new FileReader(file));
 328                 while (true) {
 329                     String line = reader.readLine();
 330                     if (line == null) {
 331                         break;
 332                     }
 333                     result.append(line).append("\n");
 334                 }
 335             } else {
 336                 // Does not exist:
 337                 return null;
 338             }
 339         } catch (Exception e) {
 340             e.printStackTrace();
 341         }
 342         return result.toString();
 343     }
 344 
 345     /**
 346      * @return Unsafe instance.
 347      */
 348     public static synchronized Unsafe getUnsafe() {
 349         if (unsafe == null) {
 350             try {
 351                 Field f = Unsafe.class.getDeclaredField("theUnsafe");
 352                 f.setAccessible(true);
 353                 unsafe = (Unsafe) f.get(null);
 354             } catch (NoSuchFieldException | IllegalAccessException e) {
 355                 throw new RuntimeException("Unable to get Unsafe instance.", e);
 356             }
 357         }
 358         return unsafe;
 359     }
 360     private static final char[] hexArray = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
 361 
 362     /**
 363      * Returns hex view of byte array
 364      *
 365      * @param bytes byte array to process