Search in sources :

Example 56 with UncheckedIOException

use of org.gradle.api.UncheckedIOException in project gradle by gradle.

the class DefaultClasspathEntryHasher method hashJar.

private HashCode hashJar(FileDetails fileDetails, Hasher hasher, ClasspathContentHasher classpathContentHasher) {
    File jarFilePath = new File(fileDetails.getPath());
    ZipInputStream zipInput = null;
    try {
        zipInput = new ZipInputStream(new FileInputStream(jarFilePath));
        ZipEntry zipEntry;
        Multimap<String, HashCode> entriesByName = MultimapBuilder.hashKeys().arrayListValues().build();
        while ((zipEntry = zipInput.getNextEntry()) != null) {
            if (zipEntry.isDirectory()) {
                continue;
            }
            HashCode hash = hashZipEntry(zipInput, zipEntry, classpathContentHasher);
            if (hash != null) {
                entriesByName.put(zipEntry.getName(), hash);
            }
        }
        Map<String, Collection<HashCode>> map = entriesByName.asMap();
        // Ensure we hash the zip entries in a deterministic order
        String[] keys = map.keySet().toArray(new String[0]);
        Arrays.sort(keys);
        for (String key : keys) {
            for (HashCode hash : map.get(key)) {
                hasher.putBytes(hash.asBytes());
            }
        }
        return hasher.hash();
    } catch (ZipException e) {
        // ZipExceptions point to a problem with the Zip, we try to be lenient for now.
        return hashMalformedZip(fileDetails, hasher, classpathContentHasher);
    } catch (IOException e) {
        // IOExceptions other than ZipException are failures.
        throw new UncheckedIOException("Error snapshotting jar [" + fileDetails.getName() + "]", e);
    } catch (Exception e) {
        // Other Exceptions can be thrown by invalid zips, too. See https://github.com/gradle/gradle/issues/1581.
        return hashMalformedZip(fileDetails, hasher, classpathContentHasher);
    } finally {
        IOUtils.closeQuietly(zipInput);
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) ZipException(java.util.zip.ZipException) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(org.gradle.api.UncheckedIOException) FileInputStream(java.io.FileInputStream) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) UncheckedIOException(org.gradle.api.UncheckedIOException) ZipInputStream(java.util.zip.ZipInputStream) HashCode(com.google.common.hash.HashCode) Collection(java.util.Collection) File(java.io.File)

Example 57 with UncheckedIOException

use of org.gradle.api.UncheckedIOException in project gradle by gradle.

the class DefaultTemporaryFileProvider method createTemporaryDirectory.

public File createTemporaryDirectory(@Nullable String prefix, @Nullable String suffix, @Nullable String... path) {
    File dir = new File(baseDirFactory.create(), CollectionUtils.join("/", path));
    GFileUtils.mkdirs(dir);
    try {
        // TODO: This is not a great paradigm for creating a temporary directory.
        // See http://guava-libraries.googlecode.com/svn/tags/release08/javadoc/com/google/common/io/Files.html#createTempDir%28%29 for an alternative.
        File tmpDir = File.createTempFile("gradle", "projectDir", dir);
        tmpDir.delete();
        tmpDir.mkdir();
        return tmpDir;
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(org.gradle.api.UncheckedIOException) File(java.io.File)

Example 58 with UncheckedIOException

use of org.gradle.api.UncheckedIOException in project gradle by gradle.

the class FileNormaliser method normalise.

// normalizes a path in similar ways as File.getCanonicalFile(), except that it
// does NOT resolve symlinks (by design)
public File normalise(File file) {
    try {
        if (!file.isAbsolute()) {
            throw new IllegalArgumentException(String.format("Cannot normalize a relative file: '%s'", file));
        }
        if (isWindowsOs) {
            // on Windows, File.getCanonicalFile() doesn't resolve symlinks
            return file.getCanonicalFile();
        }
        File candidate;
        String filePath = file.getPath();
        List<String> path = null;
        if (isNormalisingRequiredForAbsolutePath(filePath)) {
            path = splitAndNormalisePath(filePath);
            String resolvedPath = CollectionUtils.join(File.separator, path);
            boolean needLeadingSeparator = File.listRoots()[0].getPath().startsWith(File.separator);
            if (needLeadingSeparator) {
                resolvedPath = File.separator + resolvedPath;
            }
            candidate = new File(resolvedPath);
        } else {
            candidate = file;
        }
        // If the file system is case sensitive, we don't have to normalise it further
        if (fileSystem.isCaseSensitive()) {
            return candidate;
        }
        // Short-circuit the slower lookup method by using the canonical file
        File canonical = candidate.getCanonicalFile();
        if (candidate.getPath().equalsIgnoreCase(canonical.getPath())) {
            return canonical;
        }
        // TODO - start resolving only from where the expected and canonical paths are different
        if (path == null) {
            path = splitAndNormalisePath(filePath);
        }
        return normaliseUnixPathIgnoringCase(path);
    } catch (IOException e) {
        throw new UncheckedIOException(String.format("Could not normalize path for file '%s'.", file), e);
    }
}
Also used : UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(org.gradle.api.UncheckedIOException) File(java.io.File)

Example 59 with UncheckedIOException

use of org.gradle.api.UncheckedIOException in project gradle by gradle.

the class FileTimeStampInspector method updateOnFinishBuild.

protected void updateOnFinishBuild() {
    markerFile.getParentFile().mkdirs();
    try {
        FileOutputStream outputStream = new FileOutputStream(markerFile);
        try {
            outputStream.write(0);
        } finally {
            outputStream.close();
        }
    } catch (IOException e) {
        throw new UncheckedIOException("Could not update " + markerFile, e);
    }
    lastBuildTimestamp = markerFile.lastModified();
}
Also used : FileOutputStream(java.io.FileOutputStream) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(org.gradle.api.UncheckedIOException)

Example 60 with UncheckedIOException

use of org.gradle.api.UncheckedIOException in project gradle by gradle.

the class DefaultTemplateBasedStartScriptGenerator method generateScript.

public void generateScript(JavaAppStartScriptGenerationDetails details, Writer destination) {
    try {
        Map<String, String> binding = bindingFactory.transform(details);
        String scriptContent = generateStartScriptContentFromTemplate(binding);
        destination.write(scriptContent);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : UncheckedIOException(org.gradle.api.UncheckedIOException) UncheckedIOException(org.gradle.api.UncheckedIOException)

Aggregations

UncheckedIOException (org.gradle.api.UncheckedIOException)101 IOException (java.io.IOException)79 File (java.io.File)32 InputStream (java.io.InputStream)9 FileOutputStream (java.io.FileOutputStream)7 OutputStream (java.io.OutputStream)7 BufferedReader (java.io.BufferedReader)6 StringReader (java.io.StringReader)6 FileInputStream (java.io.FileInputStream)5 Matcher (java.util.regex.Matcher)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 URI (java.net.URI)4 URL (java.net.URL)4 ArrayList (java.util.ArrayList)4 Manifest (java.util.jar.Manifest)4 ZipInputStream (java.util.zip.ZipInputStream)4 FileVisitDetails (org.gradle.api.file.FileVisitDetails)4 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 FileReader (java.io.FileReader)3