001package org.unix4j.unix.find; 002 003import java.io.File; 004import java.io.IOException; 005import java.nio.file.Files; 006import java.nio.file.attribute.BasicFileAttributes; 007 008/** 009 * Helper to access file attributes if compiled and run with Java 7 or newer. 010 */ 011/* NOTE: must be public for reflection */ 012public class FileAttributes7 extends FileAttributes { 013 014 /** 015 * Done use this constructor, use {@link FileAttributes#INSTANCE} instead. 016 */ 017 //must be public for reflection 018 public FileAttributes7() { 019 super(); 020 } 021 022 private BasicFileAttributes getBasicFileAttributes(File file) { 023 try { 024 return Files.readAttributes(file.toPath(), BasicFileAttributes.class); 025 } catch (IOException e) { 026 return null; 027 } 028 } 029 030 @Override 031 public long getLastModifiedTime(File file) { 032 final BasicFileAttributes atts = getBasicFileAttributes(file); 033 return atts != null ? atts.lastModifiedTime().toMillis() : super.getLastModifiedTime(file); 034 } 035 @Override 036 public long getCreationTime(File file) { 037 final BasicFileAttributes atts = getBasicFileAttributes(file); 038 return atts != null ? atts.creationTime().toMillis() : super.getCreationTime(file); 039 } 040 @Override 041 public long getLastAccessedTime(File file) { 042 final BasicFileAttributes atts = getBasicFileAttributes(file); 043 return atts != null ? atts.lastAccessTime().toMillis() : super.getLastAccessedTime(file); 044 } 045 046 @Override 047 public boolean isDirectory(File file) { 048 final BasicFileAttributes atts = getBasicFileAttributes(file); 049 return atts != null ? atts.isDirectory() : super.isDirectory(file); 050 } 051 052 @Override 053 public boolean isRegularFile(File file) { 054 final BasicFileAttributes atts = getBasicFileAttributes(file); 055 return atts != null ? atts.isRegularFile() : super.isRegularFile(file); 056 } 057 058 @Override 059 public boolean isSymbolicLink(File file) { 060 final BasicFileAttributes atts = getBasicFileAttributes(file); 061 return atts != null ? atts.isSymbolicLink() : super.isSymbolicLink(file); 062 } 063 064 @Override 065 public boolean isOther(File file) { 066 final BasicFileAttributes atts = getBasicFileAttributes(file); 067 return atts != null ? atts.isOther() : super.isOther(file); 068 } 069}