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

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

Introduction

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

Prototype

Object fileKey();

Source Link

Document

Returns an object that uniquely identifies the given file, or null if a file key is not available.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {

    BasicFileAttributes attr = null;
    Path path = Paths.get("C:/tutorial/Java/JavaFX", "Topic.txt");

    attr = Files.readAttributes(path, BasicFileAttributes.class);

    System.out.println(attr.fileKey());

}

From source file:Test.java

public static void main(String[] args) throws Exception {
    Path path = FileSystems.getDefault().getPath("/home/docs/users.txt");
    // BasicFileAttributes attributes = Files.readAttributes(path,
    // BasicFileAttributes.class);
    BasicFileAttributeView view = Files.getFileAttributeView(path, BasicFileAttributeView.class);
    BasicFileAttributes attributes = view.readAttributes();

    System.out.println("Creation Time: " + attributes.creationTime());
    System.out.println("Last Accessed Time: " + attributes.lastAccessTime());
    System.out.println("Last Modified Time: " + attributes.lastModifiedTime());
    System.out.println("File Key: " + attributes.fileKey());
    System.out.println("Directory: " + attributes.isDirectory());
    System.out.println("Other Type of File: " + attributes.isOther());
    System.out.println("Regular File: " + attributes.isRegularFile());
    System.out.println("Symbolic File: " + attributes.isSymbolicLink());
    System.out.println("Size: " + attributes.size());
}

From source file:com.github.sakserv.lslock.cli.LockListerCli.java

/**
 * Returns the inode of the supplied File
 *
 * @param lockFile      Retrieve inode for this File
 * @return      The inode of the supplied lockFile
 * @throws IOException      If the File can not be found
 *///from   w ww. j ava 2  s .  c o m
public static int getInode(File lockFile) throws IOException {
    BasicFileAttributes basicFileAttributes = Files.readAttributes(lockFile.toPath(),
            BasicFileAttributes.class);
    Object fileKey = basicFileAttributes.fileKey();
    String fileAttrString = fileKey.toString();
    return Integer.parseInt(
            fileAttrString.substring(fileAttrString.indexOf("ino=") + 4, fileAttrString.indexOf(")")));
}

From source file:com.liferay.sync.engine.util.FileUtil.java

public static String getFileKey(Path filePath) {
    if (!Files.exists(filePath)) {
        return "";
    }/* w ww  .  j  a  v  a2  s . c om*/

    try {
        BasicFileAttributes basicFileAttributes = Files.readAttributes(filePath, BasicFileAttributes.class);

        if (OSDetector.isWindows()) {
            return String.valueOf(basicFileAttributes.creationTime());
        } else {
            Object fileKey = basicFileAttributes.fileKey();

            return fileKey.toString();
        }
    } catch (Exception e) {
        _logger.error(e.getMessage(), e);

        return "";
    }
}

From source file:com.sunteng.flume.source.tail.TailFileEventReader.java

public static String getFileKey(File file) {
    BasicFileAttributes basicAttr;
    try {//from   ww w .j  a  va2s . c o m
        basicAttr = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
    } catch (IOException e) {
        return new String();
    }
    Object obj = basicAttr.fileKey();
    if (obj == null) {
        return new String();
    }
    return obj.toString();
}

From source file:com.streamsets.pipeline.lib.io.LiveFile.java

/**
 * Refreshes the <code>LiveFile</code>, if the file was renamed, the path will have the new name.
 *
 * @return the refreshed file if the file has been renamed, or itself if the file has not been rename or the file
 * does not exist in the directory anymore.
 * @throws IOException thrown if the LiveFile could not be refreshed
 *///from   ww w . j a  va  2 s. c  om
public LiveFile refresh() throws IOException {
    LiveFile refresh = this;
    boolean changed;
    try {
        BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
        String iNodeCurrent = attrs.fileKey().toString();
        int headLenCurrent = (int) Math.min(headLen, attrs.size());
        String headHashCurrent = computeHash(path, headLenCurrent);
        changed = !this.iNode.equals(iNodeCurrent) || !this.headHash.equals(headHashCurrent);
    } catch (NoSuchFileException ex) {
        changed = true;
    }
    if (changed) {

        try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(path.getParent())) {
            for (Path path : directoryStream) {
                if (path.toFile().isDirectory()) {
                    continue;
                }
                BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
                String iNode = attrs.fileKey().toString();
                int headLen = (int) Math.min(this.headLen, attrs.size());
                String headHash = computeHash(path, headLen);
                if (iNode.equals(this.iNode) && headHash.equals(this.headHash)) {
                    if (headLen == 0) {
                        headLen = (int) Math.min(HEAD_LEN, attrs.size());
                        headHash = computeHash(path, headLen);
                    }
                    return new LiveFile(path, iNode, headHash, headLen);
                } /**rename??*/
            }
        }
        return null;
    } /**change? itself*/
    return refresh;
}

From source file:de.tiqsolutions.hdfs.BasicFileAttributeViewImpl.java

Map<String, Object> readAttributes(String attributes) throws IOException {
    BasicFileAttributes attr = readAttributes();
    List<String> attrlist = Arrays.asList(attributes.split(","));
    boolean readall = attrlist.contains("*");
    Map<String, Object> ret = new HashMap<>();
    if (readall || attrlist.contains("fileKey"))
        ret.put("fileKey", attr.fileKey());
    if (readall || attrlist.contains("creationTime"))
        ret.put("creationTime", attr.creationTime());
    if (readall || attrlist.contains("isDirectory"))
        ret.put("isDirectory", attr.isDirectory());
    if (readall || attrlist.contains("isOther"))
        ret.put("isOther", attr.isOther());
    if (readall || attrlist.contains("isRegularFile"))
        ret.put("isRegularFile", attr.isRegularFile());
    if (readall || attrlist.contains("isSymbolicLink"))
        ret.put("isSymbolicLink", attr.isSymbolicLink());
    if (readall || attrlist.contains("lastAccessTime"))
        ret.put("lastAccessTime", attr.lastAccessTime());
    if (readall || attrlist.contains("lastModifiedTime"))
        ret.put("lastModifiedTime", attr.lastModifiedTime());
    if (readall || attrlist.contains("size"))
        ret.put("size", attr.size());
    return ret;//from   ww w. j  a va 2 s. co m
}

From source file:com.streamsets.pipeline.lib.io.LiveFile.java

/**
 * Creates a <code>LiveFile</code> given a {@link Path}.
 *
 * @param path the Path of the LiveFile. The file referred by the Path must exist.
 * @throws IOException thrown if the LiveFile does not exist.
 *//*from w w w .jav a 2 s .c o  m*/
public LiveFile(Path path) throws IOException {
    Utils.checkNotNull(path, "path");
    this.path = path.toAbsolutePath();
    if (!Files.isRegularFile(this.path)) {
        throw new NoSuchFileException(Utils.format("Path '{}' is not a file", this.path));
    }
    BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
    headLen = (int) Math.min(HEAD_LEN, attrs.size());
    headHash = computeHash(path, headLen);
    iNode = attrs.fileKey().toString();
}

From source file:edu.lternet.pasta.datapackagemanager.DataPackageManagerResourceTest.java

/**
 * Test that the StorageManager has optimized the data
 * storage for two data entities with the same checksum value.
 * For this test we can use the original revision value and
 * the update revision value because we know that they have
 * the same data entity.//from   w w w  .j a va 2  s  .  c o  m
 */
private void testStorageManager() {
    HttpHeaders httpHeaders = new DummyCookieHttpHeaders(testUser);
    FileSystem fileSystem = FileSystems.getDefault();

    Response response = dataPackageManagerResource.readDataEntity(httpHeaders, testScope, testIdentifier,
            testRevision.toString(), testEntityId);
    int statusCode = response.getStatus();
    assertEquals(200, statusCode);
    File revisionDataEntity = (File) response.getEntity(); // Check the message body
    assertNotNull(revisionDataEntity);
    String revisionFilePathStr = revisionDataEntity.getAbsolutePath();
    Path revisionPath = fileSystem.getPath(revisionFilePathStr);
    System.err.println(String.format("revisionPath: %s", revisionFilePathStr));

    response = dataPackageManagerResource.readDataEntity(httpHeaders, testScope, testIdentifier,
            testUpdateRevision.toString(), testEntityId);
    statusCode = response.getStatus();
    assertEquals(200, statusCode);
    File updateRevisionDataEntity = (File) response.getEntity(); // Check the message body
    assertNotNull(updateRevisionDataEntity);
    String updateRevisionFilePathStr = updateRevisionDataEntity.getAbsolutePath();
    Path updateRevisionPath = fileSystem.getPath(updateRevisionFilePathStr);
    System.err.println(String.format("updateRevisionPath: %s", updateRevisionFilePathStr));

    response = dataPackageManagerResource.readDataEntity(httpHeaders, testScope, testIdentifier,
            testRevision.toString(), testEntityId2);
    statusCode = response.getStatus();
    assertEquals(200, statusCode);
    File revisionDataEntity2 = (File) response.getEntity(); // Check the message body
    assertNotNull(revisionDataEntity);
    String revisionFilePathStr2 = revisionDataEntity2.getAbsolutePath();
    Path revisionPath2 = fileSystem.getPath(revisionFilePathStr2);
    System.err.println(String.format("revisionPath2: %s", revisionFilePathStr2));

    response = dataPackageManagerResource.readDataEntity(httpHeaders, testScope, testIdentifier,
            testUpdateRevision.toString(), testEntityId2);
    statusCode = response.getStatus();
    assertEquals(200, statusCode);
    File updateRevisionDataEntity2 = (File) response.getEntity(); // Check the message body
    assertNotNull(updateRevisionDataEntity2);
    String updateRevisionFilePathStr2 = updateRevisionDataEntity2.getAbsolutePath();
    Path updateRevisionPath2 = fileSystem.getPath(updateRevisionFilePathStr2);
    System.err.println(String.format("updateRevisionPath2: %s", updateRevisionFilePathStr2));

    try {
        // Get the unique file keys (i.e. inodes) for the revision's data entities
        BasicFileAttributes revisionAttributes = Files.readAttributes(revisionPath, BasicFileAttributes.class);
        Object revisionKey = revisionAttributes.fileKey();
        BasicFileAttributes revisionAttributes2 = Files.readAttributes(revisionPath2,
                BasicFileAttributes.class);
        Object revisionKey2 = revisionAttributes2.fileKey();

        // Get the unique file keys (i.e. inodes) for the updated revision's data entities
        BasicFileAttributes updateRevisionAttributes = Files.readAttributes(updateRevisionPath,
                BasicFileAttributes.class);
        Object updateRevisionKey = updateRevisionAttributes.fileKey();
        BasicFileAttributes updateRevisionAttributes2 = Files.readAttributes(updateRevisionPath2,
                BasicFileAttributes.class);
        Object updateRevisionKey2 = updateRevisionAttributes2.fileKey();

        /*
         * The fileKey() method returns null on the Windows platform, so
         * this test really only works on Unix/Linux platform.
         */
        if (isWindowsPlatform) {
            assertTrue((revisionKey == null) && (updateRevisionKey == null) && (revisionKey2 == null)
                    && (updateRevisionKey2 == null));
        } else {
            assertTrue(revisionKey != null);
            assertTrue(updateRevisionKey != null);
            assertTrue(revisionKey2 != null);
            assertTrue(updateRevisionKey2 != null);
            assertTrue(revisionKey.equals(updateRevisionKey));
            assertTrue(revisionKey2.equals(updateRevisionKey2));
            assertFalse(revisionKey.equals(updateRevisionKey2));
            assertFalse(revisionKey2.equals(updateRevisionKey));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:org.sonar.application.AppFileSystemTest.java

@CheckForNull
private static Object getFileKey(File fileInTempDir) throws IOException {
    Path path = Paths.get(fileInTempDir.toURI());
    BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
    return attrs.fileKey();
}