/** * Get the stack trace from a {@link Throwable} as a {@link String}. * * <p> * The result of this method vary by JDK version as this method uses * {@link Throwable#printStackTrace(java.io.PrintWriter)}. On JDK1.3 and earlier, the cause exception will not be * shown unless the specified throwable alters printStackTrace. * </p> * * @param throwable the {@code Throwable} to get stack trace from. * @return the stack trace as a {@link String}. */ public static String getStackTrace(Throwable throwable) { StringWriter sw = null; PrintWriter pw = null; try { sw = new StringWriter(); pw = new PrintWriter(sw, true); throwable.printStackTrace(pw); return sw.getBuffer().toString(); } finally { Closeables.closeQuietly(sw, pw); } }
/** * Closes the given {@link Closeable}s, ignoring any thrown exceptions. * * @param closeables the {@code Closeable}s to close. */ public static void closeQuietly(Closeable... closeables) { for (Closeable c : closeables) { closeCloseable(c); } }
/** * Get the stack trace from a {@link Throwable} as a {@link String}. * * <p> * The result of this method vary by JDK version as this method uses * {@link Throwable#printStackTrace(java.io.PrintWriter)}. On JDK1.3 and earlier, the cause exception will not be * shown unless the specified throwable alters printStackTrace. * </p> * * @param throwable the {@code Throwable} to get stack trace from. * @return the stack trace as a {@link String}. */ public static String getStackTrace(Throwable throwable) { StringWriter sw = null; PrintWriter pw = null; try { sw = new StringWriter(); pw = new PrintWriter(sw, true); throwable.printStackTrace(pw); return sw.getBuffer().toString(); } finally { Closeables.closeQuietly(sw, pw); } }
/** * Closes the given {@link Closeable}s, ignoring any thrown exceptions. * * @param closeables the {@code Closeable}s to close. */ public static void closeQuietly(Closeable... closeables) { for (Closeable c : closeables) { closeCloseable(c); } }
protected Class<?> findClassInJarFile(String qualifiedClassName) throws ClassNotFoundException { URI classUri = URIUtil.buildUri(StandardLocation.CLASS_OUTPUT, qualifiedClassName); String internalClassName = classUri.getPath().substring(1); JarFile jarFile = null; try { for (int i = 0; i < jarFiles.size(); i++) { jarFile = jarFiles.get(i); JarEntry jarEntry = jarFile.getJarEntry(internalClassName); if (jarEntry != null) { InputStream inputStream = jarFile.getInputStream(jarEntry); try { byte[] byteCode = new byte[(int) jarEntry.getSize()]; ByteStreams.read(inputStream, byteCode, 0, byteCode.length); return defineClass(qualifiedClassName, byteCode, 0, byteCode.length); } finally { Closeables.closeQuietly(inputStream); } } } } catch (IOException e) { throw new IllegalStateException(String.format("Failed to lookup class %s in jar file %s", qualifiedClassName, jarFile), e); } return null; }
/** * Closes the given {@link Closeable}s, ignoring any thrown exceptions. * * @param closeables the {@code Closeable}s to close. */ public static void closeQuietly(Closeable... closeables) { for (Closeable c : closeables) { closeCloseable(c); } }
private List<Delta<String>> diff(BufferedReader actual, BufferedReader expected) throws IOException { try { List<String> actualLines = linesFromBufferedReader(actual); List<String> expectedLines = linesFromBufferedReader(expected); Patch<String> patch = DiffUtils.diff(expectedLines, actualLines); return unmodifiableList(patch.getDeltas()); } finally { closeQuietly(actual, expected); } }
private List<Delta<String>> diff(BufferedReader actual, BufferedReader expected) throws IOException { try { List<String> actualLines = linesFromBufferedReader(actual); List<String> expectedLines = linesFromBufferedReader(expected); Patch<String> patch = DiffUtils.diff(expectedLines, actualLines); return unmodifiableList(patch.getDeltas()); } finally { closeQuietly(actual, expected); } }
public void write(File file, Charset charset, String... content) throws IOException { PrintWriter writer = null; try { writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), charset)); for (String line : content) { writer.println(line); } } finally { closeQuietly(writer); } }
@Override @OverridingMethodsMustInvokeSuper public void close() { closeQuietly(processOutput, processError, processInput); } }
@Override @OverridingMethodsMustInvokeSuper public void close() { closeQuietly(processOutput, processError, processInput); } }
@Nonnull private KeyStrokeMappingProvider parse(@Nonnull InputStream input) throws IOException { List<KeyStrokeMapping> mappings = newArrayList(); BufferedReader reader = new BufferedReader(new InputStreamReader(input)); try { String line = reader.readLine(); while (line != null) { mappings.add(mappingFrom(line)); line = reader.readLine(); } return new ParsedKeyStrokeMappingProvider(mappings); } finally { closeQuietly(reader); } }
@VisibleForTesting public List<String> diff(File actual, File expected) throws IOException { BufferedReader reader1 = null; BufferedReader reader2 = null; try { reader1 = readerFor(actual); reader2 = readerFor(expected); return unmodifiableList(diff(reader1, reader2)); } finally { closeQuietly(reader1); closeQuietly(reader2); } }
@VisibleForTesting public List<String> diff(InputStream actual, InputStream expected) throws IOException { BufferedReader reader1 = null; BufferedReader reader2 = null; try { reader1 = readerFor(actual); reader2 = readerFor(expected); return unmodifiableList(diff(reader1, reader2)); } finally { closeQuietly(reader1); closeQuietly(reader2); } }
@VisibleForTesting public List<String> diff(File actual, String expected, Charset charset) throws IOException { BufferedReader reader1 = null; try { reader1 = readerFor(actual, charset); BufferedReader reader2 = readerFor(expected); return unmodifiableList(diff(reader1, reader2)); } finally { closeQuietly(reader1); } }