Search in sources :

Example 36 with UncheckedIOException

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

the class FileOrUriNotationConverter method convert.

public void convert(Object notation, NotationConvertResult<? super Object> result) throws TypeConversionException {
    if (notation instanceof File) {
        result.converted(notation);
        return;
    }
    if (notation instanceof Path) {
        result.converted(((Path) notation).toFile());
        return;
    }
    if (notation instanceof FileSystemLocation) {
        result.converted(((FileSystemLocation) notation).getAsFile());
        return;
    }
    if (notation instanceof URL) {
        try {
            notation = ((URL) notation).toURI();
        } catch (URISyntaxException e) {
            throw new UncheckedIOException(e);
        }
    }
    if (notation instanceof URI) {
        URI uri = (URI) notation;
        if ("file".equals(uri.getScheme()) && uri.getPath() != null) {
            result.converted(new File(uri.getPath()));
        } else {
            result.converted(uri);
        }
        return;
    }
    if (notation instanceof CharSequence) {
        String notationString = notation.toString();
        if (notationString.startsWith("file:")) {
            result.converted(new File(uriDecode(notationString.substring(5))));
            return;
        }
        for (File file : File.listRoots()) {
            String rootPath = file.getAbsolutePath();
            String normalisedStr = notationString;
            if (!fileSystem.isCaseSensitive()) {
                rootPath = rootPath.toLowerCase();
                normalisedStr = normalisedStr.toLowerCase();
            }
            if (normalisedStr.startsWith(rootPath) || normalisedStr.startsWith(rootPath.replace(File.separatorChar, '/'))) {
                result.converted(new File(notationString));
                return;
            }
        }
        // Check if string starts with a URI scheme
        if (URI_SCHEME.matcher(notationString).matches()) {
            try {
                result.converted(new URI(notationString));
                return;
            } catch (URISyntaxException e) {
                throw new UncheckedIOException(e);
            }
        }
        result.converted(new File(notationString));
    }
}
Also used : Path(java.nio.file.Path) FileSystemLocation(org.gradle.api.file.FileSystemLocation) UncheckedIOException(org.gradle.api.UncheckedIOException) URISyntaxException(java.net.URISyntaxException) File(java.io.File) URI(java.net.URI) URL(java.net.URL)

Example 37 with UncheckedIOException

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

the class FileToArchiveEntrySetTransformer method walk.

private ImmutableSet<ArchiveEntry> walk(InputStream archiveInputStream, ImmutableSet.Builder<ArchiveEntry> allEntries, ImmutableList<String> parentPaths) {
    ImmutableSet.Builder<ArchiveEntry> entries = ImmutableSet.builder();
    ZipInputStream zipStream = new ZipInputStream(archiveInputStream);
    try {
        ZipEntry entry = zipStream.getNextEntry();
        while (entry != null) {
            ArchiveEntry.Builder builder = new ArchiveEntry.Builder();
            builder.setParentPaths(parentPaths);
            builder.setPath(entry.getName());
            builder.setCrc(entry.getCrc());
            builder.setDirectory(entry.isDirectory());
            builder.setSize(entry.getSize());
            if (!builder.isDirectory() && (zipStream.available() == 1)) {
                boolean zipEntry;
                final BufferedInputStream bis = new BufferedInputStream(zipStream) {

                    @Override
                    public void close() throws IOException {
                    }
                };
                bis.mark(Integer.MAX_VALUE);
                zipEntry = new ZipInputStream(bis).getNextEntry() != null;
                bis.reset();
                if (zipEntry) {
                    ImmutableList<String> nextParentPaths = ImmutableList.<String>builder().addAll(parentPaths).add(entry.getName()).build();
                    ImmutableSet<ArchiveEntry> subEntries = walk(bis, allEntries, nextParentPaths);
                    builder.setSubEntries(subEntries);
                }
            }
            ArchiveEntry archiveEntry = builder.build();
            entries.add(archiveEntry);
            allEntries.add(archiveEntry);
            zipStream.closeEntry();
            entry = zipStream.getNextEntry();
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } finally {
        IOUtils.closeQuietly(zipStream);
    }
    return entries.build();
}
Also used : ZipEntry(java.util.zip.ZipEntry) UncheckedIOException(org.gradle.api.UncheckedIOException) UncheckedIOException(org.gradle.api.UncheckedIOException) ZipInputStream(java.util.zip.ZipInputStream) ImmutableSet(com.google.common.collect.ImmutableSet)

Example 38 with UncheckedIOException

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

the class Wrapper method writeProperties.

private void writeProperties(File propertiesFileDestination) {
    Properties wrapperProperties = new Properties();
    wrapperProperties.put(WrapperExecutor.DISTRIBUTION_URL_PROPERTY, getDistributionUrl());
    if (distributionSha256Sum != null) {
        wrapperProperties.put(WrapperExecutor.DISTRIBUTION_SHA_256_SUM, distributionSha256Sum);
    }
    wrapperProperties.put(WrapperExecutor.DISTRIBUTION_BASE_PROPERTY, distributionBase.toString());
    wrapperProperties.put(WrapperExecutor.DISTRIBUTION_PATH_PROPERTY, distributionPath);
    wrapperProperties.put(WrapperExecutor.ZIP_STORE_BASE_PROPERTY, archiveBase.toString());
    wrapperProperties.put(WrapperExecutor.ZIP_STORE_PATH_PROPERTY, archivePath);
    try {
        PropertiesUtils.store(wrapperProperties, propertiesFileDestination);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : UncheckedIOException(org.gradle.api.UncheckedIOException) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) Properties(java.util.Properties)

Example 39 with UncheckedIOException

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

the class GUtil method loadProperties.

public static Properties loadProperties(URL url) {
    try {
        URLConnection uc = url.openConnection();
        uc.setUseCaches(false);
        return loadProperties(uc.getInputStream());
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : UncheckedIOException(org.gradle.api.UncheckedIOException) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) URLConnection(java.net.URLConnection)

Example 40 with UncheckedIOException

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

the class RuntimeShadedJarCreator method processDirectory.

private void processDirectory(final ZipOutputStream outputStream, File file, final byte[] buffer, final HashSet<String> seenPaths, final Map<String, List<String>> services) {
    final List<FileVisitDetails> fileVisitDetails = new ArrayList<FileVisitDetails>();
    directoryFileTreeFactory.create(file).visit(new FileVisitor() {

        @Override
        public void visitDir(FileVisitDetails dirDetails) {
            fileVisitDetails.add(dirDetails);
        }

        @Override
        public void visitFile(FileVisitDetails fileDetails) {
            fileVisitDetails.add(fileDetails);
        }
    });
    // We need to sort here since the file order obtained from the filesystem
    // can change between machines and we always want to have the same shaded jars.
    Collections.sort(fileVisitDetails, new Comparator<FileVisitDetails>() {

        @Override
        public int compare(FileVisitDetails o1, FileVisitDetails o2) {
            return o1.getPath().compareTo(o2.getPath());
        }
    });
    for (FileVisitDetails details : fileVisitDetails) {
        try {
            if (details.isDirectory()) {
                ZipEntry zipEntry = newZipEntryWithFixedTime(details.getPath() + "/");
                processEntry(outputStream, null, zipEntry, buffer, seenPaths, services);
            } else {
                ZipEntry zipEntry = newZipEntryWithFixedTime(details.getPath());
                InputStream inputStream = details.open();
                try {
                    processEntry(outputStream, inputStream, zipEntry, buffer, seenPaths, services);
                } finally {
                    inputStream.close();
                }
            }
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }
}
Also used : FileVisitDetails(org.gradle.api.file.FileVisitDetails) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) FileVisitor(org.gradle.api.file.FileVisitor) UncheckedIOException(org.gradle.api.UncheckedIOException) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException)

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