下面列出了com.google.common.io.InputSupplier#org.gradle.api.UncheckedIOException 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void execute(Action<? super BufferedWriter> action) {
try {
File parentFile = file.getParentFile();
if (parentFile != null) {
if (!parentFile.mkdirs() && !parentFile.isDirectory()) {
throw new IOException(String.format("Unable to create directory '%s'", parentFile));
}
}
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), encoding));
try {
action.execute(writer);
} finally {
writer.close();
}
} catch (Exception e) {
throw new UncheckedIOException(String.format("Could not write to file '%s'.", file), e);
}
}
public Properties create() {
URL resource = classLoader.getResource(resourceName);
if (resource == null) {
throw new RuntimeException(
"Unable to find the released versions information.\n"
+ "The resource '" + resourceName + "' was not found.\n"
+ "Most likely, you haven't run the 'prepareVersionsInfo' task.\n"
+ "If you have trouble running tests from your IDE, please run gradlew idea|eclipse first."
);
}
try {
Properties properties = new Properties();
InputStream stream = resource.openStream();
try {
properties.load(stream);
} finally {
stream.close();
}
return properties;
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public static Scriptable parse(File source, String encoding, Action<Context> contextConfig) {
Context context = Context.enter();
if (contextConfig != null) {
contextConfig.execute(context);
}
Scriptable scope = context.initStandardObjects();
try {
Reader reader = new InputStreamReader(new FileInputStream(source), encoding);
try {
context.evaluateReader(scope, reader, source.getName(), 0, null);
} finally {
reader.close();
}
} catch (IOException e) {
throw new UncheckedIOException(e);
} finally {
Context.exit();
}
return scope;
}
@TaskAction
void doEvaluate() {
HttpFileServer fileServer = new SimpleHttpFileServerFactory().start(getContent(), 0);
try {
Writer resultWriter = new FileWriter(getResult());
getEvaluator().evaluate(fileServer.getResourceUrl(getResource()), resultWriter);
resultWriter.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
} finally {
fileServer.stop();
}
setDidWork(true);
}
@TaskAction
void doEvaluate() {
HttpFileServer fileServer = new SimpleHttpFileServerFactory().start(getContent(), 0);
try {
Writer resultWriter = new FileWriter(getResult());
getEvaluator().evaluate(fileServer.getResourceUrl(getResource()), resultWriter);
resultWriter.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
} finally {
fileServer.stop();
}
setDidWork(true);
}
/**
* Saves the settings of the file chooser; and by settings I mean the 'last visited directory'.
*
* @param saveCurrentDirectoryVsSelectedFilesParent this should be true if you're selecting only directories, false if you're selecting only files. I don't know what if you allow both.
*/
public static void saveSettings(SettingsNode settingsNode, JFileChooser fileChooser, String id, Class fileChooserClass, boolean saveCurrentDirectoryVsSelectedFilesParent) {
SettingsNode childNode = settingsNode.addChildIfNotPresent(getPrefix(fileChooserClass, id));
String save;
try {
if (saveCurrentDirectoryVsSelectedFilesParent) {
save = fileChooser.getCurrentDirectory().getCanonicalPath();
} else {
save = fileChooser.getSelectedFile().getCanonicalPath();
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
if (save != null) {
childNode.setValueOfChild(DIRECTORY_NAME, save);
}
}
public void publish(List<ModuleVersionPublisher> publishResolvers,
IvyModulePublishMetaData publishMetaData) {
try {
// Make a copy of the publication and filter missing artifacts
DefaultIvyModulePublishMetaData publication = new DefaultIvyModulePublishMetaData(publishMetaData.getId());
for (IvyModuleArtifactPublishMetaData artifact: publishMetaData.getArtifacts()) {
addPublishedArtifact(artifact, publication);
}
for (ModuleVersionPublisher publisher : publishResolvers) {
logger.info("Publishing to {}", publisher);
publisher.publish(publication);
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
protected void transformArgs(List<String> input, List<String> output, File tempDir) {
GFileUtils.mkdirs(tempDir);
File optionsFile = new File(tempDir, "options.txt");
try {
PrintWriter writer = new PrintWriter(optionsFile);
try {
ArgWriter argWriter = argWriterFactory.transform(writer);
argWriter.args(input);
} finally {
IOUtils.closeQuietly(writer);
}
} catch (IOException e) {
throw new UncheckedIOException(String.format("Could not write compiler options file '%s'.", optionsFile.getAbsolutePath()), e);
}
output.add(String.format("@%s", optionsFile.getAbsolutePath()));
}
public Properties create() {
URL resource = classLoader.getResource(resourceName);
if (resource == null) {
throw new RuntimeException(
"Unable to find the released versions information.\n"
+ "The resource '" + resourceName + "' was not found.\n"
+ "Most likely, you haven't ran the 'prepareVersionsInfo' task.\n"
+ "If you have trouble running tests from your IDE, please run gradlew idea|eclipse first."
);
}
try {
Properties properties = new Properties();
InputStream stream = resource.openStream();
try {
properties.load(stream);
} finally {
stream.close();
}
return properties;
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
private Iterable<String> shortenArgs(File tempDir, List<String> args) {
File file = new File(tempDir, "java-compiler-args.txt");
// for command file format, see http://docs.oracle.com/javase/6/docs/technotes/tools/windows/javac.html#commandlineargfile
// use platform character and line encoding
try {
PrintWriter writer = new PrintWriter(new FileWriter(file));
try {
ArgWriter argWriter = ArgWriter.unixStyle(writer);
for (String arg : args) {
argWriter.args(arg);
}
} finally {
writer.close();
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return Collections.singleton("@" + file.getPath());
}
private void maybeConfigureFrom(File propertiesFile, Map<String, String> result) {
if (!propertiesFile.isFile()) {
return;
}
Properties properties = new Properties();
try {
FileInputStream inputStream = new FileInputStream(propertiesFile);
try {
properties.load(inputStream);
} finally {
inputStream.close();
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
for (Object key : properties.keySet()) {
if (GradleProperties.ALL.contains(key.toString())) {
result.put(key.toString(), properties.get(key).toString());
}
}
}
private void parseFile(File file, DefaultSourceDetails sourceDetails) {
try {
BufferedReader bf = new BufferedReader(new PreprocessingReader(new BufferedReader(new FileReader(file))));
try {
String line;
while ((line = bf.readLine()) != null) {
Matcher m = includePattern.matcher(line.trim());
if (m.matches()) {
boolean isImport = "import".equals(m.group(1));
String value = m.group(2);
if (isImport) {
sourceDetails.getImports().add(value);
} else {
sourceDetails.getIncludes().add(value);
}
}
}
} finally {
IOUtils.closeQuietly(bf);
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
/**
* Saves the settings of the file chooser; and by settings I mean the 'last visited directory'.
*
* @param saveCurrentDirectoryVsSelectedFilesParent this should be true true if you're selecting only directories, false if you're selecting only files. I don't know what if you allow both.
*/
public static void saveSettings(SettingsNode settingsNode, JFileChooser fileChooser, String id, Class fileChooserClass, boolean saveCurrentDirectoryVsSelectedFilesParent) {
SettingsNode childNode = settingsNode.addChildIfNotPresent(getPrefix(fileChooserClass, id));
String save;
try {
if (saveCurrentDirectoryVsSelectedFilesParent) {
save = fileChooser.getCurrentDirectory().getCanonicalPath();
} else {
save = fileChooser.getSelectedFile().getCanonicalPath();
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
if (save != null) {
childNode.setValueOfChild(DIRECTORY_NAME, save);
}
}
private static List<File> findAvailableClasspathFiles(ClassLoader classLoader) {
List<URL> rawClasspath = ClasspathUtil.getClasspath(classLoader);
List<File> classpathFiles = new ArrayList<File>();
for (URL url : rawClasspath) {
if (url.getProtocol().equals("file")) {
try {
File classpathFile = new File(url.toURI());
addClasspathFile(classpathFile, classpathFiles);
} catch (URISyntaxException e) {
throw new UncheckedIOException(e);
}
}
}
// The file names passed to -cp are canonicalised by the JVM when it creates the system classloader, and so the file names are
// lost if they happen to refer to links, for example, into the Gradle artifact cache. Try to reconstitute the file names
// from the system classpath
if (classLoader == ClassLoader.getSystemClassLoader()) {
for (String value : System.getProperty("java.class.path").split(File.pathSeparator)) {
addClasspathFile(new File(value), classpathFiles);
}
}
return classpathFiles;
}
private static List<File> findAvailableClasspathFiles(ClassLoader classLoader) {
List<URL> rawClasspath = ClasspathUtil.getClasspath(classLoader);
List<File> classpathFiles = new ArrayList<File>();
for (URL url : rawClasspath) {
if (url.getProtocol().equals("file")) {
try {
File classpathFile = new File(url.toURI());
addClasspathFile(classpathFile, classpathFiles);
} catch (URISyntaxException e) {
throw new UncheckedIOException(e);
}
}
}
// The file names passed to -cp are canonicalised by the JVM when it creates the system classloader, and so the file names are
// lost if they happen to refer to links, for example, into the Gradle artifact cache. Try to reconstitute the file names
// from the system classpath
if (classLoader == ClassLoader.getSystemClassLoader()) {
for (String value : System.getProperty("java.class.path").split(File.pathSeparator)) {
addClasspathFile(new File(value), classpathFiles);
}
}
return classpathFiles;
}
public DefaultClassPath create() {
File markerFile = new File(cache.getBaseDir(), "built.bin");
final boolean rebuild = !markerFile.exists();
BuildSrcBuildListenerFactory.Listener listener = listenerFactory.create(rebuild);
gradleLauncher.addListener(listener);
gradleLauncher.run().rethrowFailure();
Collection<File> classpath = listener.getRuntimeClasspath();
LOGGER.debug("Gradle source classpath is: {}", classpath);
LOGGER.info("================================================" + " Finished building buildSrc");
try {
markerFile.createNewFile();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return new DefaultClassPath(classpath);
}
public void write(Collection<TestClassResult> results) {
try {
OutputStream outputStream = new FileOutputStream(resultsFile);
try {
if (!results.isEmpty()) { // only write if we have results, otherwise truncate
FlushableEncoder encoder = new KryoBackedEncoder(outputStream);
encoder.writeSmallInt(RESULT_VERSION);
write(results, encoder);
encoder.flush();
}
} finally {
outputStream.close();
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public void publish(List<ModuleVersionPublisher> publishResolvers,
ModuleVersionPublishMetaData publishMetaData) {
try {
// Make a copy of the publication and filter missing artifacts
DefaultModuleVersionPublishMetaData publication = new DefaultModuleVersionPublishMetaData(publishMetaData.getId());
for (ModuleVersionArtifactPublishMetaData artifact: publishMetaData.getArtifacts()) {
addPublishedArtifact(artifact, publication);
}
for (ModuleVersionPublisher publisher : publishResolvers) {
logger.info("Publishing to {}", publisher);
publisher.publish(publication);
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public static Scriptable parse(File source, String encoding, Action<Context> contextConfig) {
Context context = Context.enter();
if (contextConfig != null) {
contextConfig.execute(context);
}
Scriptable scope = context.initStandardObjects();
try {
Reader reader = new InputStreamReader(new FileInputStream(source), encoding);
try {
context.evaluateReader(scope, reader, source.getName(), 0, null);
} finally {
reader.close();
}
} catch (IOException e) {
throw new UncheckedIOException(e);
} finally {
Context.exit();
}
return scope;
}
public void execute(Action<? super BufferedWriter> action) {
try {
File parentFile = file.getParentFile();
if (parentFile != null) {
if (!parentFile.mkdirs() && !parentFile.isDirectory()) {
throw new IOException(String.format("Unable to create directory '%s'", parentFile));
}
}
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), encoding));
try {
action.execute(writer);
} finally {
writer.close();
}
} catch (Exception e) {
throw new UncheckedIOException(String.format("Could not write to file '%s'.", file), e);
}
}
@TaskAction
public void generate() {
try {
ReportRenderer renderer = getRenderer();
File outputFile = getOutputFile();
if (outputFile != null) {
renderer.setOutputFile(outputFile);
} else {
renderer.setOutput(getServices().get(StyledTextOutputFactory.class).create(getClass()));
}
Set<Project> projects = new TreeSet<Project>(getProjects());
for (Project project : projects) {
renderer.startProject(project);
generate(project);
renderer.completeProject(project);
}
renderer.complete();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
private Iterable<String> shortenArgs(List<String> args) {
File file = tempFileProvider.createTemporaryFile("compile-args", null, "java-compiler");
// for command file format, see http://docs.oracle.com/javase/6/docs/technotes/tools/windows/javac.html#commandlineargfile
// use platform character and line encoding
try {
PrintWriter writer = new PrintWriter(new FileWriter(file));
try {
ArgWriter argWriter = ArgWriter.unixStyle(writer);
for (String arg : args) {
argWriter.args(arg);
}
} finally {
writer.close();
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return Collections.singleton("@" + file.getPath());
}
private void deleteOutputs(Path pathToBeDeleted) {
try {
Files.walk(pathToBeDeleted).map(path -> path.toFile()).forEach(file -> file.delete());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public static void copyURLToFile(URL source, File destination) {
try {
FileUtils.copyURLToFile(source, destination);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public File createTemporaryFile(String prefix, @Nullable String suffix, String... path) {
File dir = new File(baseDirFactory.create(), CollectionUtils.join("/", path));
GFileUtils.mkdirs(dir);
try {
return File.createTempFile(prefix, suffix, dir);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public void describeTo(Appendable appendable) {
try {
appendable.append(getDescription());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public void supply(InstallDeployTaskSupport installDeployTaskSupport) {
try {
settingsXml = temporaryFileProvider.createTemporaryFile("gradle_empty_settings", ".xml");
FileUtils.writeStringToFile(settingsXml, "<settings/>");
settingsXml.deleteOnExit();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
installDeployTaskSupport.setSettingsFile(settingsXml);
}
public DefaultMavenPom writeTo(final Writer pomWriter) {
try {
getEffectivePom().writeNonEffectivePom(pomWriter);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return this;
}
public MavenPomFileGenerator writeTo(File file) {
xmlTransformer.transform(file, POM_FILE_ENCODING, new Action<Writer>() {
public void execute(Writer writer) {
try {
mavenProject.writeModel(writer);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
});
return this;
}
private void render(Action<Ansi> action) {
Ansi ansi = createAnsi();
action.execute(ansi);
try {
target.append(ansi.toString());
flushable.flush();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}