static void toStream(Serializable obj, OutputStream stream) { try (ObjectOutputStream objectOutput = new ObjectOutputStream(stream)) { objectOutput.writeObject(obj); } catch (IOException e) { throw Errors.asRuntime(e); } }
private static byte[] toByteArray(InputStream in) { ByteArrayOutputStream to = new ByteArrayOutputStream(); byte[] buf = new byte[8192]; try { while (true) { int r = in.read(buf); if (r == -1) { break; } to.write(buf, 0, r); } return to.toByteArray(); } catch (IOException e) { throw Errors.asRuntime(e); } }
@SuppressWarnings("unchecked") static <T> T fromStream(Class<T> clazz, InputStream stream) { try (ObjectInputStream objectInput = new ObjectInputStream(stream)) { T object = (T) objectInput.readObject(); Preconditions.checkArgument(clazz.isInstance(object), "Requires class %s, was %s", clazz, object); return object; } catch (ClassNotFoundException | IOException e) { throw Errors.asRuntime(e); } } }
/** Returns the error message. */ public String getMessage() { try { Objects.requireNonNull(runToFix, "runToFix"); Objects.requireNonNull(formatter, "formatter"); Objects.requireNonNull(problemFiles, "problemFiles"); DiffMessageFormatter diffFormater = new DiffMessageFormatter(this); return "The following files had format violations:\n" + diffFormater.buffer + runToFix; } catch (IOException e) { throw Errors.asRuntime(e); } }
/** * Load class based on the given configuration of JAR provider and Maven coordinates. * Different class loader instances are provided in the following scenarios: * <ol> * <li>The JARs ({@link #jarState}) have changes (this should only occur during development)</li> * <li>Different configurations ({@link #settingsFiles}) are used for different sub-projects</li> * <li>The same Eclipse step implementation provides different formatter types ({@link #formatterStepExt})</li> * </ol> */ public Class<?> loadClass(String name) { try { return jarState.getClassLoader(this).loadClass(name); } catch (ClassNotFoundException e) { throw Errors.asRuntime(e); } } }
@SuppressWarnings("unchecked") private <T extends FormatExtension> T maybeCreate(String name, Class<T> clazz) { FormatExtension existing = formats.get(name); if (existing != null) { if (!existing.getClass().equals(clazz)) { throw new GradleException("Tried to add format named '" + name + "'" + " of type " + clazz + " but one has already been created of type " + existing.getClass()); } else { return (T) existing; } } else { try { Constructor<T> constructor = clazz.getConstructor(SpotlessExtension.class); T newlyCreated = constructor.newInstance(this); formats.put(name, newlyCreated); return newlyCreated; } catch (NoSuchMethodException e) { throw new GradleException("Must have a constructor " + clazz.getSimpleName() + "(SpotlessExtension root)", e); } catch (Exception e) { throw Errors.asRuntime(e); } } } }
static void toFile(Serializable obj, File file) { try { java.nio.file.Files.createDirectories(file.getParentFile().toPath()); try (OutputStream output = Files.asByteSink(file).openBufferedStream()) { toStream(obj, output); } } catch (IOException e) { throw Errors.asRuntime(e); } }
static <T extends Serializable> T fromFile(Class<T> clazz, File file) { try (InputStream input = Files.asByteSource(file).openBufferedStream()) { return fromStream(clazz, input); } catch (IOException e) { throw Errors.asRuntime(e); } }
private static RuntimeException rethrowErrorAndWrapOthersAsRuntime(Throwable e) { if (e instanceof Error) { throw (Error) e; } else { return Errors.asRuntime(e); } }
private static RuntimeException rethrowErrorAndWrapOthersAsRuntime(Throwable e) { if (e instanceof Error) { throw (Error) e; } else { return Errors.asRuntime(e); } }
private static RuntimeException rethrowErrorAndWrapOthersAsRuntime(Throwable e) { if (e instanceof Error) { throw (Error) e; } else { return Errors.asRuntime(e); } }
static void toStream(Serializable obj, OutputStream stream) { try (ObjectOutputStream objectOutput = new ObjectOutputStream(stream)) { objectOutput.writeObject(obj); } catch (IOException e) { throw Errors.asRuntime(e); } }
static byte[] toBytes(Serializable obj) { ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); try (ObjectOutputStream objectOutput = new ObjectOutputStream(byteOutput)) { objectOutput.writeObject(obj); } catch (IOException e) { throw Errors.asRuntime(e); } return byteOutput.toByteArray(); } }
public static ParsedSVG parse(File file) { try (InputStream iconDocumentStream = new BufferedInputStream(new FileInputStream(file))) { return new ParsedSVG(iconDocumentStream); } catch (IOException e) { throw Errors.asRuntime(e); } }
@SuppressWarnings("unchecked") static <T> T fromStream(Class<T> clazz, InputStream stream) { try (ObjectInputStream objectInput = new ObjectInputStream(stream)) { T object = (T) objectInput.readObject(); Preconditions.checkArgument(clazz.isInstance(object), "Requires class %s, was %s", clazz, object); return object; } catch (ClassNotFoundException | IOException e) { throw Errors.asRuntime(e); } } }
/** Runs the display loop until the given `Predicate<Display>` returns false. */ public static void loopUntil(Predicate<Display> until) { Display display = assertUI(); while (!until.test(display)) { try { if (!display.readAndDispatch()) { display.sleep(); } } catch (Throwable e) { throw Errors.asRuntime(e); } } }
static <T extends Serializable> T fromFile(Class<T> clazz, File file) { try (InputStream input = Files.asByteSource(file).openBufferedStream()) { return fromStream(clazz, input); } catch (IOException e) { throw Errors.asRuntime(e); } }
static void toFile(Serializable obj, File file) { try { FileMisc.mkdirs(file.getParentFile()); try (OutputStream output = Files.asByteSink(file).openBufferedStream()) { toStream(obj, output); } } catch (IOException e) { throw Errors.asRuntime(e); } }
/** Runs the display loop until the given future has returned. */ public static <T> T loopUntilGet(CompletionStage<T> future) { Box.Nullable<T> result = Box.Nullable.ofNull(); Box.Nullable<Throwable> error = Box.Nullable.ofNull(); Rx.subscribe(future, Rx.onValueOnFailure(result::set, error::set)); CompletableFuture<?> actualFuture = future.toCompletableFuture(); loopUntil(display -> actualFuture.isDone()); if (error.get() != null) { throw Errors.asRuntime(error.get()); } else { return result.get(); } }