类java.nio.file.attribute.DosFileAttributes源码实例Demo

下面列出了怎么用java.nio.file.attribute.DosFileAttributes的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: zip4j   文件: FileUtils.java
private static byte[] getWindowsFileAttributes(Path file) {
  byte[] fileAttributes = new byte[4];

  try {
    DosFileAttributeView dosFileAttributeView = Files.getFileAttributeView(file, DosFileAttributeView.class,
        LinkOption.NOFOLLOW_LINKS);
    DosFileAttributes dosFileAttributes = dosFileAttributeView.readAttributes();

    byte windowsAttribute = 0;

    windowsAttribute = setBitIfApplicable(dosFileAttributes.isReadOnly(), windowsAttribute, 0);
    windowsAttribute = setBitIfApplicable(dosFileAttributes.isHidden(), windowsAttribute, 1);
    windowsAttribute = setBitIfApplicable(dosFileAttributes.isSystem(), windowsAttribute, 2);
    windowsAttribute = setBitIfApplicable(dosFileAttributes.isArchive(), windowsAttribute, 5);
    fileAttributes[0] = windowsAttribute;
  } catch (IOException e) {
    // ignore
  }

  return fileAttributes;
}
 
源代码2 项目: zip4j   文件: FileUtilsTestWindows.java
@Test
public void testGetFileAttributesReturnsAttributesAsDefined() throws IOException {
  File file = mock(File.class);
  Path path = mock(Path.class);
  when(file.toPath()).thenReturn(path);
  when(file.exists()).thenReturn(true);
  DosFileAttributes dosFileAttributes = mock(DosFileAttributes.class);
  DosFileAttributeView dosFileAttributeView = mockDosFileAttributeView(path, true, dosFileAttributes);
  when(dosFileAttributeView.readAttributes()).thenReturn(dosFileAttributes);
  when(dosFileAttributes.isReadOnly()).thenReturn(true);
  when(dosFileAttributes.isHidden()).thenReturn(true);
  when(dosFileAttributes.isSystem()).thenReturn(true);
  when(dosFileAttributes.isArchive()).thenReturn(true);

  byte[] attributes = FileUtils.getFileAttributes(file);

  assertThat(isBitSet(attributes[0], 0)).isTrue();
  assertThat(isBitSet(attributes[0], 1)).isTrue();
  assertThat(isBitSet(attributes[0], 2)).isTrue();
  assertThat(isBitSet(attributes[0], 5)).isTrue();
}
 
源代码3 项目: zip4j   文件: FileUtilsTestWindows.java
private DosFileAttributeView mockDosFileAttributeView(Path path, boolean fileExists, DosFileAttributes dosFileAttributes) throws IOException {
  FileSystemProvider fileSystemProvider = mock(FileSystemProvider.class);
  FileSystem fileSystem = mock(FileSystem.class);
  DosFileAttributeView dosFileAttributeView = mock(DosFileAttributeView.class);

  when(fileSystemProvider.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS)).thenReturn(dosFileAttributes);
  when(path.getFileSystem()).thenReturn(fileSystem);
  when(fileSystemProvider.getFileAttributeView(path, DosFileAttributeView.class, LinkOption.NOFOLLOW_LINKS))
      .thenReturn(dosFileAttributeView);
  when(path.getFileSystem().provider()).thenReturn(fileSystemProvider);

  if (!fileExists) {
    doThrow(new IOException()).when(fileSystemProvider).checkAccess(path);
  }

  return dosFileAttributeView;
}
 
源代码4 项目: jimfs   文件: JimfsFileSystemProvider.java
@Override
public boolean isHidden(Path path) throws IOException {
  // TODO(cgdecker): This should probably be configurable, but this seems fine for now
  /*
   * If the DOS view is supported, use the Windows isHidden method (check the dos:hidden
   * attribute). Otherwise, use the Unix isHidden method (just check if the file name starts with
   * ".").
   */
  JimfsPath checkedPath = checkPath(path);
  FileSystemView view = getDefaultView(checkedPath);
  if (getFileStore(path).supportsFileAttributeView("dos")) {
    return view.readAttributes(checkedPath, DosFileAttributes.class, Options.NOFOLLOW_LINKS)
        .isHidden();
  }
  return path.getNameCount() > 0 && path.getFileName().toString().startsWith(".");
}
 
源代码5 项目: jimfs   文件: DosAttributeProviderTest.java
@Test
public void testAttributes() {
  DosFileAttributes attrs = provider.readAttributes(file);
  assertThat(attrs.isHidden()).isFalse();
  assertThat(attrs.isArchive()).isFalse();
  assertThat(attrs.isReadOnly()).isFalse();
  assertThat(attrs.isSystem()).isFalse();

  file.setAttribute("dos", "hidden", true);

  attrs = provider.readAttributes(file);
  assertThat(attrs.isHidden()).isTrue();
  assertThat(attrs.isArchive()).isFalse();
  assertThat(attrs.isReadOnly()).isFalse();
  assertThat(attrs.isSystem()).isFalse();
}
 
源代码6 项目: zip4j   文件: FileUtilsTestWindows.java
@Test
public void testGetFileAttributesWhenFileDoesNotExistReturnsEmptyBytes() throws IOException {
  File file = mock(File.class);
  Path path = mock(Path.class);
  when(file.toPath()).thenReturn(path);
  when(file.exists()).thenReturn(false);
  DosFileAttributes dosFileAttributes = mock(DosFileAttributes.class);
  mockDosFileAttributeView(path, true, dosFileAttributes);

  byte[] attributes = FileUtils.getFileAttributes(file);

  assertThat(attributes).contains(0, 0, 0, 0);
}
 
源代码7 项目: cyberduck   文件: LocalAttributesFinderFeature.java
protected PathAttributes convert(final java.nio.file.Path file) throws IOException {
    final boolean isPosix = session.isPosixFilesystem();
    final PathAttributes attributes = new PathAttributes();
    final Class<? extends BasicFileAttributes> provider = isPosix ? PosixFileAttributes.class : DosFileAttributes.class;
    final BasicFileAttributes a = Files.readAttributes(file, provider, LinkOption.NOFOLLOW_LINKS);
    if(a.isRegularFile()) {
        attributes.setSize(a.size());
    }
    attributes.setModificationDate(a.lastModifiedTime().toMillis());
    attributes.setCreationDate(a.creationTime().toMillis());
    attributes.setAccessedDate(a.lastAccessTime().toMillis());
    if(isPosix) {
        attributes.setOwner(((PosixFileAttributes) a).owner().getName());
        attributes.setGroup(((PosixFileAttributes) a).group().getName());
        attributes.setPermission(new Permission(PosixFilePermissions.toString(((PosixFileAttributes) a).permissions())));
    }
    else {
        Permission.Action actions = Permission.Action.none;
        if(Files.isReadable(file)) {
            actions = actions.or(Permission.Action.read);
        }
        if(Files.isWritable(file)) {
            actions = actions.or(Permission.Action.write);
        }
        if(Files.isExecutable(file)) {
            actions = actions.or(Permission.Action.execute);
        }
        attributes.setPermission(new Permission(
            actions, Permission.Action.none, Permission.Action.none
        ));
    }
    return attributes;
}
 
源代码8 项目: ChromeForensics   文件: Utils.java
/**
 * 
 * @param fileLoc
 * @return 
 */
public static Map<String, String> getFileMetadata(Path fileLoc) {
    Map<String, String> linkedHashMap = new LinkedHashMap<>();

    try {
        DosFileAttributes dosFileAttr = Files.readAttributes(fileLoc, DosFileAttributes.class);

        linkedHashMap.put("File Name", fileLoc.getFileName().toString());
        linkedHashMap.put("File Location", fileLoc.toString());
        linkedHashMap.put("File Size", readableFileSize(dosFileAttr.size()));
        linkedHashMap.put("Creation Time", getDateTime(dosFileAttr.creationTime()));
        linkedHashMap.put("Last Accessed Time", getDateTime(dosFileAttr.lastAccessTime()));
        linkedHashMap.put("Last Modified Time", getDateTime(dosFileAttr.lastModifiedTime()));
        linkedHashMap.put("Is Directory?", dosFileAttr.isDirectory() ? "True" : "False");
        linkedHashMap.put("Is Regular File?", dosFileAttr.isRegularFile() ? "True" : "False");
        linkedHashMap.put("Is Symbolic Link?", dosFileAttr.isSymbolicLink() ? "True" : "False");
        linkedHashMap.put("Is Archive?", dosFileAttr.isArchive() ? "True" : "False");
        linkedHashMap.put("Is Hidden File?", dosFileAttr.isHidden() ? "True" : "False");
        linkedHashMap.put("Is ReadOnly?", dosFileAttr.isReadOnly() ? "True" : "False");
        linkedHashMap.put("Is System File?", dosFileAttr.isSystem() ? "True" : "False");

        if (getOsName().equals("Linux")) {
            PosixFileAttributes attr = Files.readAttributes(fileLoc, PosixFileAttributes.class);
            String posixPerm = String.format("%s %s %s%n", attr.owner().getName(), attr.group().getName(),
                    PosixFilePermissions.toString(attr.permissions()));

            linkedHashMap.put("Posix", posixPerm);
        }

    } catch (UnsupportedOperationException | IOException ex) {
        System.err.println(ex.getMessage());
    }

    return linkedHashMap;
}
 
源代码9 项目: jsr203-hadoop   文件: TestFiles.java
@Test(expected = UnsupportedOperationException.class)
public void getFileAttributeViewUnsupportedOperationException()
    throws IOException {
  Path rootPath = Paths.get(clusterUri);
  Path path = Files.createTempFile(rootPath, "test", "tmp");
  Files.readAttributes(path, DosFileAttributes.class);
}
 
源代码10 项目: jimfs   文件: DosAttributeProviderTest.java
@Test
public void testView() throws IOException {
  DosFileAttributeView view =
      provider.view(
          fileLookup(),
          ImmutableMap.<String, FileAttributeView>of(
              "basic", new BasicAttributeProvider().view(fileLookup(), NO_INHERITED_VIEWS)));
  assertNotNull(view);

  assertThat(view.name()).isEqualTo("dos");

  DosFileAttributes attrs = view.readAttributes();
  assertThat(attrs.isHidden()).isFalse();
  assertThat(attrs.isArchive()).isFalse();
  assertThat(attrs.isReadOnly()).isFalse();
  assertThat(attrs.isSystem()).isFalse();

  view.setArchive(true);
  view.setReadOnly(true);
  view.setHidden(true);
  view.setSystem(false);

  assertThat(attrs.isHidden()).isFalse();
  assertThat(attrs.isArchive()).isFalse();
  assertThat(attrs.isReadOnly()).isFalse();

  attrs = view.readAttributes();
  assertThat(attrs.isHidden()).isTrue();
  assertThat(attrs.isArchive()).isTrue();
  assertThat(attrs.isReadOnly()).isTrue();
  assertThat(attrs.isSystem()).isFalse();

  view.setTimes(FileTime.fromMillis(0L), null, null);
  assertThat(view.readAttributes().lastModifiedTime()).isEqualTo(FileTime.fromMillis(0L));
}
 
源代码11 项目: simple-nfs   文件: LocalFileSystem.java
private Stat statPath(Path p, long inodeNumber) throws IOException {

        Class<? extends  BasicFileAttributeView> attributeClass =
                IS_UNIX ? PosixFileAttributeView.class : DosFileAttributeView.class;

        BasicFileAttributes attrs = Files.getFileAttributeView(p, attributeClass, NOFOLLOW_LINKS).readAttributes();

        Stat stat = new Stat();

        stat.setATime(attrs.lastAccessTime().toMillis());
        stat.setCTime(attrs.creationTime().toMillis());
        stat.setMTime(attrs.lastModifiedTime().toMillis());

        if (IS_UNIX) {
            stat.setGid((Integer) Files.getAttribute(p, "unix:gid", NOFOLLOW_LINKS));
            stat.setUid((Integer) Files.getAttribute(p, "unix:uid", NOFOLLOW_LINKS));
            stat.setMode((Integer) Files.getAttribute(p, "unix:mode", NOFOLLOW_LINKS));
            stat.setNlink((Integer) Files.getAttribute(p, "unix:nlink", NOFOLLOW_LINKS));
        } else {
            DosFileAttributes dosAttrs = (DosFileAttributes)attrs;
            stat.setGid(0);
            stat.setUid(0);
            int type = dosAttrs.isSymbolicLink() ? Stat.S_IFLNK : dosAttrs.isDirectory() ? Stat.S_IFDIR : Stat.S_IFREG;
            stat.setMode( type |(dosAttrs.isReadOnly()? 0400 : 0600));
            stat.setNlink(1);
        }

        stat.setDev(17);
        stat.setIno((int) inodeNumber);
        stat.setRdev(17);
        stat.setSize(attrs.size());
        stat.setFileid((int) inodeNumber);
        stat.setGeneration(attrs.lastModifiedTime().toMillis());

        return stat;
    }
 
源代码12 项目: ats-framework   文件: LocalFileSystemOperations.java
@Override
public void setFileHiddenAttribute(
        String sourceFile,
        boolean hidden ) {

    sourceFile = IoUtils.normalizeFilePath(sourceFile, osType);
    checkFileExistence(new File(sourceFile));

    final String errMsg = "Could not " + (hidden
                                          ? "set"
                                          : "unset")
                          + " the hidden attribute of file '" + sourceFile + "'";
    if (OperatingSystemType.getCurrentOsType().isWindows()) {
        try {
            Path path = Paths.get(sourceFile);
            DosFileAttributes attr;
            attr = Files.readAttributes(path, DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS);

            boolean goHidden = attr.isHidden();
            if (!hidden && goHidden) {
                Files.setAttribute(path, "dos:hidden", false, LinkOption.NOFOLLOW_LINKS);
            } else if (hidden && !goHidden) {
                Files.setAttribute(path, "dos:hidden", true, LinkOption.NOFOLLOW_LINKS);
            }
        } catch (IOException e) {
            throw new FileSystemOperationException(errMsg, e);
        }
    } else if (OperatingSystemType.getCurrentOsType().isUnix()) {
        // a '.' prefix makes the file hidden
        String filePath = IoUtils.getFilePath(sourceFile);
        String fileName = IoUtils.getFileName(sourceFile);
        if (hidden) {
            if (fileName.startsWith(".")) {
                log.warn("File '" + sourceFile + "' is already hidden. No changes are made!");
                return;
            } else {
                fileName = "." + fileName;
            }
        } else {
            if (!fileName.startsWith(".")) {
                log.warn("File '" + sourceFile + "' is already NOT hidden. No changes are made!");
                return;
            } else {
                fileName = fileName.substring(1);
            }
        }
        renameFile(sourceFile, filePath + fileName, false);
    } else {
        throw new FileSystemOperationException(errMsg + ": Unknown OS type");
    }
}
 
源代码13 项目: bazel   文件: WindowsFileSystem.java
@Override
protected FileStatus stat(Path path, boolean followSymlinks) throws IOException {
  File file = getIoFile(path);
  final DosFileAttributes attributes;
  try {
    attributes = getAttribs(file, followSymlinks);
  } catch (IOException e) {
    throw new FileNotFoundException(path + ERR_NO_SUCH_FILE_OR_DIR);
  }

  final boolean isSymbolicLink = !followSymlinks && fileIsSymbolicLink(file);
  FileStatus status =
      new FileStatus() {
        @Override
        public boolean isFile() {
          return !isSymbolicLink && (attributes.isRegularFile() || isSpecialFile());
        }

        @Override
        public boolean isSpecialFile() {
          // attributes.isOther() returns false for symlinks but returns true for junctions.
          // Bazel treats junctions like symlinks. So let's return false here for junctions.
          // This fixes https://github.com/bazelbuild/bazel/issues/9176
          return !isSymbolicLink && attributes.isOther();
        }

        @Override
        public boolean isDirectory() {
          return !isSymbolicLink && attributes.isDirectory();
        }

        @Override
        public boolean isSymbolicLink() {
          return isSymbolicLink;
        }

        @Override
        public long getSize() throws IOException {
          return attributes.size();
        }

        @Override
        public long getLastModifiedTime() throws IOException {
          return attributes.lastModifiedTime().toMillis();
        }

        @Override
        public long getLastChangeTime() {
          // This is the best we can do with Java NIO...
          return attributes.lastModifiedTime().toMillis();
        }

        @Override
        public long getNodeId() {
          // TODO(bazel-team): Consider making use of attributes.fileKey().
          return -1;
        }
      };

  return status;
}
 
源代码14 项目: bazel   文件: WindowsFileSystem.java
private static DosFileAttributes getAttribs(File file, boolean followSymlinks)
    throws IOException {
  return Files.readAttributes(
      file.toPath(), DosFileAttributes.class, symlinkOpts(followSymlinks));
}
 
源代码15 项目: jimfs   文件: DosAttributeProvider.java
@Override
public Class<DosFileAttributes> attributesType() {
  return DosFileAttributes.class;
}
 
源代码16 项目: jimfs   文件: DosAttributeProvider.java
@Override
public DosFileAttributes readAttributes(File file) {
  return new Attributes(file);
}
 
源代码17 项目: jimfs   文件: DosAttributeProvider.java
@Override
public DosFileAttributes readAttributes() throws IOException {
  return new Attributes(lookupFile());
}
 
 类所在包
 类方法
 同包方法