Example usage for java.nio.file.attribute BasicFileAttributes lastModifiedTime

List of usage examples for java.nio.file.attribute BasicFileAttributes lastModifiedTime

Introduction

In this page you can find the example usage for java.nio.file.attribute BasicFileAttributes lastModifiedTime.

Prototype

FileTime lastModifiedTime();

Source Link

Document

Returns the time of last modification.

Usage

From source file:org.cryptomator.frontend.webdav.servlet.DavFolder.java

private void copyInternal(DavNode destination, boolean shallow) throws DavException {
    assert exists();
    assert attr.isPresent();
    if (!Files.isDirectory(destination.path.getParent())) {
        throw new DavException(DavServletResponse.SC_CONFLICT, "Destination's parent doesn't exist.");
    }//from  w w w. j a  va2 s .c  om

    try {
        if (shallow && destination instanceof DavFolder) {
            // http://www.webdav.org/specs/rfc2518.html#copy.for.collections
            Files.createDirectory(destination.path);
            BasicFileAttributeView attrView = Files.getFileAttributeView(destination.path,
                    BasicFileAttributeView.class);
            if (attrView != null) {
                BasicFileAttributes a = attr.get();
                attrView.setTimes(a.lastModifiedTime(), a.lastAccessTime(), a.creationTime());
            }
        } else {
            Files.walkFileTree(path,
                    new CopyingFileVisitor(path, destination.path, StandardCopyOption.REPLACE_EXISTING));
        }
    } catch (IOException e) {
        throw new DavException(DavServletResponse.SC_INTERNAL_SERVER_ERROR, e);
    }
}

From source file:de.elomagic.carafile.client.CaraCloud.java

private List<Path> getLocalChangeList(final Path basePath, final long lastScanMillis) throws IOException {
    if (Files.notExists(basePath)) {
        return Collections.EMPTY_LIST;
    }/*  w w  w.  j  a  va2s .c  o m*/

    final List<Path> changedPathList = new ArrayList();

    Files.walkFileTree(basePath, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            if (attrs.lastModifiedTime().toMillis() > lastScanMillis) {
                changedPathList.add(dir);
            }
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if (attrs.lastModifiedTime().toMillis() > lastScanMillis) {
                changedPathList.add(file);
            }
            return FileVisitResult.CONTINUE;
        }

    });

    return changedPathList;
}

From source file:net.mozq.picto.core.ProcessCore.java

private static Date getBaseDate(ProcessCondition processCondition, Path file, BasicFileAttributes attrs,
        ImageMetadata imageMetadata) throws IOException {

    Date baseDate = null;//from w  w w .  j a  va2  s  .  com
    switch (processCondition.getBaseDateType()) {
    case CurrentDate:
        baseDate = new Date(System.currentTimeMillis());
        break;
    case FileCreationDate:
        baseDate = toDate(attrs.creationTime());
        break;
    case FileModifiedDate:
        baseDate = toDate(attrs.lastModifiedTime());
        break;
    case FileAccessDate:
        baseDate = toDate(attrs.lastAccessTime());
        break;
    case ExifDate:
        baseDate = getExifDate(imageMetadata);
        break;
    case CustomDate:
        baseDate = processCondition.getCustomBaseDate();
        break;
    default:
        throw new IllegalStateException(processCondition.getBaseDateType().toString());
    }

    if (baseDate != null) {
        if (processCondition.getBaseDateModType() != DateModType.None) {
            Calendar cal = Calendar.getInstance(processCondition.getTimeZone());
            cal.setTime(baseDate);
            int signum = 1;
            switch (processCondition.getBaseDateModType()) {
            case None:
                break;
            case Minus:
                signum = -1;
                // FALLTHRU
            case Plus:
                addField(cal, Calendar.YEAR, processCondition.getBaseDateModYears(), signum);
                addField(cal, Calendar.MONTH, processCondition.getBaseDateModMonths(), signum);
                addField(cal, Calendar.DAY_OF_MONTH, processCondition.getBaseDateModDays(), signum);
                addField(cal, Calendar.HOUR_OF_DAY, processCondition.getBaseDateModHours(), signum);
                addField(cal, Calendar.MINUTE, processCondition.getBaseDateModMinutes(), signum);
                addField(cal, Calendar.SECOND, processCondition.getBaseDateModSeconds(), signum);
                break;
            case Overwrite:
                setField(cal, Calendar.YEAR, processCondition.getBaseDateModYears());
                setField(cal, Calendar.MONTH, processCondition.getBaseDateModMonths());
                setField(cal, Calendar.DAY_OF_MONTH, processCondition.getBaseDateModDays());
                setField(cal, Calendar.HOUR_OF_DAY, processCondition.getBaseDateModHours());
                setField(cal, Calendar.MINUTE, processCondition.getBaseDateModMinutes());
                setField(cal, Calendar.SECOND, processCondition.getBaseDateModSeconds());
                break;
            default:
                throw new IllegalStateException(processCondition.getBaseDateModType().toString());
            }
            baseDate = cal.getTime();
        }
    }

    return baseDate;
}

From source file:com.willkara.zeteo.filetypes.impl.BaseFileType.java

/**
 * Creates the BaseFileType object from the Java {@link java.io.File} class
 * as an argument./*from  ww  w  . j  av a2 s . c  o m*/
 *
 * @param f The file object
 */
public BaseFileType(File f) {

    if (f.isFile()) {
        BasicFileAttributes attr;
        try {
            attr = Files.readAttributes(f.toPath(), BasicFileAttributes.class);

            fileName = f.getName();
            filePath = f.getCanonicalPath();
            fileSize = f.length();

            lastModifiedDate = new Date(attr.lastModifiedTime().toMillis());
            createdDate = new Date(attr.creationTime().toMillis());
            lastAccessDate = new Date(attr.lastAccessTime().toMillis());
        } catch (IOException ex) {
            Logger.getLogger(BaseFileType.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:org.application.backupsync.PathName.java

public JSONObject getAttrs() throws IOException, JSONException {
    JSONObject result;/*from  www . ja va  2s . co m*/
    BasicFileAttributes attr;
    DosFileAttributes dosAttr;
    PosixFileAttributes posixAttr;

    result = new JSONObject();
    attr = Files.readAttributes(this.path, BasicFileAttributes.class);

    result.append("ctime", attr.creationTime().toMillis());
    result.append("mtime", attr.lastModifiedTime().toMillis());
    //result.append("symlink", attr.isSymbolicLink()); //Redundant
    result.append("size", attr.size());

    if (System.getProperty("os.name").startsWith("Windows")) {
        dosAttr = Files.readAttributes(this.path, DosFileAttributes.class);

        result.append("dos:archive", dosAttr.isArchive());
        result.append("dos:hidden", dosAttr.isHidden());
        result.append("dos:readonly", dosAttr.isReadOnly());
        result.append("dos:system", dosAttr.isSystem());
    } else {
        posixAttr = Files.readAttributes(this.path, PosixFileAttributes.class);

        result.append("posix:symlink", posixAttr.isSymbolicLink());
        result.append("posix:owner", posixAttr.owner());
        result.append("posix:group", posixAttr.group());
        result.append("posix:permission", PosixFilePermissions.toString(posixAttr.permissions()));
    }

    return result;
}

From source file:org.cryptomator.webdav.jackrabbit.resources.EncryptedFile.java

@Override
protected void determineProperties() {
    final Path path = ResourcePathUtils.getPhysicalPath(this);
    if (Files.exists(path)) {
        SeekableByteChannel channel = null;
        try {/*ww  w  .j  av a2s. c o m*/
            channel = Files.newByteChannel(path, StandardOpenOption.READ);
            final Long contentLength = cryptor.decryptedContentLength(channel);
            properties.add(new DefaultDavProperty<Long>(DavPropertyName.GETCONTENTLENGTH, contentLength));

            final BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
            properties.add(new DefaultDavProperty<String>(DavPropertyName.CREATIONDATE,
                    FileTimeUtils.toRfc1123String(attrs.creationTime())));
            properties.add(new DefaultDavProperty<String>(DavPropertyName.GETLASTMODIFIED,
                    FileTimeUtils.toRfc1123String(attrs.lastModifiedTime())));
            properties.add(new HttpHeaderProperty(HttpHeader.ACCEPT_RANGES.asString(),
                    HttpHeaderValue.BYTES.asString()));
        } catch (IOException e) {
            LOG.error("Error determining metadata " + path.toString(), e);
            throw new IORuntimeException(e);
        } finally {
            IOUtils.closeQuietly(channel);
        }
    }
}

From source file:org.fim.command.DetectCorruptionCommandTest.java

private void simulateHardwareCorruption(String fileName) throws IOException {
    Path file = rootDir.resolve(fileName);
    // Keep original timestamps
    BasicFileAttributes attributes = Files.readAttributes(file, BasicFileAttributes.class);

    // A zero byte appears in the middle of the file
    byte[] bytes = Files.readAllBytes(file);
    bytes[bytes.length / 2] = 0;/*from ww w  .j a  va 2s  . c om*/

    Files.delete(file);
    Files.write(file, bytes, CREATE);

    // Restore the original timestamps
    Files.getFileAttributeView(file, BasicFileAttributeView.class).setTimes(attributes.lastModifiedTime(),
            attributes.lastAccessTime(), attributes.creationTime());
}

From source file:functionaltests.job.log.TestJobServerLogs.java

private void printDiagnosticMessage() {
    int LIMIT = 5;
    System.out.println("This test is going to fail, but before we print diagnostic message."
            + simpleDateFormat.format(new Date()));
    // iterate over all files in the 'logsLocation'
    for (File file : FileUtils.listFiles(new File(logsLocation), TrueFileFilter.INSTANCE,
            TrueFileFilter.INSTANCE)) {/*  w  w w  . j  av  a2s . co m*/
        try {
            BasicFileAttributes attr = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
            System.out.println(String.format("Name: %s, Size: %d, Created: %s, Modified: %s",
                    file.getAbsolutePath(), attr.size(), attr.creationTime(), attr.lastModifiedTime()));
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line;
            int i;
            // print up to LIMIT first lines
            for (i = 0; i < LIMIT && (line = br.readLine()) != null; ++i) {
                System.out.println(line);
            }

            Queue<String> queue = new CircularFifoQueue<>(LIMIT);
            // reading last LIMIT lines
            for (; (line = br.readLine()) != null; ++i) {
                queue.add(line);
            }

            if (i >= LIMIT * 2) { // if there is more line than 2*LIMIT
                System.out.println(".......");
                System.out.println("....... (skipped content)");
                System.out.println(".......");
            }
            for (String l : queue) { // print rest of the file
                System.out.println(l);
            }

            System.out.println("------------------------------------");
            System.out.println();
        } catch (IOException e) {
            System.out.println("Exception ocurred during accessing file attributes " + e);
        }
    }
}

From source file:org.cryptomator.frontend.webdav.servlet.DavResourceFactoryImpl.java

/**
 * @return <code>true</code> if a partial response should be generated according to an If-Range precondition.
 *///w w  w  .ja  v  a  2  s.  c  om
private boolean isIfRangeHeaderSatisfied(BasicFileAttributes attr, String ifRangeHeader) throws DavException {
    if (ifRangeHeader == null) {
        // no header set -> satisfied implicitly
        return true;
    } else {
        try {
            Instant expectedTime = Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(ifRangeHeader));
            Instant actualTime = attr.lastModifiedTime().toInstant();
            return expectedTime.compareTo(actualTime) == 0;
        } catch (DateTimeParseException e) {
            throw new DavException(DavServletResponse.SC_BAD_REQUEST,
                    "Unsupported If-Range header: " + ifRangeHeader);
        }
    }
}

From source file:org.cryptomator.webdav.jackrabbit.EncryptedDir.java

@Override
protected void determineProperties() {
    final Path path = ResourcePathUtils.getPhysicalPath(this);
    properties.add(new ResourceType(ResourceType.COLLECTION));
    properties.add(new DefaultDavProperty<Integer>(DavPropertyName.ISCOLLECTION, 1));
    if (Files.exists(path)) {
        try {/*from  w w w  .  j  a v a 2s .c  om*/
            final BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
            properties.add(new DefaultDavProperty<String>(DavPropertyName.CREATIONDATE,
                    FileTimeUtils.toRfc1123String(attrs.creationTime())));
            properties.add(new DefaultDavProperty<String>(DavPropertyName.GETLASTMODIFIED,
                    FileTimeUtils.toRfc1123String(attrs.lastModifiedTime())));
        } catch (IOException e) {
            LOG.error("Error determining metadata " + path.toString(), e);
            // don't add any further properties
        }
    }
}