Example usage for java.nio.file Files size

List of usage examples for java.nio.file Files size

Introduction

In this page you can find the example usage for java.nio.file Files size.

Prototype

public static long size(Path path) throws IOException 

Source Link

Document

Returns the size of a file (in bytes).

Usage

From source file:org.tallison.cc.CCGetter.java

private void fetch(CCIndexRecord r, Path rootDir, BufferedWriter writer) throws IOException {
    Path targFile = rootDir.resolve(r.getDigest().substring(0, 2) + "/" + r.getDigest());

    if (Files.isRegularFile(targFile)) {
        writeStatus(r, FETCH_STATUS.ALREADY_IN_REPOSITORY, writer);
        logger.info("already retrieved:" + targFile.toAbsolutePath());
        return;//from  ww  w  .  j  a v  a  2  s .  co m
    }

    String url = AWS_BASE + r.getFilename();
    URI uri = null;
    try {
        uri = new URI(url);
    } catch (URISyntaxException e) {
        logger.warn("Bad url: " + url);
        writeStatus(r, FETCH_STATUS.BAD_URL, writer);
        return;
    }
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpHost target = new HttpHost(uri.getHost());
    String urlPath = uri.getRawPath();
    if (uri.getRawQuery() != null) {
        urlPath += "?" + uri.getRawQuery();
    }
    HttpGet httpGet = null;
    try {
        httpGet = new HttpGet(urlPath);
    } catch (Exception e) {
        logger.warn("bad path " + uri.toString(), e);
        writeStatus(r, FETCH_STATUS.BAD_URL, writer);
        return;
    }
    if (proxyHost != null && proxyPort > -1) {
        HttpHost proxy = new HttpHost(proxyHost, proxyPort, "http");
        RequestConfig requestConfig = RequestConfig.custom().setProxy(proxy).build();
        httpGet.setConfig(requestConfig);
    }
    httpGet.addHeader("Range", r.getOffsetHeader());
    HttpCoreContext coreContext = new HttpCoreContext();
    CloseableHttpResponse httpResponse = null;
    URI lastURI = null;
    try {
        httpResponse = httpClient.execute(target, httpGet, coreContext);
        RedirectLocations redirectLocations = (RedirectLocations) coreContext
                .getAttribute(DefaultRedirectStrategy.REDIRECT_LOCATIONS);
        if (redirectLocations != null) {
            for (URI redirectURI : redirectLocations.getAll()) {
                lastURI = redirectURI;
            }
        } else {
            lastURI = httpGet.getURI();
        }
    } catch (IOException e) {
        logger.warn("IOException for " + uri.toString(), e);
        writeStatus(r, FETCH_STATUS.FETCHED_IO_EXCEPTION, writer);
        return;
    }
    lastURI = uri.resolve(lastURI);

    if (httpResponse.getStatusLine().getStatusCode() != 200
            && httpResponse.getStatusLine().getStatusCode() != 206) {
        logger.warn("Bad status for " + uri.toString() + " : " + httpResponse.getStatusLine().getStatusCode());
        writeStatus(r, FETCH_STATUS.FETCHED_NOT_200, writer);
        return;
    }
    Path tmp = null;
    Header[] headers = null;
    boolean isTruncated = false;
    try {
        //this among other parts is plagiarized from centic9's CommonCrawlDocumentDownload
        //probably saved me hours.  Thank you, Dominik!
        tmp = Files.createTempFile("cc-getter", "");
        try (InputStream is = new GZIPInputStream(httpResponse.getEntity().getContent())) {
            WARCRecord warcRecord = new WARCRecord(new FastBufferedInputStream(is), "", 0);
            ArchiveRecordHeader archiveRecordHeader = warcRecord.getHeader();
            if (archiveRecordHeader.getHeaderFields().containsKey(WARCConstants.HEADER_KEY_TRUNCATED)) {
                isTruncated = true;
            }
            headers = LaxHttpParser.parseHeaders(warcRecord, "UTF-8");

            Files.copy(warcRecord, tmp, StandardCopyOption.REPLACE_EXISTING);
        }
    } catch (IOException e) {
        writeStatus(r, null, headers, 0L, isTruncated, FETCH_STATUS.FETCHED_IO_EXCEPTION_READING_ENTITY,
                writer);
        deleteTmp(tmp);
        return;
    }

    String digest = null;
    long tmpLength = 0l;
    try (InputStream is = Files.newInputStream(tmp)) {
        digest = base32.encodeAsString(DigestUtils.sha1(is));
        tmpLength = Files.size(tmp);
    } catch (IOException e) {
        writeStatus(r, null, headers, tmpLength, isTruncated, FETCH_STATUS.FETCHED_IO_EXCEPTION_SHA1, writer);
        logger.warn("IOException during digesting: " + tmp.toAbsolutePath());
        deleteTmp(tmp);
        return;
    }

    if (Files.exists(targFile)) {
        writeStatus(r, digest, headers, tmpLength, isTruncated, FETCH_STATUS.ALREADY_IN_REPOSITORY, writer);
        deleteTmp(tmp);
        return;
    }
    try {
        Files.createDirectories(targFile.getParent());
        Files.copy(tmp, targFile);
    } catch (IOException e) {
        writeStatus(r, digest, headers, tmpLength, isTruncated,
                FETCH_STATUS.FETCHED_EXCEPTION_COPYING_TO_REPOSITORY, writer);
        deleteTmp(tmp);

    }
    writeStatus(r, digest, headers, tmpLength, isTruncated, FETCH_STATUS.ADDED_TO_REPOSITORY, writer);
    deleteTmp(tmp);
}

From source file:com.shmsoft.dmass.main.ActionStaging.java

private void calculateSize() throws IOException {
    Project project = Project.getProject();
    String[] dirs = project.getInputs();
    totalSize = 0;/*from   ww w  . j av a2s  .c  om*/
    for (String dir : dirs) {
        Path path = Paths.get(dir);
        if (Files.exists(path)) {
            if (Files.isDirectory(path)) {
                // TODO check for efficiency
                totalSize += dirSize(path);
            } else {
                totalSize += Files.size(path);
            }
        }
    }
}

From source file:org.tinymediamanager.core.ImageCache.java

/**
 * Cache image./*from  w w  w .ja  v a 2 s  .co  m*/
 * 
 * @param mf
 *          the media file
 * @return the file the cached file
 * @throws Exception
 */
public static Path cacheImage(Path originalFile) throws Exception {
    MediaFile mf = new MediaFile(originalFile);
    Path cachedFile = ImageCache.getCacheDir()
            .resolve(getMD5(originalFile.toString()) + "." + Utils.getExtension(originalFile));
    if (!Files.exists(cachedFile)) {
        // check if the original file exists && size > 0
        if (!Files.exists(originalFile)) {
            throw new FileNotFoundException("unable to cache file: " + originalFile + "; file does not exist");
        }
        if (Files.size(originalFile) == 0) {
            throw new EmptyFileException(originalFile);
        }

        // recreate cache dir if needed
        // rescale & cache
        BufferedImage originalImage = null;
        try {
            originalImage = createImage(originalFile);
        } catch (Exception e) {
            throw new Exception("cannot create image - file seems not to be valid? " + originalFile);
        }

        // calculate width based on MF type
        int desiredWidth = originalImage.getWidth(); // initialize with fallback
        switch (mf.getType()) {
        case FANART:
            if (originalImage.getWidth() > 1000) {
                desiredWidth = 1000;
            }
            break;

        case POSTER:
            if (originalImage.getHeight() > 500) {
                desiredWidth = 350;
            }
            break;

        case EXTRAFANART:
        case THUMB:
        case BANNER:
        case GRAPHIC:
            desiredWidth = 300;
            break;

        default:
            break;
        }

        // special handling for movieset-fanart or movieset-poster
        if (mf.getFilename().startsWith("movieset-fanart") || mf.getFilename().startsWith("movieset-poster")) {
            if (originalImage.getWidth() > 1000) {
                desiredWidth = 1000;
            }
        }

        Point size = calculateSize(desiredWidth, (int) (originalImage.getHeight() / 1.5),
                originalImage.getWidth(), originalImage.getHeight(), true);
        BufferedImage scaledImage = null;

        if (Globals.settings.getImageCacheType() == CacheType.FAST) {
            // scale fast
            scaledImage = Scalr.resize(originalImage, Scalr.Method.BALANCED, Scalr.Mode.FIT_EXACT, size.x,
                    size.y);
        } else {
            // scale with good quality
            scaledImage = Scalr.resize(originalImage, Scalr.Method.QUALITY, Scalr.Mode.FIT_EXACT, size.x,
                    size.y);
        }
        originalImage = null;

        ImageWriter imgWrtr = null;
        ImageWriteParam imgWrtrPrm = null;

        // here we have two different ways to create our thumb
        // a) a scaled down jpg/png (without transparency) which we have to modify since OpenJDK cannot call native jpg encoders
        // b) a scaled down png (with transparency) which we can store without any more modifying as png
        if (hasTransparentPixels(scaledImage)) {
            // transparent image -> png
            imgWrtr = ImageIO.getImageWritersByFormatName("png").next();
            imgWrtrPrm = imgWrtr.getDefaultWriteParam();

        } else {
            // non transparent image -> jpg
            // convert to rgb
            BufferedImage rgb = new BufferedImage(scaledImage.getWidth(), scaledImage.getHeight(),
                    BufferedImage.TYPE_INT_RGB);
            ColorConvertOp xformOp = new ColorConvertOp(null);
            xformOp.filter(scaledImage, rgb);
            imgWrtr = ImageIO.getImageWritersByFormatName("jpg").next();
            imgWrtrPrm = imgWrtr.getDefaultWriteParam();
            imgWrtrPrm.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT);
            imgWrtrPrm.setCompressionQuality(0.80f);

            scaledImage = rgb;
        }

        FileImageOutputStream output = new FileImageOutputStream(cachedFile.toFile());
        imgWrtr.setOutput(output);
        IIOImage image = new IIOImage(scaledImage, null, null);
        imgWrtr.write(null, image, imgWrtrPrm);
        imgWrtr.dispose();
        output.flush();
        output.close();
        scaledImage = null;
    }

    if (!Files.exists(cachedFile)) {
        throw new Exception("unable to cache file: " + originalFile);
    }

    return cachedFile;
}

From source file:org.darkware.wpman.config.ReloadableWordpressConfig.java

/**
 * Load a theme profile fragment and apply it as an override.
 *
 * @param themes The {@link ThemeListConfig} to load the profile fragment into
 * @param themeFile The file containing the theme fragment.
 *///from www  .j a v a2 s .c om
protected void loadTheme(final ThemeListConfig themes, final Path themeFile) {
    try {
        ThemeConfig theme;
        if (Files.size(themeFile) < 3)
            theme = new ThemeConfig();
        else
            theme = this.mapper.readValue(themeFile.toFile(), ThemeConfig.class);

        // Set the source file
        theme.setPolicyFile(themeFile);

        String slug = ReloadableWordpressConfig.slugForFile(themeFile);

        themes.overrideItem(slug, theme);
        WPManager.log.debug("Loaded configuration for theme: {}", slug);
    } catch (JsonMappingException e) {
        WPManager.log.warn("Skipped loading theme configuration (formatting): {}", themeFile);
    } catch (IllegalSlugException e) {
        WPManager.log.warn("Skipped loading theme configuration (illegal slug): {}", themeFile, e);
    } catch (IOException e) {
        WPManager.log.error("Error loading theme configuration: {}", themeFile, e);
    }
}

From source file:com.streamsets.datacollector.bundles.content.SdcInfoContentGenerator.java

private void printFile(Path path, Path prefix, String type, BundleWriter writer) throws IOException {
    writer.write(type);/*from  w  ww  .j ava 2 s  .  c o  m*/
    writer.write(";");
    writer.write(getOrWriteError(() -> prefix.relativize(path).toString()));
    writer.write(";");
    writer.write(getOrWriteError(() -> Files.getOwner(path).getName()));
    writer.write(";");
    if ("F".equals(type)) {
        writer.write(getOrWriteError(() -> String.valueOf(Files.size(path))));
    }
    writer.write(";");
    writer.write(getOrWriteError(() -> StringUtils.join(Files.getPosixFilePermissions(path), ",")));
    writer.write("\n");
}

From source file:com.shmsoft.dmass.main.ActionStaging.java

private long dirSize(Path path) {
    long size = 0;
    try {//from  w ww  . j a va2s .  c om
        DirectoryStream ds = Files.newDirectoryStream(path);
        for (Object o : ds) {
            Path p = (Path) o;
            if (Files.isDirectory(p)) {
                size += dirSize(p);
            } else {
                size += Files.size(p);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return size;
}

From source file:org.freeeed.main.ActionStaging.java

/**
 * This is a recursive function going through all subdirectories It uses the
 * class variable totalSize to keep track through recursions
 *
 * @throws IOException/*w w  w. ja v a2 s . c  om*/
 */
private void calculateSize() throws IOException {
    Project project = Project.getCurrentProject();
    String[] dirs = project.getInputs();
    totalSize = 0;
    for (String dir : dirs) {
        Path path = Paths.get(dir);
        if (Files.exists(path)) {
            if (Files.isDirectory(path)) {
                // TODO check for efficiency
                totalSize += dirSize(path);
            } else {
                totalSize += Files.size(path);
            }
        }
    }
}

From source file:io.pravega.segmentstore.storage.impl.extendeds3.S3FileSystemImpl.java

@Synchronized
@Override/* www.j a va2  s .  com*/
public CompleteMultipartUploadResult completeMultipartUpload(CompleteMultipartUploadRequest request) {
    Map<Integer, CopyPartRequest> partMap = multipartUploads.get(request.getKey());
    if (partMap == null) {
        throw new S3Exception("NoSuchKey", HttpStatus.SC_NOT_FOUND, "NoSuchKey", "");
    }
    try {
        partMap.forEach((index, copyPart) -> {
            if (copyPart.getKey() != copyPart.getSourceKey()) {
                Path sourcePath = Paths.get(this.baseDir, copyPart.getBucketName(), copyPart.getSourceKey());
                Path targetPath = Paths.get(this.baseDir, copyPart.getBucketName(), copyPart.getKey());
                try (FileChannel sourceChannel = FileChannel.open(sourcePath, StandardOpenOption.READ);
                        FileChannel targetChannel = FileChannel.open(targetPath, StandardOpenOption.WRITE)) {
                    targetChannel.transferFrom(sourceChannel, Files.size(targetPath),
                            copyPart.getSourceRange().getLast() + 1 - copyPart.getSourceRange().getFirst());
                    targetChannel.close();
                    AclSize aclMap = this.aclMap.get(copyPart.getKey());
                    this.aclMap.put(copyPart.getKey(), aclMap.withSize(Files.size(targetPath)));
                } catch (IOException e) {
                    throw new S3Exception("NoSuchKey", 404, "NoSuchKey", "");
                }
            }
        });
    } finally {
        multipartUploads.remove(request.getKey());
    }

    return new CompleteMultipartUploadResult();
}

From source file:ec.edu.chyc.manejopersonal.util.ServerUtils.java

/**
 * Devuelve el tamao de un archivo en un formato legible (por ejemplo 23KB, 23MB, 2B, etc)
 *  en caso de no poder obtener el tamao (puede darse en caso de que el archivo no exista o se trate de 
 *  un directorio, o algn error I/O) se devuelve una cadena vaca.
 *  tenga permiso/*  ww  w.j a v  a  2s .c om*/
 * @param pathArchivoSubido Path del archivo que se desea obtener el tamao.
 * @return Tamao del archivo en formato amigable.
 */
static public String tamanoArchivo(Path pathArchivoSubido) {
    if (Files.isRegularFile(pathArchivoSubido) && Files.exists(pathArchivoSubido)) {
        try {
            Long size = Files.size(pathArchivoSubido);
            return ServerUtils.humanReadableByteCount(size);
        } catch (IOException ex) {
            return "";
            //Logger.getLogger(ServerUtils.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    return "";
}

From source file:com.github.zhanhb.ckfinder.connector.handlers.command.FileUploadCommand.java

/**
 * saves temporary file in the correct file path.
 *
 * @param path path to save file//from  w w w . ja  v  a  2s.c om
 * @param item file upload item
 * @param param the parameter
 * @param context ckfinder context
 * @throws IOException when IO Exception occurs.
 * @throws ConnectorException when error occurs
 */
private void saveTemporaryFile(Path path, MultipartFile item, FileUploadParameter param,
        CKFinderContext context) throws IOException, ConnectorException {
    Path file = getPath(path, param.getNewFileName());

    if (ImageUtils.isImageExtension(file)) {
        if (!context.isCheckSizeAfterScaling() && !ImageUtils.checkImageSize(item, context)) {
            param.throwException(ErrorCode.UPLOADED_TOO_BIG);
        }
        ImageUtils.createTmpThumb(item, file, getFileItemName(item), context);
        if (context.isCheckSizeAfterScaling()
                && !FileUtils.isFileSizeInRange(param.getType(), Files.size(file))) {
            Files.deleteIfExists(file);
            param.throwException(ErrorCode.UPLOADED_TOO_BIG);
        }
    } else {
        try (InputStream in = item.getInputStream()) {
            Files.copy(in, file);
        }
    }
    FileUploadEvent args = new FileUploadEvent(param.getCurrentFolder(), file);
    context.fireOnFileUpload(args);
}