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:com.github.zhanhb.ckfinder.connector.utils.ImageUtils.java

/**
 * Uploads image and if the image size is larger than maximum allowed it
 * resizes the image.//from  w  w  w.ja v  a2  s. com
 *
 * @param part servlet part
 * @param file file name
 * @param fileName name of file
 * @param context ckfinder context
 * @throws IOException when IO Exception occurs.
 */
public static void createTmpThumb(InputStreamSource part, Path file, String fileName, CKFinderContext context)
        throws IOException {
    BufferedImage image;
    try (InputStream stream = part.getInputStream()) {
        image = ImageIO.read(stream);
        if (image == null) {
            throw new IOException("Wrong file");
        }
    }
    ImageProperties imageProperties = context.getImage();
    Dimension dimension = createThumbDimension(image, imageProperties.getMaxWidth(),
            imageProperties.getMaxHeight());
    if (dimension.width == 0 || dimension.height == 0
            || (image.getHeight() <= dimension.height && image.getWidth() <= dimension.width)) {
        try (InputStream stream = part.getInputStream()) {
            Files.copy(stream, file, StandardCopyOption.REPLACE_EXISTING);
        }
    } else {
        resizeImage(image, dimension.width, dimension.height, imageProperties.getQuality(), file);
    }
    if (log.isTraceEnabled()) {
        log.trace("thumb size: {}", Files.size(file));
    }
}

From source file:org.wso2.carbon.apimgt.hybrid.gateway.usage.publisher.util.UsageFileWriter.java

/**
 * Writes the given content to the usage file
 *
 * @param content String content to be written
 *//*from ww w  . j a v a 2 s.  c  om*/
public synchronized void writeToFile(String content) {
    //Check if the file size exceeds the max limit (12mb can contain roughly 10000 requests or 30000 events)
    try {
        String sizeInMb = ConfigManager.getConfigManager().getProperty("MaxUsageFileSize");
        int maxFileSize = Integer.parseInt((sizeInMb != null && !sizeInMb.isEmpty()) ? sizeInMb : "12") * 1024
                * 1024;
        if (Files.size(filePath) > maxFileSize) {
            if (log.isDebugEnabled()) {
                log.debug("Rotating API Usage File. File Size is > MaxFileSize (" + maxFileSize + " Mb)");
            }
            rotateFile(filePath.toString());
        }
    } catch (IOException | OnPremiseGatewayException | UsagePublisherException e) {
        log.error("Error occurred while rotating the file : " + filePath.toString(), e);
    }

    try {
        bufferedWriter.write(content);
        bufferedWriter.newLine();
        bufferedWriter.flush();
    } catch (IOException e) {
        log.warn("Error occurred while writing event [" + content + "] to the file : " + filePath.toString(),
                e);
    }
}

From source file:org.nuxeo.ecm.core.blob.FilesystemBlobProvider.java

/**
 * Creates a filesystem blob with the given information.
 * <p>/*w w  w .j  a v  a2 s .c  om*/
 * The passed {@link BlobInfo} contains information about the blob, and the key is a file path.
 *
 * @param blobInfo the blob info where the key is a file path
 * @return the blob
 */
public ManagedBlob createBlob(BlobInfo blobInfo) throws IOException {
    String filePath = blobInfo.key;
    if (filePath.contains("..")) {
        throw new FileNotFoundException("Illegal path: " + filePath);
    }
    if (!filePath.startsWith(root)) {
        throw new FileNotFoundException("Path is not under configured root: " + filePath);
    }
    Path path = Paths.get(filePath);
    if (!Files.exists(path)) {
        throw new FileNotFoundException(filePath);
    }
    // dereference links
    while (Files.isSymbolicLink(path)) {
        // dereference if link
        path = Files.readSymbolicLink(path);
        if (!Files.exists(path)) {
            throw new FileNotFoundException(filePath);
        }
    }
    String relativePath = filePath.substring(root.length());
    long length = Files.size(path);
    blobInfo = new BlobInfo(blobInfo); // copy
    blobInfo.key = blobProviderId + ":" + relativePath;
    blobInfo.length = Long.valueOf(length);
    if (blobInfo.filename == null) {
        blobInfo.filename = Paths.get(filePath).getFileName().toString();
    }
    if (blobInfo.digest == null) {
        try (InputStream in = Files.newInputStream(path)) {
            blobInfo.digest = DigestUtils.md5Hex(in);
        }
    }
    return new SimpleManagedBlob(blobInfo);
}

From source file:ddf.catalog.impl.operations.OperationsMetacardSupport.java

void generateMetacardAndContentItems(List<ContentItem> incomingContentItems, Map<String, Metacard> metacardMap,
        List<ContentItem> contentItems, Map<String, Map<String, Path>> tmpContentPaths) throws IngestException {
    for (ContentItem contentItem : incomingContentItems) {
        try {/*from   w  w  w  .j a  va2s .  c o m*/
            Path tmpPath = null;
            String fileName;
            long size;
            try (InputStream inputStream = contentItem.getInputStream()) {
                fileName = contentItem.getFilename();
                if (inputStream == null) {
                    throw new IngestException("Could not copy bytes of content message.  Message was NULL.");
                }

                if (!InputValidation.isFileNameClientSideSafe(fileName)) {
                    throw new IngestException("Ignored filename found.");
                }

                String sanitizedFilename = InputValidation.sanitizeFilename(fileName);
                tmpPath = Files.createTempFile(FilenameUtils.getBaseName(sanitizedFilename),
                        FilenameUtils.getExtension(sanitizedFilename));
                Files.copy(inputStream, tmpPath, StandardCopyOption.REPLACE_EXISTING);
                size = Files.size(tmpPath);

                final String key = contentItem.getId();
                Map<String, Path> pathAndQualifiers = tmpContentPaths.get(key);

                if (pathAndQualifiers == null) {
                    pathAndQualifiers = new HashMap<>();
                    pathAndQualifiers.put(contentItem.getQualifier(), tmpPath);
                    tmpContentPaths.put(key, pathAndQualifiers);
                } else {
                    pathAndQualifiers.put(contentItem.getQualifier(), tmpPath);
                }

            } catch (IOException e) {
                if (tmpPath != null) {
                    FileUtils.deleteQuietly(tmpPath.toFile());
                }
                throw new IngestException("Could not copy bytes of content message.", e);
            }
            String mimeTypeRaw = contentItem.getMimeTypeRawData();
            mimeTypeRaw = guessMimeType(mimeTypeRaw, fileName, tmpPath);

            if (!InputValidation.isMimeTypeClientSideSafe(mimeTypeRaw)) {
                throw new IngestException("Unsupported mime type.");
            }

            // If any sanitization was done, rename file name to sanitized file name.
            if (!InputValidation.sanitizeFilename(fileName).equals(fileName)) {
                fileName = InputValidation.sanitizeFilename(fileName);
            } else {
                fileName = updateFileExtension(mimeTypeRaw, fileName);
            }

            Metacard metacard;
            boolean qualifiedContent = StringUtils.isNotEmpty(contentItem.getQualifier());
            if (qualifiedContent) {
                metacard = contentItem.getMetacard();
            } else {
                metacard = metacardFactory.generateMetacard(mimeTypeRaw, contentItem.getId(), fileName,
                        tmpPath);
            }
            metacardMap.put(metacard.getId(), metacard);

            ContentItem generatedContentItem = new ContentItemImpl(metacard.getId(),
                    qualifiedContent ? contentItem.getQualifier() : "",
                    com.google.common.io.Files.asByteSource(tmpPath.toFile()), mimeTypeRaw, fileName, size,
                    metacard);
            contentItems.add(generatedContentItem);
        } catch (Exception e) {
            tmpContentPaths.values().stream().flatMap(id -> id.values().stream())
                    .forEach(path -> FileUtils.deleteQuietly(path.toFile()));
            tmpContentPaths.clear();
            throw new IngestException("Could not create metacard.", e);
        }
    }
}

From source file:org.discosync.ApplySyncPack.java

/**
 * Apply a syncpack to a target directory.
 *///from www . jav  a 2s .  c o m
protected void applySyncPack(String syncPackDir, String targetDir) throws SQLException, IOException {

    // read file operations from database
    File fileOpDbFile = new File(syncPackDir, "fileoperations");
    FileOperationDatabase db = new FileOperationDatabase(fileOpDbFile.getAbsolutePath());
    db.open();

    Iterator<FileListEntry> it = db.getFileListOperationIterator();

    Path syncFileBaseDir = Paths.get(syncPackDir, "files");
    String syncFileBaseDirStr = syncFileBaseDir.toAbsolutePath().toString();

    int filesCopied = 0;
    int filesReplaced = 0;
    int filesDeleted = 0;
    long copySize = 0L;
    long deleteSize = 0L;

    // Collect directories during processing.
    List<FileListEntry> directoryOperations = new ArrayList<>();

    // First process all files, then the directories
    while (it.hasNext()) {

        FileListEntry e = it.next();

        // Remember directories
        if (e.isDirectory()) {
            directoryOperations.add(e);
            continue;
        }

        String path = e.getPath();
        Path sourcePath = Paths.get(syncFileBaseDirStr, path); // may not exist
        Path targetPath = Paths.get(targetDir, path); // may not exist

        if (e.getOperation() == FileOperations.COPY) {
            // copy new file, target files should not exist
            if (Files.exists(targetPath)) {
                System.out
                        .println("Error: the file should not exist: " + targetPath.toAbsolutePath().toString());
            } else {
                if (!Files.exists(targetPath.getParent())) {
                    Files.createDirectories(targetPath.getParent());
                }

                Files.copy(sourcePath, targetPath);
                filesCopied++;
                copySize += e.getSize();
            }

        } else if (e.getOperation() == FileOperations.REPLACE) {
            // replace existing file
            if (!Files.exists(targetPath)) {
                System.out.println("Info: the file should exist: " + targetPath.toAbsolutePath().toString());
            }
            Files.copy(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING);
            filesReplaced++;
            copySize += e.getSize();

        } else if (e.getOperation() == FileOperations.DELETE) {
            // delete existing file
            if (!Files.exists(targetPath)) {
                System.out.println("Info: the file should exist: " + targetPath.toAbsolutePath().toString());
            } else {
                long fileSize = Files.size(targetPath);
                if (fileSize != e.getSize()) {
                    // show info, delete anyway
                    System.out.println(
                            "Info: the file size is different: " + targetPath.toAbsolutePath().toString());
                }
                deleteSize += fileSize;
                Files.delete(targetPath);
                filesDeleted++;
            }
        }
    }

    db.close();

    // Sort directory list to ensure directories are deleted bottom-up (first /dir1/dir2, then /dir1)
    Collections.sort(directoryOperations, new Comparator<FileListEntry>() {
        @Override
        public int compare(FileListEntry e1, FileListEntry e2) {
            return e2.getPath().compareTo(e1.getPath().toString());
        }
    });

    // Now process directories - create and delete empty directories
    for (FileListEntry e : directoryOperations) {

        String path = e.getPath();
        Path targetPath = Paths.get(targetDir, path); // may not exist

        if (e.getOperation() == FileOperations.COPY) {
            // create directory if needed
            if (!Files.exists(targetPath)) {
                Files.createDirectories(targetPath);
            }
        } else if (e.getOperation() == FileOperations.DELETE) {

            if (!Files.exists(targetPath)) {
                System.out.println(
                        "Info: Directory to DELETE does not exist: " + targetPath.toAbsolutePath().toString());

            } else if (!Files.isDirectory(targetPath, LinkOption.NOFOLLOW_LINKS)) {
                System.out.println("Info: Directory to DELETE is not a directory, but a file: "
                        + targetPath.toAbsolutePath().toString());

            } else if (!Utils.isDirectoryEmpty(targetPath)) {
                System.out.println("Info: Directory to DELETE is not empty, but should be empty: "
                        + targetPath.toAbsolutePath().toString());

            } else {
                // delete directory
                Files.delete(targetPath);
            }
        }
    }

    System.out.println("Apply of syncpack '" + syncPackDir + "' to directory '" + targetDir + "' finished.");
    System.out.println("Files copied  : " + String.format("%,d", filesCopied));
    System.out.println("Files replaced: " + String.format("%,d", filesReplaced));
    System.out.println("Files deleted : " + String.format("%,d", filesDeleted));
    System.out.println("Bytes copied  : " + String.format("%,d", copySize));
    System.out.println("Bytes deleted : " + String.format("%,d", deleteSize));
}

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

@Override
public void run() {
    long timestamp = System.currentTimeMillis(); // multi episode same file
    try {/*from  w w w .  j a  va  2 s  .  com*/
        if (StringUtils.isBlank(filename)) {
            return;
        }

        // don't write jpeg -> write jpg
        if (FilenameUtils.getExtension(filename).equalsIgnoreCase("JPEG")) {
            filename = FilenameUtils.getBaseName(filename) + ".jpg";
        }

        String oldFilename = null;
        try {
            // store old filename at the first image
            if (firstImage) {
                switch (type) {
                case POSTER:
                case BACKGROUND:
                case BANNER:
                case THUMB:
                case CLEARART:
                case DISC:
                case LOGO:
                case CLEARLOGO:
                    oldFilename = entity.getArtworkFilename(MediaFileType.getMediaFileType(type));
                    entity.removeAllMediaFiles(MediaFileType.getMediaFileType(type));
                    break;

                default:
                    return;
                }
            }

            // debug message
            LOGGER.debug("writing " + type + " " + filename);
            Path destFile = entity.getPathNIO().resolve(filename);
            Path tempFile = entity.getPathNIO().resolve(filename + "." + timestamp + ".part"); // multi episode same file

            // check if old and new file are the same (possible if you select it in the imagechooser)
            boolean sameFile = false;
            if (url.startsWith("file:")) {
                String newUrl = url.replace("file:/", "");
                Path file = Paths.get(newUrl);
                if (file.equals(destFile)) {
                    sameFile = true;
                }
            }

            // fetch and store images
            if (!sameFile) {
                Url url1 = new Url(url);
                FileOutputStream outputStream = new FileOutputStream(tempFile.toFile());
                InputStream is = url1.getInputStream();
                IOUtils.copy(is, outputStream);
                outputStream.flush();
                try {
                    outputStream.getFD().sync(); // wait until file has been completely written
                    // give it a few milliseconds
                    Thread.sleep(150);
                } catch (Exception e) {
                    // empty here -> just not let the thread crash
                }
                outputStream.close();
                is.close();

                // check if the file has been downloaded
                if (!Files.exists(tempFile) || Files.size(tempFile) == 0) {
                    throw new Exception("0byte file downloaded: " + filename);
                }

                // delete the old one if exisiting
                if (StringUtils.isNotBlank(oldFilename)) {
                    Path oldFile = entity.getPathNIO().resolve(oldFilename);
                    Utils.deleteFileSafely(oldFile);
                }

                // delete new destination if existing
                Utils.deleteFileSafely(destFile);

                // move the temp file to the expected filename
                if (!Utils.moveFileSafe(tempFile, destFile)) {
                    throw new Exception("renaming temp file failed: " + filename);
                }
            }

            // has tmm been shut down?
            if (Thread.interrupted()) {
                return;
            }

            // set the new image if its the first image
            if (firstImage) {
                LOGGER.debug("set " + type + " " + FilenameUtils.getName(filename));
                ImageCache.invalidateCachedImage(entity.getPathNIO().resolve(filename));
                switch (type) {
                case POSTER:
                case BACKGROUND:
                case BANNER:
                case THUMB:
                case CLEARART:
                case DISC:
                case LOGO:
                case CLEARLOGO:
                    entity.setArtwork(destFile, MediaFileType.getMediaFileType(type));
                    entity.callbackForWrittenArtwork(type);
                    entity.saveToDb();
                    break;

                default:
                    return;
                }
            }
        }

        catch (Exception e) {
            if (e instanceof InterruptedException) {
                // only warning
                LOGGER.warn("interrupted image download");
            } else {
                LOGGER.error("fetch image", e);
            }

            // remove temp file
            Path tempFile = entity.getPathNIO().resolve(filename + "." + timestamp + ".part"); // multi episode same file
            if (Files.exists(tempFile)) {
                Utils.deleteFileSafely(tempFile);
            }

            // fallback
            if (firstImage && StringUtils.isNotBlank(oldFilename)) {
                switch (type) {
                case POSTER:
                case BACKGROUND:
                case BANNER:
                case THUMB:
                case CLEARART:
                case DISC:
                case LOGO:
                case CLEARLOGO:
                    entity.setArtwork(Paths.get(oldFilename), MediaFileType.getMediaFileType(type));
                    entity.callbackForWrittenArtwork(type);
                    entity.saveToDb();
                    break;

                default:
                    return;
                }
            }

            MessageManager.instance.pushMessage(new Message(MessageLevel.ERROR, "ArtworkDownload",
                    "message.artwork.threadcrashed", new String[] { ":", e.getLocalizedMessage() }));
        }

    } catch (Exception e) {
        LOGGER.error("crashed thread: ", e);
    }
}

From source file:com.cloudera.oryx.common.OryxTest.java

public static void assertNonEmpty(Path p) throws IOException {
    assertTrue("File should exist: " + p, Files.exists(p));
    assertTrue("File should not be empty: " + p, Files.size(p) > 0);
}

From source file:org.opendatakit.briefcase.reused.UncheckedFiles.java

public static Optional<String> getMd5Hash(Path file) {
    try {/*from  w w w.  j a v  a  2  s  . c  o  m*/
        // CTS (6/15/2010) : stream file through digest instead of handing
        // it the
        // byte[]
        MessageDigest md = MessageDigest.getInstance("MD5");
        int chunkSize = 256;

        byte[] chunk = new byte[chunkSize];

        // Get the size of the file
        long lLength = Files.size(file);

        if (lLength > Integer.MAX_VALUE) {
            log.error("File is too large");
            return Optional.empty();
        }

        int length = (int) lLength;

        InputStream is = Files.newInputStream(file);

        int l = 0;
        for (l = 0; l + chunkSize < length; l += chunkSize) {
            is.read(chunk, 0, chunkSize);
            md.update(chunk, 0, chunkSize);
        }

        int remaining = length - l;
        if (remaining > 0) {
            is.read(chunk, 0, remaining);
            md.update(chunk, 0, remaining);
        }
        byte[] messageDigest = md.digest();

        BigInteger number = new BigInteger(1, messageDigest);
        String md5 = number.toString(16);
        while (md5.length() < 32)
            md5 = "0" + md5;
        is.close();
        return Optional.of(md5);

    } catch (NoSuchAlgorithmException e) {
        log.error("MD5 calculation failed", e);
        return Optional.empty();
    } catch (FileNotFoundException e) {
        log.error("No File", e);
        return Optional.empty();
    } catch (IOException e) {
        log.error("Problem reading from file", e);
        return Optional.empty();
    }

}

From source file:org.niord.proxy.web.ETagServletFilter.java

/** Returns the etag for the given file **/
private EntityTag etagForFile(Path f) throws IOException {
    return new EntityTag("" + Files.getLastModifiedTime(f).toMillis() + "_" + Files.size(f), true);
}

From source file:org.n52.geolabel.formats.PngTest.java

@Test
public void emptyLabelIsSmallerThanFullLabel() throws IOException {
    Label empty = new Label();
    InputStream inSmall = this.encoder.encode(empty);

    Label full = new Label();
    full.getCitationsFacet().updateAvailability(Availability.AVAILABLE);
    full.getExpertFeedbackFacet().updateAvailability(Availability.AVAILABLE);
    full.getLineageFacet().updateAvailability(Availability.AVAILABLE);
    full.getProducerCommentsFacet().updateAvailability(Availability.AVAILABLE);
    full.getProducerProfileFacet().updateAvailability(Availability.AVAILABLE);
    full.getQualityInformationFacet().updateAvailability(Availability.AVAILABLE);
    full.getStandardsComplianceFacet().updateAvailability(Availability.AVAILABLE);
    full.getUserFeedbackFacet().updateAvailability(Availability.AVAILABLE);
    InputStream inLarge = this.encoder.encode(full);

    File tempSmall = File.createTempFile("geolabel_", ".png");
    try (FileOutputStream tempFileSmall = new FileOutputStream(tempSmall);) {
        IOUtils.copy(inSmall, tempFileSmall);
    }/*from  w  ww  .  ja v a  2s . c  om*/

    File tempLarge = File.createTempFile("geolabel_", ".png");
    try (FileOutputStream tempFileLarge = new FileOutputStream(tempLarge);) {
        IOUtils.copy(inLarge, tempFileLarge);
    }

    // System.out.printf("Saved small and larg geolabel as %s and %s",
    // tempSmall.getAbsolutePath(),
    // tempLarge.getAbsolutePath());

    Long small = Long.valueOf(Files.size(tempSmall.toPath()));
    Long large = Long.valueOf(Files.size(tempLarge.toPath()));
    assertThat("file size of full label is larger than empty label", large, is(greaterThan(small)));

    tempSmall.deleteOnExit();
    tempLarge.deleteOnExit();
}