Java 类java.nio.file.attribute.UserDefinedFileAttributeView 实例源码

项目:uavstack    文件:ReliableTaildirEventReader.java   
private long getInode(File file) throws IOException {

        UserDefinedFileAttributeView view = null;
        // windows system and file customer Attribute
        if (OS_WINDOWS.equals(os)) {
            view = Files.getFileAttributeView(file.toPath(), UserDefinedFileAttributeView.class);// 把文件的内容属性值放置在view里面?
            try {
                ByteBuffer buffer = ByteBuffer.allocate(view.size(INODE));// view.size得到inode属性值大小
                view.read(INODE, buffer);// 把属性值放置在buffer中
                buffer.flip();
                return Long.parseLong(Charset.defaultCharset().decode(buffer).toString());// 返回编码后的inode的属性值

            }
            catch (NoSuchFileException e) {
                long winode = random.nextLong();
                view.write(INODE, Charset.defaultCharset().encode(String.valueOf(winode)));
                return winode;
            }
        }
        long inode = (long) Files.getAttribute(file.toPath(), "unix:ino");// 返回unix的inode的属性值
        return inode;
    }
项目:tinyMediaManager    文件:FSTest.java   
private Set<String> getSupportedFileAttributes(FileStore fs) {
  Set<String> attrs = new HashSet<String>();
  if (fs.supportsFileAttributeView(AclFileAttributeView.class)) {
    attrs.add("acl");
  }
  if (fs.supportsFileAttributeView(BasicFileAttributeView.class)) {
    attrs.add("basic");
  }
  if (fs.supportsFileAttributeView(FileOwnerAttributeView.class)) {
    attrs.add("owner");
  }
  if (fs.supportsFileAttributeView(UserDefinedFileAttributeView.class)) {
    attrs.add("user");
  }
  if (fs.supportsFileAttributeView(DosFileAttributeView.class)) {
    attrs.add("dos");
  }
  if (fs.supportsFileAttributeView(PosixFileAttributeView.class)) {
    attrs.add("posix");
  }
  if (fs.supportsFileAttributeView(FileAttributeView.class)) {
    attrs.add("file");
  }
  return attrs;
}
项目:libre    文件:MkAttributes.java   
@Override
public boolean visible() throws IOException {
    final boolean shown;
    final UserDefinedFileAttributeView view =
        Files.getFileAttributeView(
            this.file.toPath(), UserDefinedFileAttributeView.class
        );
    if (view.list().contains(MkAttributes.VISIBILITY)) {
        final ByteBuffer buf = ByteBuffer.allocate(
            view.size(MkAttributes.VISIBILITY)
        );
        view.read(MkAttributes.VISIBILITY, buf);
        buf.flip();
        shown = Boolean.valueOf(
            StandardCharsets.UTF_8.decode(buf).toString()
        );
    } else {
        shown = false;
    }
    return shown;
}
项目:jsr203-hadoop    文件:TestFileStore.java   
/**
 * Test: File and FileStore attributes
 */
@Test
public void testFileStoreAttributes() throws URISyntaxException, IOException {
  URI uri = clusterUri.resolve("/tmp/testFileStore");
  Path path = Paths.get(uri);
  if (Files.exists(path))
    Files.delete(path);
  assertFalse(Files.exists(path));
  Files.createFile(path);
  assertTrue(Files.exists(path));
  FileStore store1 = Files.getFileStore(path);
  assertNotNull(store1);
  assertTrue(store1.supportsFileAttributeView("basic"));
  assertTrue(store1.supportsFileAttributeView(BasicFileAttributeView.class));
  assertTrue(store1.supportsFileAttributeView("posix") == store1
      .supportsFileAttributeView(PosixFileAttributeView.class));
  assertTrue(store1.supportsFileAttributeView("dos") == store1
      .supportsFileAttributeView(DosFileAttributeView.class));
  assertTrue(store1.supportsFileAttributeView("acl") == store1
      .supportsFileAttributeView(AclFileAttributeView.class));
  assertTrue(store1.supportsFileAttributeView("user") == store1
      .supportsFileAttributeView(UserDefinedFileAttributeView.class));
}
项目:Singularity    文件:SingularityUploader.java   
Optional<Long> readFileAttributeAsLong(Path file, String attribute, UserDefinedFileAttributeView view, List<String> knownAttributes) {
  if (knownAttributes.contains(attribute)) {
    try {
      LOG.trace("Attempting to read attribute {}, from file {}", attribute, file);
      ByteBuffer buf = ByteBuffer.allocate(view.size(attribute));
      view.read(attribute, buf);
      buf.flip();
      String value = Charset.defaultCharset().decode(buf).toString();
      if (Strings.isNullOrEmpty(value)) {
        LOG.debug("No attrbiute {} found for file {}", attribute, file);
        return Optional.absent();
      }
      return Optional.of(Long.parseLong(value));
    } catch (Exception e) {
      LOG.error("Error getting extra file metadata for {}", file, e);
      return Optional.absent();
    }
  } else {
    return Optional.absent();
  }
}
项目:Singularity    文件:SingularityUploader.java   
UploaderFileAttributes getFileAttributes(Path file) {
  Set<String> supportedViews = FileSystems.getDefault().supportedFileAttributeViews();
  LOG.trace("Supported attribute views are {}", supportedViews);
  if (supportedViews.contains("user")) {
    try {
      UserDefinedFileAttributeView view = Files.getFileAttributeView(file, UserDefinedFileAttributeView.class);
      List<String> attributes = view.list();
      LOG.debug("Found file attributes {} for file {}", attributes, file);
      Optional<Long> maybeStartTime = readFileAttributeAsLong(file, LOG_START_TIME_ATTR, view, attributes);
      Optional<Long> maybeEndTime = readFileAttributeAsLong(file, LOG_END_TIME_ATTR, view, attributes);
      return new UploaderFileAttributes(maybeStartTime, maybeEndTime);
    } catch (Exception e) {
      LOG.error("Could not get extra file metadata for {}", file, e);
    }
  }
  return new UploaderFileAttributes(Optional.absent(), Optional.absent());
}
项目:incubator-netbeans    文件:FileObjTest.java   
/**
 * Test for bug 240953 - Netbeans Deletes User Defined Attributes.
 *
 * @throws java.io.IOException
 */
public void testWritingKeepsFileAttributes() throws IOException {

    final String attName = "User_Attribute";
    final String attValue = "User_Attribute_Value";

    if (Utilities.isWindows()) {
        clearWorkDir();
        File f = new File(getWorkDir(), "fileWithAtts.txt");
        f.createNewFile();
        UserDefinedFileAttributeView attsView = Files.getFileAttributeView(
                f.toPath(), UserDefinedFileAttributeView.class);
        ByteBuffer buffer = Charset.defaultCharset().encode(attValue);
        attsView.write(attName, buffer);

        buffer.rewind();
        attsView.read(attName, buffer);
        buffer.flip();
        String val = Charset.defaultCharset().decode(buffer).toString();
        assertEquals(attValue, val);

        FileObject fob = FileUtil.toFileObject(f);
        OutputStream os = fob.getOutputStream();
        try {
            os.write(55);
        } finally {
            os.close();
        }

        buffer.rewind();
        attsView.read(attName, buffer);
        buffer.flip();
        String val2 = Charset.defaultCharset().decode(buffer).toString();
        assertEquals(attValue, val2);
    }
}
项目:neoscada    文件:Executables.java   
public static void setExecutable ( final Path path, final boolean state ) throws IOException
{
    final UserDefinedFileAttributeView ua = Files.getFileAttributeView ( path, UserDefinedFileAttributeView.class );
    if ( state )
    {
        ua.write ( ATTR_EXECUTE, ByteBuffer.wrap ( marker ) );
    }
    else
    {
        ua.delete ( ATTR_EXECUTE );
    }
}
项目:neoscada    文件:Executables.java   
public static boolean getExecutable ( final Path path ) throws IOException
{
    final UserDefinedFileAttributeView ua = Files.getFileAttributeView ( path, UserDefinedFileAttributeView.class );

    if ( !ua.list ().contains ( ATTR_EXECUTE ) )
    {
        // check first, otherwise the size() call with give an exception
        return false;
    }

    final ByteBuffer buf = ByteBuffer.allocate ( ua.size ( ATTR_EXECUTE ) );
    ua.read ( ATTR_EXECUTE, buf );
    buf.flip ();
    return Boolean.parseBoolean ( CHARSET.decode ( buf ).toString () );
}
项目:uavstack    文件:DoTestRuleFilterFactory.java   
@Test
public void setCustomerAttr() throws IOException {

    Path target = Paths.get("/Users/fathead/temp/file4");
    UserDefinedFileAttributeView view = Files.getFileAttributeView(target, UserDefinedFileAttributeView.class);
    view.write(name, Charset.defaultCharset().encode("pinelet"));
}
项目:uavstack    文件:DoTestRuleFilterFactory.java   
@Test
public void getCustomerAttr() throws IOException {

    Path target = Paths.get("/Users/fathead/temp/file4");
    UserDefinedFileAttributeView view2 = Files.getFileAttributeView(target, UserDefinedFileAttributeView.class);
    ByteBuffer buf = ByteBuffer.allocate(view2.size(name));
    view2.read(name, buf);
    buf.flip();
    String value = Charset.defaultCharset().decode(buf).toString();
    System.out.println("value=" + value);
}
项目:ListOfJavaNioFileSystems    文件:MarschallWindowsTest.java   
@BeforeClass
public static void before() throws IOException {

    fsDescription = build().
            windows().noUNC().noRootComponents().next().
            playground().set( MemoryFileSystemBuilder.newWindows().addFileAttributeView( UserDefinedFileAttributeView.class ).build( "marschall" ).getPath( "play" ).toAbsolutePath() ).
            time().noLastAccessTime().next().
            pathConstraints().noMaxFilenameLength().next().
            closable().no().
            hardlinks().no().
            symlinks().noDirs().next().
            watchable().no().
            bugScheme( "RelSymLink", true ).
            bug( "testAppendAndReadThrows" ).
            bug("testEveryChannelWriteUpdatesLastModifiedTime").
            bug("testGetNameOfDefaultPathIsItself").
            bug("testPathMatcherKnowsGlob").
            bug("testPathMatherGlob").
            bug("testReadCreateNonExistingFileThrows").
            bug("testCopyIntoItself").
            bug("testCopyNonEmptyDirDoesNotCopyKids").
            bug("testUnsupportedAttributeViewReturnsNull").
            bug("testIsSameFileOfDifferentPathNonExistingFileIsNot").
            bug( "testIsSameFileOfDifferentPathNonExistingFile2IsNot").
            bug( "testCopyToSymLink").
            bug( "testSymLinkToUnnormalizedRelPath" ).
            bug("testIsSameFileOtherProvider2").
            bug( "testIsSameFileOnEqualPathElementsOtherProvider" ).
            bug( "testGetFileStoreOfNonExistent" ).
            bug( "testGetFileStoreOfBrokenSymLink" ).
            bug( "testPathToUriAndBackIsSame").
            bug( "testPathWithWitespaceToUriAndBack").
            nitpick( "testIsSameFileOtherProvider", "strange anyway").
            nitpick("testGetPathOtherURI", "different exception").
            nitpick("testRegisterOnClosedWatchService", "different exception").
            done();


}
项目:libre    文件:MkAttributes.java   
@Override
public void show(final boolean shwn) throws IOException {
    Files.getFileAttributeView(
        this.file.toPath(), UserDefinedFileAttributeView.class
    ).write(
            MkAttributes.VISIBILITY,
            StandardCharsets.UTF_8.encode(String.valueOf(shwn))
        );
}
项目:niotest    文件:Tests06Attributes.java   
@Test
@Category( Attributes.class )
public void testUserDefinedAttributes() {
    try {
        Files.getFileAttributeView( getFile(), UserDefinedFileAttributeView.class );
    } catch( Exception e ) {
        fail( "UserDefinedAttributeView must be supported" );
    }
}
项目:javase-study    文件:AttributeMain.java   
public static void main(String[] args) throws IOException {
//    第三个并且是最后一个例子涉及了利用 java.nio.file.attribute 包中的类获取并设置文件属性的新的 API。
//
//    新的 API 能够提供对各种文件属性的访问。在以前的 Java 版本中,仅能得到基本的文件属性集(大小、修改时间、文件是否隐藏、以及它是文件还是目录)。为了获取或者修改更多的文件属性,必须利用运行所在平台特定的本地代码来实现,这很困难。很幸运的是,Java 7 能够允许您通过很简单的方式,利用 java.nio.file.attribute 类来读取,如果可能,修改扩展的属性集,完全去掉了这些操作的平台特定属性。
//
//    在新的 API 中有七个属性视图,其中一些特定于操作系统。这些 “ 视图 ” 类允许您获取并设置任何关联的属性,并且其中每个都具有对应的包含真实属性信息的属性类。让我们依次来看一下。
//    AclFileAttributeView 与 AclEntry
//
//    AclFileAttributeView 允许您为特定文件设置 ACL 及文件所有者属性。其 getAcl() 方法返回一个 List of AclEntry 对象,每个对应文件的一个权限集。其 setAcl(List<AclEntry>) 方法允许您修改该访问列表。这些属性视图仅可用于 Microsoft® Windows® 系统。
//    BasicFileAttributeView 与 BasicFileAttributes
//
//    这一视图类允许您获取一系列 —— 平常的 —— 基本文件属性,构建于以前的 Java 版本之上。其 readAttributes() 方法返回一个 BasicFileAttributes 实例,该实例包含最后修改时间、最后访问时间、创建时间、大小、以及文件属性等细节(常规文件、目录、符号链接、或者其他)。这一属性视图在所有平台上均可用。
    File attribFile = new File(".");
    Path attribPath = attribFile.toPath();
//    为获取想要的文件属性视图,我们在 Path 上使用 getFileAttributeView(Class viewClass) 方法。为获取 BasicFileAttributeView for attribPath,我们简单地调用:

    BasicFileAttributeView basicFileAttributeView =Files.getFileAttributeView(attribPath, BasicFileAttributeView.class);
//    正如前面所描述的,为从 BasicFileAttributeView 获取 BasicFileAttributes,我们只要调用其 readAttributes() 方法:
    BasicFileAttributes basicAttribs = basicFileAttributeView.readAttributes();
//    那么这样就可以了,现在已经得到了您所想要的任何基本文件属性。对于 BasicFileAttributes,只有创建、最后修改、以及最后访问时间可被修改(因为改变文件大小或者类型没有意义)。为改变这些,我们可以使用 java.nio.file.attribute.FileTime 类来创建新的时间,然后在 BasicFileAttributeView 上调用 setTimes() 方法。例如,我们可以不断地更新文件的最后修改时间。
    FileTime newModTime
            = FileTime.fromMillis(basicAttribs.lastModifiedTime().toMillis() + 60000);
    basicFileAttributeView.setTimes(newModTime, null, null);
//    这两个 null 指出,我们不想改变这一文件的最后访问时间或者创建时间。如果以前面相同的方式再次检查基本属性,您会发现最后修改时间已被修改,但是创建时间和最后访问时间还保持原样。

//    DosFileAttributeView 与 DosFileAttributes
//
//    这一视图类允许您获取指定给 DOS 的属性。(您可能会猜想,这一视图仅用于 Windows 系统。)其 readAttributes() 方法返回一个 DosFileAttributes 实例,该实例包含有问题的文件是否为只读、隐藏、系统文件、以及存档文件等细节信息。这一视图还包含针对每个属性的 set*(boolean) 方法。
//    FileOwnerAttributeView 与 UserPrincipal
//
//    这一视图类允许您获取并设置特定文件的所有者。其 getOwner()方法返回一个 UserPrincipal(还处于 java.nio.file.attribute 包中),其又具有 getName() 方法,来返回包含所有者名字的 String。该视图还提供 setOwner(UserPrincipal) 方法用于变更文件所有者。该视图在所有平台上都可用。
//    FileStoreSpaceAttributeView 与 FileStoreSpaceAttributes
//
//    这一用很吸引人的方式命名的类,允许您获取有关特定文件存储的信息。其 readAttributes() 方法返回一个包含文件存储的整个空间、未分配空间、以及已使用空间细节的 FileStoreSpaceAttributes 实例。这一视图在所有平台上都可用。
//    PosixFileAttributeView 与 PosixFileAttributes
//
//    这一视图类,仅在 UNIX® 系统上可用,允许您获取并设置指定给 POSIX(Portable Operating System Interface)的属性。其 readAttributes() 方法返回一个包含有关这一文件的所有者、组所有者、以及这一文件许可(这些细节通常用 UNIX chmod 命令设置)的 PosixFileAttributes 实例。这一视图还提供 setOwner(UserPrincipal)、 setGroup(GroupPrincipal)、以及 setPermissions(Set<PosixFilePermission>) 来修改这些属性。
//    UserDefinedFileAttributeView 与 String
//
//    这一视图类,仅可用于 Windows,允许您获取并设置文件的扩展属性。 这些属性跟其他的不同,它们只是名称值对,并可按需对其进行设置。 如果想向文件增加一些隐藏的元数据,而不必修改文件内容,这就很有用了。 这一属性提供 list() 方法,来为相关的文件返回 List of String 扩展属性的名字。
//
//    有了其名字后,就要获取特定属性的内容,这一视图具有一个 size(String name) 方法来返回属性值的大小,以及一个 read(String name, ByteBuffer dest) 方法来将属性值读取到 ByteBuffer 中。这一视图还提供 write(String name, ByteBuffer source) 方法来创建或者修改属性,以及一个 delete(String name) 方法来完全移除现有的属性。
//
//    这可能是最有趣的新属性视图,因为它允许您利用任意 String 名字和 ByteBuffer 值向文件增加属性。这很对 —— 其值是个 ByteBuffer,因此您可以在这里存储任何二进制数据。

    UserDefinedFileAttributeView userView
            = Files.getFileAttributeView(attribPath, UserDefinedFileAttributeView.class);
//    为获取用户为这一文件定义的属性名,我们在视图上调用 list() 方法:
    List<String> attribList = userView.list();
//    一旦我们拥有了想得到相关值的特定属性名,就为该值分配一个大小合适的 ByteBuffer,然后调用视图的 read(String, ByteBuffer) 方法:
//
//    ByteBuffer attribValue = ByteBuffer.allocate(userView.size(attribName));
//    userView.read(attribName, attribValue);
//
//    attribValue 现在包含了为那一特定属性所存储的任何数据。 想设置自己的属性,只需创建 ByteBuffer 并按需填入数据,然后在视图上调用 write(String,
// ByteBuffer) 方法:
//
//    userView.write(attribName, attribValue);
  }
项目:niotest    文件:MarschallUnixTest.java   
@BeforeClass
public static void before() throws IOException {

    fsDescription = build().
            playgrounds().
                std( MemoryFileSystemBuilder.
                        newLinux().
                        addFileAttributeView( UserDefinedFileAttributeView.class ).
                                     build( "marschallu" ).
                                getPath( "play" ).
                                toAbsolutePath() ).
                noSizeLimit().
                sameProviderDifferentFileSystem(
                        MemoryFileSystemBuilder.
                        newLinux().
                        addFileAttributeView( UserDefinedFileAttributeView.class ).
                                                                 build( "marschall2" ).
                                getPath( "play" ).
                                toAbsolutePath() ).
                next().
            unix().next().
            time().noLastAccessTime().next().
            pathConstraints().
                noMaxFilenameLength().
                noMaxPathLength(). // timeout
                next().
            closable().no().
            hardlinks().no().
            symlinks().noDirs().next().
            watchable().no().
            attributes().remove( "owner", FileOwnerView.class ).next().
            bugScheme( "RelSymLink" ).
            bug( "testAppendAndReadThrows" ).
            bug( "testAppendAndTruncateExistingThrows" ).
            bug( "testGetNameOfDefaultPathIsItself" ).
            bug( "testCopyIntoItself" ).
            bug( "testCopyNonEmptyDirDoesNotCopyKids" ).
            bug( "testIsSameFileOfDifferentPathNonExistingFileIsNot" ).
            bug( "testSymLinkToUnnormalizedRelPath" ).
            bug( "testGetFileStoreOfNonExistent" ).
            bug( "testGetFileStoreOfBrokenSymLink" ).
            bug( "testTransferFromSourceWithLessThanRequestedBytesGetsWhatsThere" ).
            bug( "testTransferFromPositionBeyondFileSizeDoesNothing" ).
            bug( "testDeleteWhileReading" ).

            bug( "testDeleteWhileWriting" ).
            bug( "testDifferentOwnerCanNotWrite" ).
            bug( "testDotFilesAreHidden" ).
            bug( "testFilesHaveOwners" ).
            bug( "testOwnerByTwoMethods" ).
            bug( "testReadChannelOfDirDoesNotThrow" ).
            bug( "testEveryChannelWriteUpdatesLastModifiedTime" ).

            bug( "testCopyToDifferentFS" ).
            bug( "testMoveToDifferentFS" ).

            done();

}
项目:niotest    文件:MarschallWindowsTest.java   
@BeforeClass
public static void before() throws IOException {

    FileSystem fs = MemoryFileSystemBuilder.
            newWindows().
            addRoot( "D:\\" ).
            addFileAttributeView( UserDefinedFileAttributeView.class ).
            //addFileAttributeView( FileOwnerAttributeView.class ).
                    build( "marschallw" );
    Path std = fs.getPath( "C:\\play" );
    Path d = fs.getPath( "D:\\play" );

    fsDescription = build().
            playgrounds().
                std( std ).
                sameFileSystemDifferentStore( d ).
                noClosable().
                noSizeLimit().
                noSameProviderDifferentFileSystem().
                next().
            windows().noUNC().noRootComponents().next().
            //playground().set( std ).
            time().noLastAccessTime().next().
            pathConstraints().
                noMaxFilenameLength().
                noMaxPathLength(). // timeout
                next().
            //closable().no().
            hardlinks().no().
            symlinks().noDirs().next().
            watchable().no().
            fileStores().noLimitedPlayground().next().
            attributes().remove( "owner", FileOwnerView.class ).next().
            bugScheme( "RelSymLink" ).
            bug( "testAppendAndReadThrows" ).
            bug( "testGetNameOfDefaultPathIsItself" ).
            bug( "testCopyIntoItself" ).
            bug( "testCopyNonEmptyDirDoesNotCopyKids" ).
            bug( "testIsSameFileOfDifferentPathNonExistingFileIsNot" ).
            bug( "testSymLinkToUnnormalizedRelPath" ).
            bug( "testGetFileStoreOfNonExistent" ).
            bug( "testGetFileStoreOfBrokenSymLink" ).
            bug( "testAppendAndTruncateExistingThrows" ).
            bug( "testTransferFromSourceWithLessThanRequestedBytesGetsWhatsThere" ).
            bug( "testTransferFromPositionBeyondFileSizeDoesNothing" ).
            bug( "testEveryChannelWriteUpdatesLastModifiedTime" ).


            done();


}
项目:jimfs    文件:UserDefinedAttributeProvider.java   
@Override
public Class<UserDefinedFileAttributeView> viewType() {
  return UserDefinedFileAttributeView.class;
}
项目:jimfs    文件:UserDefinedAttributeProvider.java   
@Override
public UserDefinedFileAttributeView view(
    FileLookup lookup, ImmutableMap<String, FileAttributeView> inheritedViews) {
  return new View(lookup);
}