Search in sources :

Example 36 with FileTime

use of java.nio.file.attribute.FileTime in project jdk8u_jdk by JetBrains.

the class Basic method neq.

static void neq(long v1, TimeUnit u1, long v2, TimeUnit u2) {
    FileTime t1 = FileTime.from(v1, u1);
    FileTime t2 = FileTime.from(v2, u2);
    if (t1.equals(t2))
        throw new RuntimeException("should not be equal");
}
Also used : FileTime(java.nio.file.attribute.FileTime)

Example 37 with FileTime

use of java.nio.file.attribute.FileTime in project jdk8u_jdk by JetBrains.

the class Basic method to.

static void to(long v, TimeUnit unit) {
    FileTime t = FileTime.from(v, unit);
    for (TimeUnit u : TimeUnit.values()) {
        long result = t.to(u);
        long expected = u.convert(v, unit);
        if (result != expected) {
            throw new RuntimeException("unexpected result");
        }
    }
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) FileTime(java.nio.file.attribute.FileTime)

Example 38 with FileTime

use of java.nio.file.attribute.FileTime in project jdk8u_jdk by JetBrains.

the class TestExtraTime method testTimeConversions.

static void testTimeConversions(long from, long to, long step) {
    ZipEntry ze = new ZipEntry("TestExtraTime.java");
    for (long time = from; time <= to; time += step) {
        ze.setTime(time);
        FileTime lastModifiedTime = ze.getLastModifiedTime();
        if (lastModifiedTime.toMillis() != time) {
            throw new RuntimeException("setTime should make getLastModifiedTime " + "return the specified instant: " + time + " got: " + lastModifiedTime.toMillis());
        }
        if (ze.getTime() != time) {
            throw new RuntimeException("getTime after setTime, expected: " + time + " got: " + ze.getTime());
        }
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) FileTime(java.nio.file.attribute.FileTime)

Example 39 with FileTime

use of java.nio.file.attribute.FileTime in project logging-log4j2 by apache.

the class IfLastModified method accept.

/*
     * (non-Javadoc)
     * 
     * @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#accept(java.nio.file.Path,
     * java.nio.file.Path, java.nio.file.attribute.BasicFileAttributes)
     */
@Override
public boolean accept(final Path basePath, final Path relativePath, final BasicFileAttributes attrs) {
    final FileTime fileTime = attrs.lastModifiedTime();
    final long millis = fileTime.toMillis();
    final long ageMillis = CLOCK.currentTimeMillis() - millis;
    final boolean result = ageMillis >= age.toMillis();
    final String match = result ? ">=" : "<";
    final String accept = result ? "ACCEPTED" : "REJECTED";
    LOGGER.trace("IfLastModified {}: {} ageMillis '{}' {} '{}'", accept, relativePath, ageMillis, match, age);
    if (result) {
        return IfAll.accept(nestedConditions, basePath, relativePath, attrs);
    }
    return result;
}
Also used : FileTime(java.nio.file.attribute.FileTime)

Example 40 with FileTime

use of java.nio.file.attribute.FileTime in project lucene-solr by apache.

the class NativeFSLockFactory method obtainFSLock.

@Override
protected Lock obtainFSLock(FSDirectory dir, String lockName) throws IOException {
    Path lockDir = dir.getDirectory();
    // Ensure that lockDir exists and is a directory.
    // note: this will fail if lockDir is a symlink
    Files.createDirectories(lockDir);
    Path lockFile = lockDir.resolve(lockName);
    try {
        Files.createFile(lockFile);
    } catch (IOException ignore) {
    // we must create the file to have a truly canonical path.
    // if it's already created, we don't care. if it cant be created, it will fail below.
    }
    // fails if the lock file does not exist
    final Path realPath = lockFile.toRealPath();
    // used as a best-effort check, to see if the underlying file has changed
    final FileTime creationTime = Files.readAttributes(realPath, BasicFileAttributes.class).creationTime();
    if (LOCK_HELD.add(realPath.toString())) {
        FileChannel channel = null;
        FileLock lock = null;
        try {
            channel = FileChannel.open(realPath, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
            lock = channel.tryLock();
            if (lock != null) {
                return new NativeFSLock(lock, channel, realPath, creationTime);
            } else {
                throw new LockObtainFailedException("Lock held by another program: " + realPath);
            }
        } finally {
            if (lock == null) {
                // not successful - clear up and move out
                // TODO: addSuppressed
                IOUtils.closeWhileHandlingException(channel);
                // clear LOCK_HELD last 
                clearLockHeld(realPath);
            }
        }
    } else {
        throw new LockObtainFailedException("Lock held by this virtual machine: " + realPath);
    }
}
Also used : Path(java.nio.file.Path) FileChannel(java.nio.channels.FileChannel) FileLock(java.nio.channels.FileLock) FileTime(java.nio.file.attribute.FileTime) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Aggregations

FileTime (java.nio.file.attribute.FileTime)40 Path (java.nio.file.Path)11 File (java.io.File)7 Test (org.junit.Test)7 Test (org.testng.annotations.Test)7 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)6 IOException (java.io.IOException)5 SegmentMetadataImpl (com.linkedin.pinot.core.segment.index.SegmentMetadataImpl)4 BasicFileAttributeView (java.nio.file.attribute.BasicFileAttributeView)3 ProcessResult (com.facebook.buck.testutil.integration.ProjectWorkspace.ProcessResult)2 IndexSegment (com.linkedin.pinot.core.indexsegment.IndexSegment)2 SegmentV1V2ToV3FormatConverter (com.linkedin.pinot.core.segment.index.converter.SegmentV1V2ToV3FormatConverter)2 FileNotFoundException (java.io.FileNotFoundException)2 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)2 NoSuchFileException (java.nio.file.NoSuchFileException)2 Date (java.util.Date)2 BuildTarget (com.facebook.buck.model.BuildTarget)1 ProjectWorkspace (com.facebook.buck.testutil.integration.ProjectWorkspace)1 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)1 ImmutableMap (com.google.common.collect.ImmutableMap)1