public static void sleep(long delay, boolean ignore) { try { Thread.sleep(delay); } catch (InterruptedException e) { if (!ignore) { ExceptionUtil.throwRuntime(e); } } }
public static List<String> loadLines(InputStream stream, Charset charset) { try (BufferedReader br = new BufferedReader(new InputStreamReader(stream, charset))) { return loadLines(br); } catch (IOException e) { ExceptionUtil.throwRuntime(e); } return Collections.emptyList(); }
public static Path getResourcePath(String path) { URL url = IOUtil.class.getResource(path); if (url == null) { throw new ResourceNotFoundException("Resource not found: " + path); } try { return Paths.get(url.toURI()); } catch (URISyntaxException e) { ExceptionUtil.throwRuntime(e); } return null; }
public void runTask(Class<? extends Task> taskClazz) { try { Task task = taskClazz.newInstance(); runTask(task); } catch (InstantiationException | IllegalAccessException e) { ExceptionUtil.throwRuntime(e); } }
public static String loadText(Reader reader, int length) { char[] chars = new char[length]; int count = 0; while (count < chars.length) { int n = 0; try { n = reader.read(chars, count, chars.length - count); } catch (IOException e) { ExceptionUtil.throwRuntime(e); } if (n <= 0) break; count += n; } if (count == chars.length) { return new String(chars); } char[] newChars = new char[count]; System.arraycopy(chars, 0, newChars, 0, count); return new String(newChars); }
public static boolean canWrite(String path) { if (SystemInfo.isWindows) { try { DosFileAttributes attributes = Files.readAttributes(Paths.get(path), DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS); return attributes.isDirectory() || !attributes.isReadOnly(); } catch (IOException e) { ExceptionUtil.throwRuntime(e); } } return new File(path).canWrite(); }
public static void setFactory(Class<? extends LoggerFactory> factory) { if (isInitialized()) { if (factory.isInstance(Logger.factory)) { return; } System.out.println("Changing log factory\n" + ExceptionUtil.toString(new Throwable())); } try { Logger.factory = factory.newInstance(); } catch (Throwable e) { ExceptionUtil.throwRuntime(e); } }