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:algorithm.MetsSubmissionInformationPackage.java

@SuppressWarnings("unchecked")
@Override//ww  w .  j  a v  a2  s .  c om
public File encapsulate(File carrier, List<File> userPayloadList) throws IOException {
    List<File> payloadList = new ArrayList<File>();
    payloadList.addAll(userPayloadList);
    Mets metsDocument = new Mets();
    metsDocument.setID(carrier.getName() + "_SIP");
    metsDocument.setLABEL("PeriCAT information package");
    metsDocument.setTYPE("digital object and metadata files");
    // HEADER:
    Agent periPack = new Agent();
    periPack.setID("peripack agent");
    periPack.setROLE(Role.PRESERVATION);
    periPack.setOTHERTYPE("SOFTWARE");
    Name name = new Name();
    PCData nameString = new PCData();
    nameString.add("peripack");
    name.getContent().add(nameString);
    periPack.getContent().add(name);
    Note note = new Note();
    PCData noteString = new PCData();
    noteString.add(TOOL_DESCRIPTION);
    note.getContent().add(noteString);
    periPack.getContent().add(note);
    MetsHdr header = new MetsHdr();
    header.setID("mets header");
    header.setCREATEDATE(new Date());
    header.setLASTMODDATE(new Date());
    header.setRECORDSTATUS("complete");
    header.getContent().add(periPack);
    if (truePersonButton.isSelected()) {
        Agent person = new Agent();
        person.setID("peripack user");
        person.setROLE(Role.CREATOR);
        person.setTYPE(Type.INDIVIDUAL);
        Name personName = new Name();
        PCData personNameString = new PCData();
        personNameString.add(personField.getText());
        personName.getContent().add(personNameString);
        person.getContent().add(personName);
        header.getContent().add(person);
    }
    metsDocument.getContent().add(header);
    // FILE SECTION:
    FileSec fileSection = new FileSec();
    FileGrp carrierGroup = new FileGrp();
    carrierGroup.setID("carrier files");
    carrierGroup.setUSE("digital object");
    // carrier div for structural map:
    Div carrierDiv = new Div();
    carrierDiv.setID("carrier list");
    carrierDiv.setTYPE("carrier files");
    // back to file section:
    edu.harvard.hul.ois.mets.File metsCarrier = new edu.harvard.hul.ois.mets.File();
    metsCarrier.setID(carrier.getAbsolutePath());
    metsCarrier.setGROUPID("carrier");
    metsCarrier.setCHECKSUM("" + FileUtils.checksumCRC32(carrier));
    metsCarrier.setMIMETYPE(Files.probeContentType(carrier.toPath()));
    metsCarrier.setOWNERID(Files.getOwner(carrier.toPath(), LinkOption.NOFOLLOW_LINKS).toString());
    metsCarrier.setSIZE(Files.size(carrier.toPath()));
    metsCarrier.setUSE(Files.getPosixFilePermissions(carrier.toPath(), LinkOption.NOFOLLOW_LINKS).toString());
    FLocat fileLocation = new FLocat();
    fileLocation.setXlinkHref(carrier.getAbsolutePath());
    fileLocation.setLOCTYPE(Loctype.OTHER);
    fileLocation.setOTHERLOCTYPE("system file path");
    fileLocation.setXlinkTitle("original file path of the carrier");
    metsCarrier.getContent().add(fileLocation);
    carrierGroup.getContent().add(metsCarrier);
    // add structural map information:
    Fptr carrierFilePointer = new Fptr();
    carrierFilePointer.setFILEID(carrier.getAbsolutePath());
    carrierDiv.getContent().add(carrierFilePointer);
    fileSection.getContent().add(carrierGroup);
    FileGrp payloadGroup = new FileGrp();
    payloadGroup.setID("payload files");
    payloadGroup.setUSE("metadata");

    // payload div for structural map:
    Div payloadDiv = new Div();
    payloadDiv.setID("payload list");
    payloadDiv.setTYPE("payload files");
    // back to file section:
    for (File payload : payloadList) {
        edu.harvard.hul.ois.mets.File metsPayload = new edu.harvard.hul.ois.mets.File();
        metsPayload.setID(payload.getAbsolutePath());
        metsPayload.setGROUPID("payload");
        metsPayload.setCHECKSUM(DigestUtils.md5Hex(new FileInputStream(payload)));
        metsPayload.setCHECKSUMTYPE(Checksumtype.MD5);
        metsPayload.setMIMETYPE(Files.probeContentType(payload.toPath()));
        metsPayload.setOWNERID(Files.getOwner(payload.toPath(), LinkOption.NOFOLLOW_LINKS).toString());
        metsPayload.setSIZE(Files.size(payload.toPath()));
        metsPayload
                .setUSE(Files.getPosixFilePermissions(payload.toPath(), LinkOption.NOFOLLOW_LINKS).toString());
        FLocat fileLocation2 = new FLocat();
        fileLocation2.setXlinkHref(payload.getAbsolutePath());
        fileLocation2.setLOCTYPE(Loctype.OTHER);
        fileLocation2.setOTHERLOCTYPE("system file path");
        fileLocation2.setXlinkTitle("original file path of the payload");
        metsPayload.getContent().add(fileLocation2);
        payloadGroup.getContent().add(metsPayload);
        // add structural map information:
        Fptr payloadFilePointer = new Fptr();
        payloadFilePointer.setFILEID(payload.getAbsolutePath());
        payloadDiv.getContent().add(payloadFilePointer);
    }
    fileSection.getContent().add(payloadGroup);
    metsDocument.getContent().add(fileSection);
    // STRUCTURAL MAP:
    StructMap structuralMap = new StructMap();
    structuralMap.setID("structural map");
    Div encapsulatedFiles = new Div();
    encapsulatedFiles.setID("peripack files");
    encapsulatedFiles.setTYPE("encapsulated files");
    structuralMap.getContent().add(encapsulatedFiles);
    encapsulatedFiles.getContent().add(carrierDiv);
    encapsulatedFiles.getContent().add(payloadDiv);
    metsDocument.getContent().add(structuralMap);
    File metsFile = new File(OUTPUT_DIRECTORY + "mets.xml");
    FileOutputStream outputStream = new FileOutputStream(metsFile);
    try {
        metsDocument.write(new MetsWriter(outputStream));
    } catch (MetsException e) {
    }
    outputStream.close();
    payloadList.add(metsFile);
    File outputFile = null;
    if (zipButton.isSelected()) {
        outputFile = new ZipPackaging().encapsulate(carrier, payloadList);

    } else if (tarButton.isSelected()) {
        outputFile = new TarPackaging().encapsulate(carrier, payloadList);
    }
    metsFile.delete();
    return outputFile;
}

From source file:dk.dma.dmiweather.service.FTPLoader.java

/**
 * Copied the files from DMIs ftp server to the local machine
 *
 * @return a Map with a local file and the time the file was created on the FTP server
 *//*  w  ww  .ja  v  a 2s.com*/
private Map<File, Instant> transferFilesIfNeeded(FTPClient client, String directoryName, List<FTPFile> files)
        throws IOException {

    File current = new File(tempDirLocation, directoryName);
    if (newestDirectories.isEmpty()) {
        // If we just started check if there is data from an earlier run and delete it
        File temp = new File(tempDirLocation);
        if (temp.exists()) {
            File[] oldFolders = temp.listFiles(new PatternFilenameFilter(FOLDER_PATTERN));
            if (oldFolders != null) {
                List<File> foldersToDelete = Lists.newArrayList(oldFolders);
                foldersToDelete.remove(current);
                for (File oldFolder : foldersToDelete) {
                    log.info("deleting old GRIB folder {}", oldFolder);
                    deleteRecursively(oldFolder);
                }
            }
        }
    }

    if (!current.exists()) {
        if (!current.mkdirs()) {
            throw new IOException("Unable to create temp directory " + current.getAbsolutePath());
        }
    }

    Stopwatch stopwatch = Stopwatch.createStarted();
    Map<File, Instant> transferred = new HashMap<>();
    for (FTPFile file : files) {
        File tmp = new File(current, file.getName());
        if (tmp.exists()) {
            long localSize = Files.size(tmp.toPath());
            if (localSize != file.getSize()) {
                log.info("deleting {} local file has size {}, remote is {}", tmp.getName(), localSize,
                        file.getSize());
                if (!tmp.delete()) {
                    log.warn("Unable to delete " + tmp.getAbsolutePath());
                }
            } else {
                // If the file has the right size we assume it was copied correctly (otherwise we needed to hash them)
                log.info("Reusing already downloaded version of {}", tmp.getName());
                transferred.put(tmp, file.getTimestamp().toInstant());
                continue;
            }
        }
        if (tmp.createNewFile()) {
            log.info("downloading {}", tmp.getName());

            // this often fails with java.net.ConnectException: Operation timed out
            int count = 0;
            while (count++ < MAX_TRIES) {
                try (FileOutputStream fout = new FileOutputStream(tmp)) {
                    client.retrieveFile(file.getName(), fout);
                    fout.flush();
                    break;
                } catch (IOException e) {
                    log.warn(String.format("Failed to transfer file %s, try number %s", file.getName(), count),
                            e);
                }
            }
        } else {
            throw new IOException("Unable to create temp file on disk.");
        }

        transferred.put(tmp, file.getTimestamp().toInstant());
    }
    log.info("transferred weather files in {} ms", stopwatch.stop().elapsed(TimeUnit.MILLISECONDS));
    return transferred;
}

From source file:com.google.devtools.build.lib.bazel.dash.DashModule.java

private Log getLog(String logPath) {
    Log.Builder builder = Log.newBuilder().setPath(logPath);
    File log = new File(logPath);
    try {//from  ww w  .j a  v  a2s  .c o m
        long fileSize = Files.size(log.toPath());
        if (fileSize > ONE_MB) {
            fileSize = ONE_MB;
            builder.setTruncated(true);
        }
        byte buffer[] = new byte[(int) fileSize];
        try (FileInputStream in = new FileInputStream(log)) {
            ByteStreams.readFully(in, buffer);
        }
        builder.setContents(ByteString.copyFrom(buffer));
    } catch (IOException e) {
        env.getReporter().getOutErr().printOutLn("Error reading log file " + logPath + ": " + e.getMessage());
        // TODO(kchodorow): add this info to the proto and send.
    }
    return builder.build();
}

From source file:com.liferay.sync.engine.file.system.SyncWatchEventProcessor.java

protected void addFile(SyncWatchEvent syncWatchEvent) throws Exception {
    final Path targetFilePath = Paths.get(syncWatchEvent.getFilePathName());

    if (FileUtil.notExists(targetFilePath) || sanitizeFileName(targetFilePath)
            || isInErrorState(targetFilePath)) {

        return;//from w  w w  .  j a  v a 2 s  .  co  m
    }

    Path parentTargetFilePath = targetFilePath.getParent();

    final SyncFile parentSyncFile = SyncFileService.fetchSyncFile(parentTargetFilePath.toString());

    if ((parentSyncFile == null) || (!parentSyncFile.isSystem() && (parentSyncFile.getTypePK() == 0))) {

        queueSyncWatchEvent(parentTargetFilePath.toString(), syncWatchEvent);

        return;
    }

    SyncFile syncFile = SyncFileService.fetchSyncFile(targetFilePath.toString());

    if (syncFile == null) {
        syncFile = SyncFileService.fetchSyncFile(FileKeyUtil.getFileKey(targetFilePath));

        if (!verifySite(syncFile, parentSyncFile)) {
            syncFile = null;
        }
    }

    if (syncFile == null) {
        Runnable runnable = new Runnable() {

            @Override
            public void run() {
                try {
                    SyncSite syncSite = SyncSiteService.fetchSyncSite(parentSyncFile.getRepositoryId(),
                            _syncAccountId);

                    if ((syncSite == null) || !syncSite.isActive() || !FileUtil.checkFilePath(targetFilePath)) {

                        return;
                    }

                    SyncFileService.addFileSyncFile(targetFilePath, parentSyncFile.getTypePK(),
                            parentSyncFile.getRepositoryId(), _syncAccountId);
                } catch (Exception e) {
                    if (SyncFileService.fetchSyncFile(targetFilePath.toString()) == null) {

                        _logger.error(e.getMessage(), e);
                    }
                }
            }

        };

        _executorService.execute(runnable);

        return;
    }

    Path sourceFilePath = Paths.get(syncFile.getFilePathName());

    if (targetFilePath.equals(sourceFilePath)) {
        if (isPendingTypePK(syncFile) || (syncFile.getState() == SyncFile.STATE_IN_PROGRESS)) {

            queueSyncWatchEvent(syncFile.getFilePathName(), syncWatchEvent);

            return;
        }

        if (FileUtil.isModified(syncFile)) {
            SyncFileService.updateFileSyncFile(targetFilePath, _syncAccountId, syncFile);
        }
    } else if (FileUtil.exists(sourceFilePath)) {
        try {
            if ((Files.size(targetFilePath) == 0) || FileUtil.isModified(syncFile, targetFilePath)
                    || isInErrorState(sourceFilePath)) {

                SyncFileService.addFileSyncFile(targetFilePath, parentSyncFile.getTypePK(),
                        parentSyncFile.getRepositoryId(), _syncAccountId);
            } else {
                SyncFileService.copySyncFile(syncFile, targetFilePath, parentSyncFile.getTypePK(),
                        parentSyncFile.getRepositoryId(), _syncAccountId);
            }
        } catch (Exception e) {
            if (SyncFileService.fetchSyncFile(targetFilePath.toString()) == null) {

                _logger.error(e.getMessage(), e);
            }
        }

        return;
    } else if (parentTargetFilePath.equals(sourceFilePath.getParent())) {
        if (isPendingTypePK(syncFile) || (syncFile.getState() == SyncFile.STATE_IN_PROGRESS)) {

            queueSyncWatchEvent(syncFile.getFilePathName(), syncWatchEvent);

            return;
        }

        SyncFileService.updateFileSyncFile(targetFilePath, _syncAccountId, syncFile);
    } else {
        if (isPendingTypePK(syncFile) || (syncFile.getState() == SyncFile.STATE_IN_PROGRESS)) {

            queueSyncWatchEvent(syncFile.getFilePathName(), syncWatchEvent);

            return;
        }

        SyncFileService.moveFileSyncFile(targetFilePath, parentSyncFile.getTypePK(), _syncAccountId, syncFile);

        Path sourceFileNameFilePath = sourceFilePath.getFileName();

        if (!sourceFileNameFilePath.equals(targetFilePath.getFileName())) {
            SyncFileService.updateFileSyncFile(targetFilePath, _syncAccountId, syncFile);
        }
    }

    SyncAccount syncAccount = SyncAccountService.fetchSyncAccount(_syncAccountId);

    if (syncAccount.getState() == SyncAccount.STATE_CONNECTED) {
        SyncWatchEvent relatedSyncWatchEvent = SyncWatchEventService.fetchSyncWatchEvent(
                SyncWatchEvent.EVENT_TYPE_DELETE, syncWatchEvent.getFilePathName(),
                syncWatchEvent.getTimestamp());

        if (relatedSyncWatchEvent != null) {
            _processedSyncWatchEventIds.add(relatedSyncWatchEvent.getSyncWatchEventId());
        }
    }
}

From source file:org.fim.internal.hash.FileHasherTest.java

@Test
@Ignore/*from w  ww  .  j  a  v a  2 s . c  o  m*/
public void checkHashIsCompleteInEveryCases() throws IOException {
    if (hashMode != dontHash) {
        int initialSize = 4190000;
        Path file = createFileWithSize(initialSize - 1);
        for (int fileSize = initialSize; fileSize < (10 * _1_MB); fileSize++) {
            byte contentByte = getContentByte(globalSequenceCount, false);
            globalSequenceCount++;
            Files.write(file, new byte[] { contentByte }, CREATE, APPEND);

            cut.hashFile(file, Files.size(file));
        }
    }
}

From source file:de.dentrassi.rpm.builder.YumMojo.java

private void addSinglePackage(final Path path, final Context context) throws IOException {
    final String checksum = makeChecksum(path);
    final String fileName = path.getFileName().toString();
    final String location = "packages/" + fileName;
    final FileInformation fileInformation = new FileInformation(Files.getLastModifiedTime(path).toInstant(),
            Files.size(path), location);

    final RpmInformation rpmInformation;
    try (RpmInputStream ris = new RpmInputStream(Files.newInputStream(path))) {
        rpmInformation = RpmInformations.makeInformation(ris);
    }/*from   w  ww  .j a  v  a  2 s  . com*/

    context.addPackage(fileInformation, rpmInformation, singletonMap(SHA256, checksum), SHA256);

    Files.copy(path, this.packagesPath.toPath().resolve(fileName), StandardCopyOption.COPY_ATTRIBUTES);
}

From source file:org.fao.geonet.api.groups.GroupsApi.java

/**
 * Writes the group logo image to the response. If no image is found it
 * writes a 1x1 transparent PNG. If the request contain cache related
 * headers it checks if the resource has changed and return a 304 Not
 * Modified response if not changed./*w  ww  . j a  va  2  s .c o m*/
 *
 * @param groupId    the group identifier.
 * @param webRequest the web request.
 * @param request    the native HTTP Request.
 * @param response   the servlet response.
 * @throws ResourceNotFoundException if no group exists with groupId.
 */
@ApiOperation(value = "Get the group logo image.", nickname = "get", notes = API_GET_LOGO_NOTE)
@RequestMapping(value = "/{groupId}/logo", method = RequestMethod.GET)
public void getGroupLogo(
        @ApiParam(value = "Group identifier", required = true) @PathVariable(value = "groupId") final Integer groupId,
        @ApiIgnore final WebRequest webRequest, HttpServletRequest request, HttpServletResponse response)
        throws ResourceNotFoundException {

    Locale locale = languageUtils.parseAcceptLanguage(request.getLocales());

    ApplicationContext context = ApplicationContextHolder.get();
    ServiceContext serviceContext = ApiUtils.createServiceContext(request, locale.getISO3Country());
    if (context == null) {
        throw new RuntimeException("ServiceContext not available");
    }

    GroupRepository groupRepository = context.getBean(GroupRepository.class);

    Group group = groupRepository.findOne(groupId);
    if (group == null) {
        throw new ResourceNotFoundException(
                messages.getMessage("api.groups.group_not_found", new Object[] { groupId }, locale));
    }
    try {
        final Path logosDir = Resources.locateLogosDir(serviceContext);
        final Path harvesterLogosDir = Resources.locateHarvesterLogosDir(serviceContext);
        final String logoUUID = group.getLogo();
        Path imagePath = null;
        FileTime lastModifiedTime = null;
        if (StringUtils.isNotBlank(logoUUID) && !logoUUID.startsWith("http://")
                && !logoUUID.startsWith("https//")) {
            imagePath = Resources.findImagePath(logoUUID, logosDir);
            if (imagePath == null) {
                imagePath = Resources.findImagePath(logoUUID, harvesterLogosDir);
            }
            if (imagePath != null) {
                lastModifiedTime = Files.getLastModifiedTime(imagePath);
                if (webRequest.checkNotModified(lastModifiedTime.toMillis())) {
                    // webRequest.checkNotModified sets the right HTTP headers
                    response.setDateHeader("Expires", System.currentTimeMillis() + SIX_HOURS * 1000L);

                    return;
                }
                response.setContentType(AttachmentsApi.getFileContentType(imagePath));
                response.setContentLength((int) Files.size(imagePath));
                response.addHeader("Cache-Control", "max-age=" + SIX_HOURS + ", public");
                response.setDateHeader("Expires", System.currentTimeMillis() + SIX_HOURS * 1000L);
                FileUtils.copyFile(imagePath.toFile(), response.getOutputStream());
            }
        }

        if (imagePath == null) {
            // no logo image found. Return a transparent 1x1 png
            lastModifiedTime = FileTime.fromMillis(0);
            if (webRequest.checkNotModified(lastModifiedTime.toMillis())) {
                return;
            }
            response.setContentType("image/png");
            response.setContentLength(TRANSPARENT_1_X_1_PNG.length);
            response.addHeader("Cache-Control", "max-age=" + SIX_HOURS + ", public");
            response.getOutputStream().write(TRANSPARENT_1_X_1_PNG);
        }

    } catch (IOException e) {
        Log.error(LOGGER,
                String.format("There was an error accessing the logo of the group with id '%d'", groupId));
        throw new RuntimeException(e);
    }
}

From source file:org.exist.storage.journal.JournalBinaryTest.java

@Override
protected XmldbURI storeAndVerify(final DBBroker broker, final Txn transaction, final Collection collection,
        final Path file, final String dbFilename)
        throws EXistException, PermissionDeniedException, IOException, TriggerException, LockException {

    final byte[] data = Files.readAllBytes(file);
    final BinaryDocument doc = collection.addBinaryResource(transaction, broker, XmldbURI.create(dbFilename),
            data, "application/octet-stream");

    assertNotNull(doc);/*from www .  j  av a 2 s  .c o m*/
    assertEquals(Files.size(file), doc.getContentLength());

    return collection.getURI().append(dbFilename);
}

From source file:org.fao.geonet.api.records.attachments.FilesystemStore.java

private MetadataResource getResourceDescription(String metadataUuid, MetadataResourceVisibility visibility,
        Path filePath) {/*  w  w w  .ja  v a  2s .  c o  m*/
    ApplicationContext _appContext = ApplicationContextHolder.get();
    SettingManager settingManager = _appContext.getBean(SettingManager.class);

    double fileSize = Double.NaN;
    try {
        fileSize = Files.size(filePath);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return new FilesystemStoreResource(metadataUuid + "/attachments/" + filePath.getFileName(),
            settingManager.getNodeURL() + "api/records/", visibility, fileSize);
}

From source file:codes.thischwa.c5c.impl.LocalConnector.java

/**
 * Construct file info.//from   ww  w .  j  av a  2s  . co  m
 * 
 * @param path the file
 * @param needSize the need size
 * @return the file info
 * @throws C5CException the connector exception
 */
private FileProperties constructFileInfo(Path path, boolean needSize) throws C5CException {
    InputStream imageIn = null;
    try {
        FileProperties fileProperties;
        Date lastModified = new Date(Files.getLastModifiedTime(path).toMillis());
        // 'needsize' isn't implemented in the filemanager yet, so the dimension is set if we have an image.
        String fileName = path.getFileName().toString();
        String ext = FilenameUtils.getExtension(fileName.toString());
        long size = Files.size(path);
        boolean isProtected = isProtected(path);
        if (isImageExtension(ext)) {
            imageIn = new BufferedInputStream(Files.newInputStream(path));
            Dimension dim = UserObjectProxy.getDimension(imageIn);
            fileProperties = buildForImage(fileName, isProtected, dim.width, dim.height, size, lastModified);
        } else {
            fileProperties = (Files.isDirectory(path)) ? buildForDirectory(fileName, isProtected, lastModified)
                    : buildForFile(fileName, isProtected, size, lastModified);
        }
        return fileProperties;
    } catch (FileNotFoundException e) {
        throw new C5CException(String.format("File not found: %s", path.getFileName().toString()));
    } catch (SecurityException | IOException e) {
        logger.warn("Error while analyzing an image!", e);
        throw new C5CException(String.format("Error while getting the dimension of the image %s: %s",
                path.getFileName().toString(), e.getMessage()));
    } finally {
        IOUtils.closeQuietly(imageIn);
    }
}