use of java.nio.file.attribute.BasicFileAttributeView in project packr by libgdx.
the class ArchiveUtils method setLastModifiedTime.
private static void setLastModifiedTime(Path path, FileTime lastModifiedTime) throws IOException {
if (Files.isSymbolicLink(path)) {
return;
}
BasicFileAttributeView pathAttributeView = Files.getFileAttributeView(path, BasicFileAttributeView.class, NOFOLLOW_LINKS);
final BasicFileAttributes fileAttributes = pathAttributeView.readAttributes();
pathAttributeView.setTimes(lastModifiedTime, fileAttributes.lastAccessTime(), fileAttributes.creationTime());
}
use of java.nio.file.attribute.BasicFileAttributeView in project sakuli by ConSol.
the class LogCleanUpServiceImplTest method createFileWithDate.
private Path createFileWithDate(Path path, FileTime todayMinus15) throws IOException {
Path file = Files.createFile(path);
BasicFileAttributeView fileAttributeView = Files.getFileAttributeView(file, BasicFileAttributeView.class);
fileAttributeView.setTimes(todayMinus15, todayMinus15, todayMinus15);
return file;
}
use of java.nio.file.attribute.BasicFileAttributeView in project mycore by MyCoRe-Org.
the class MCRPathXML method getDirectoryXML.
/**
* Sends the contents of an MCRDirectory as XML data to the client
*/
public static Document getDirectoryXML(MCRPath path, BasicFileAttributes attr) throws IOException {
LOGGER.debug("MCRDirectoryXML: start listing of directory {}", path);
Element root = new Element("mcr_directory");
Document doc = new org.jdom2.Document(root);
addString(root, "uri", path.toUri().toString(), false);
addString(root, "ownerID", path.getOwner(), false);
MCRPath relativePath = path.getRoot().relativize(path);
boolean isRoot = relativePath.toString().isEmpty();
addString(root, "name", (isRoot ? "" : relativePath.getFileName().toString()), true);
addString(root, "path", toStringValue(relativePath), true);
if (!isRoot) {
addString(root, "parentPath", toStringValue(relativePath.getParent()), true);
}
addBasicAttributes(root, attr, path);
Element numChildren = new Element("numChildren");
Element here = new Element("here");
root.addContent(numChildren);
numChildren.addContent(here);
Element nodes = new Element("children");
root.addContent(nodes);
SortedMap<MCRPath, MCRFileAttributes<?>> directories = new TreeMap<>();
SortedMap<MCRPath, MCRFileAttributes<?>> files = new TreeMap<>();
try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(path)) {
LOGGER.debug(() -> "Opened DirectoryStream for " + path);
Function<Path, MCRFileAttributes<?>> attrResolver = p -> {
try {
return Files.readAttributes(p, MCRFileAttributes.class);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
};
if (dirStream instanceof SecureDirectoryStream) {
// fast path
LOGGER.debug(() -> "Using SecureDirectoryStream code path for " + path);
attrResolver = p -> {
try {
BasicFileAttributeView attributeView = ((SecureDirectoryStream<Path>) dirStream).getFileAttributeView(p.getFileName(), BasicFileAttributeView.class);
return (MCRFileAttributes<?>) attributeView.readAttributes();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
};
}
for (Path child : dirStream) {
MCRFileAttributes<?> childAttrs;
try {
childAttrs = attrResolver.apply(child);
} catch (UncheckedIOException e) {
throw e.getCause();
}
if (childAttrs.isDirectory()) {
directories.put(MCRPath.toMCRPath(child), childAttrs);
} else {
files.put(MCRPath.toMCRPath(child), childAttrs);
}
}
}
// store current directory statistics
addString(here, "directories", Integer.toString(directories.size()), false);
addString(here, "files", Integer.toString(files.size()), false);
for (Map.Entry<MCRPath, MCRFileAttributes<?>> dirEntry : directories.entrySet()) {
Element child = new Element("child");
child.setAttribute("type", "directory");
addString(child, "name", dirEntry.getKey().getFileName().toString(), true);
addString(child, "uri", dirEntry.getKey().toUri().toString(), false);
nodes.addContent(child);
addBasicAttributes(child, dirEntry.getValue(), dirEntry.getKey());
}
for (Map.Entry<MCRPath, MCRFileAttributes<?>> fileEntry : files.entrySet()) {
Element child = new Element("child");
child.setAttribute("type", "file");
addString(child, "name", fileEntry.getKey().getFileName().toString(), true);
addString(child, "uri", fileEntry.getKey().toUri().toString(), false);
nodes.addContent(child);
addAttributes(child, fileEntry.getValue(), fileEntry.getKey());
}
LOGGER.debug("MCRDirectoryXML: end listing of directory {}", path);
return doc;
}
use of java.nio.file.attribute.BasicFileAttributeView in project mycore by MyCoRe-Org.
the class MCRFileSystemProvider method copy.
/* (non-Javadoc)
* @see java.nio.file.spi.FileSystemProvider#copy(java.nio.file.Path, java.nio.file.Path, java.nio.file.CopyOption[])
*/
@Override
public void copy(Path source, Path target, CopyOption... options) throws IOException {
if (isSameFile(source, target)) {
// that was easy
return;
}
checkCopyOptions(options);
HashSet<CopyOption> copyOptions = Sets.newHashSet(options);
boolean createNew = !copyOptions.contains(StandardCopyOption.REPLACE_EXISTING);
MCRPath src = MCRFileSystemUtils.checkPathAbsolute(source);
MCRPath tgt = MCRFileSystemUtils.checkPathAbsolute(target);
MCRFilesystemNode srcNode = resolvePath(src);
// checkParent of target;
if (tgt.getNameCount() == 0 && srcNode instanceof MCRDirectory) {
MCRDirectory tgtDir = MCRDirectory.getRootDirectory(tgt.getOwner());
if (tgtDir != null) {
if (tgtDir.hasChildren() && copyOptions.contains(StandardCopyOption.REPLACE_EXISTING)) {
throw new DirectoryNotEmptyException(tgt.toString());
}
} else {
// TODO: handle StandardCopyOption.COPY_ATTRIBUTES
tgtDir = new MCRDirectory(tgt.getOwner());
}
// created new root component
return;
}
MCRDirectory tgtParentDir = (MCRDirectory) resolvePath(tgt.getParent());
if (srcNode instanceof MCRFile) {
MCRFile srcFile = (MCRFile) srcNode;
MCRFile targetFile = MCRFileSystemUtils.getMCRFile(tgt, true, createNew);
targetFile.setContentFrom(srcFile.getContentAsInputStream());
if (copyOptions.contains(StandardCopyOption.COPY_ATTRIBUTES)) {
@SuppressWarnings("unchecked") MCRMD5AttributeView<String> srcAttrView = Files.getFileAttributeView(src, MCRMD5AttributeView.class, (LinkOption[]) null);
File targetLocalFile = targetFile.getLocalFile();
BasicFileAttributeView targetBasicFileAttributeView = Files.getFileAttributeView(targetLocalFile.toPath(), BasicFileAttributeView.class);
MCRFileAttributes<String> srcAttr = srcAttrView.readAllAttributes();
targetFile.adjustMetadata(srcAttr.lastModifiedTime(), srcFile.getMD5(), srcFile.getSize());
targetBasicFileAttributeView.setTimes(srcAttr.lastModifiedTime(), srcAttr.lastAccessTime(), srcAttr.creationTime());
}
} else if (srcNode instanceof MCRDirectory) {
MCRFilesystemNode child = tgtParentDir.getChild(tgt.getFileName().toString());
if (child != null) {
if (!copyOptions.contains(StandardCopyOption.REPLACE_EXISTING)) {
throw new FileAlreadyExistsException(tgtParentDir.toString(), tgt.getFileName().toString(), null);
}
if (child instanceof MCRFile) {
throw new NotDirectoryException(tgt.toString());
}
MCRDirectory tgtDir = (MCRDirectory) child;
if (tgtDir.hasChildren() && copyOptions.contains(StandardCopyOption.REPLACE_EXISTING)) {
throw new DirectoryNotEmptyException(tgt.toString());
}
// TODO: handle StandardCopyOption.COPY_ATTRIBUTES
} else {
// simply create directory
@SuppressWarnings("unused") MCRDirectory tgtDir = new MCRDirectory(tgt.getFileName().toString(), tgtParentDir);
// TODO: handle StandardCopyOption.COPY_ATTRIBUTES
}
}
}
use of java.nio.file.attribute.BasicFileAttributeView in project Malai by arnobl.
the class ShowFileInformation method doActionBody.
// The operation that executes the action.
@Override
protected void doActionBody() {
if (path == null)
textArea.setText("");
else {
// Showing some properties of the selected file.
final BasicFileAttributeView attrV = Files.getFileAttributeView(path, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
String txt = path.toString();
if (attrV != null) {
try {
BasicFileAttributes attr = attrV.readAttributes();
txt += "\n" + attr.size() + " bytes";
} catch (IOException e) {
e.printStackTrace();
}
}
textArea.setText(txt);
}
}
Aggregations