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 () ); }
public static void main(String args[]) throws Exception { Path target = Paths.get("C:\\mytemp\\Something.txt"); Files.createFile(target); UserDefinedFileAttributeView view = Files.getFileAttributeView(target, UserDefinedFileAttributeView.class); view.write("user.mimetype", Charset.defaultCharset().encode("text/html")); String name = "user.mimetype"; ByteBuffer buf = ByteBuffer.allocate(view.size(name)); view.read(name, buf); buf.flip(); String value = Charset.defaultCharset().decode(buf).toString(); System.out.println("value="+value);
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 ); } }
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(); } }
public static void main(String[] args) { try{ Path file = Paths.get("C:\\Users\\rohit\\Desktop\\imp docs\\Steps.txt"); UserDefinedFileAttributeView userView = Files.getFileAttributeView(file, UserDefinedFileAttributeView.class); String attribName = "RESULT"; String attribValue = "SUCCESS"; userView.write(attribName, Charset.defaultCharset().encode(attribValue)); List<String> attribList = userView.list(); for (String s : attribList) { ByteBuffer buf = ByteBuffer.allocate(userView.size(s)); userView.read(s, buf); buf.flip(); String value = Charset.defaultCharset().decode(buf).toString(); if("SUCCESS".equals(value)){ System.out.print(String.format("User defined attribute: %s", s)); System.out.println(String.format("; value: %s", value)); } } } catch(Exception e){ }
/** Write an filesystem attribute, if its value is non-null. */ private static void writeStringAttributeIfPresent(UserDefinedFileAttributeView view, String name, String value) throws IOException { if (value != null) { view.write(name, ByteBuffer.wrap(value.getBytes(StandardCharsets.UTF_8))); } }
Path file = Paths.get("filename.ext"); // check that user defined attributes are supported by the file system FileStore store = file.getFileStore(); if (!store.supportsFileAttributeView("xattr")) { System.err.format("UserDefinedFileAttributeView not supported on %s\n", store); System.exit(-1); } UserDefinedFileAttributeView view = file.getFileAttributeView(UserDefinedFileAttributeView.class); // list user defined attributes if (args.length == 1) { System.out.println(" Size Name"); System.out.println("-------- --------------------------------------"); for (String name: view.list()) { System.out.format("%8d %s\n", view.size(name), name); } }
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()); }
final String name = "user.hash."+hashAlgo; final ByteBuffer bb = ByteBuffer.allocate(64); try { view.read(name, bb); return ((ByteBuffer)bb.flip()).array(); } catch(final NoSuchFileException t) { // Not yet calculated } catch(final Throwable t) { t.printStackTrace(); } System.out.println("Hash not found calculation"); final byte[] hash = getFileHash(src, hashAlgo); view.write(name, ByteBuffer.wrap(hash)); return hash;
@Override public void removeBlob(final String container, final String blobKey) { filesystemContainerNameValidator.validate(container); filesystemBlobKeyValidator.validate(blobKey); String fileName = buildPathStartingFromBaseDir(container, blobKey); logger.debug("Deleting blob %s", fileName); File fileToBeDeleted = new File(fileName); if (fileToBeDeleted.isDirectory()) { try { UserDefinedFileAttributeView view = getUserDefinedFileAttributeView(fileToBeDeleted.toPath()); if (view != null) { for (String s : view.list()) { view.delete(s); } } } catch (IOException e) { logger.debug("Could not delete attributes from %s: %s", fileToBeDeleted, e); } } try { delete(fileToBeDeleted); } catch (IOException e) { logger.debug("Could not delete %s: %s", fileToBeDeleted, e); } // now examine if the key of the blob is a complex key (with a directory structure) // and eventually remove empty directory removeDirectoriesTreeOfBlobKey(container, blobKey); }
/** Read the String representation of filesystem attribute, or return null if not present. */ private static String readStringAttributeIfPresent(UserDefinedFileAttributeView view, Set<String> attributes, String name) throws IOException { if (!attributes.contains(name)) { return null; } ByteBuffer buf = ByteBuffer.allocate(view.size(name)); view.read(name, buf); return new String(buf.array(), StandardCharsets.UTF_8); }
Path file = ...; UserDefinedFileAttributeView view = Files .getFileAttributeView(file, UserDefinedFileAttributeView.class); view.write("user.mimetype", Charset.defaultCharset().encode("text/html");
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.attribute.UserDefinedFileAttributeView; import java.util.List; public class ReadData { public static void main(String[] args) throws IOException { Path path = Paths.get("someImage.jpg").toAbsolutePath(); UserDefinedFileAttributeView fileAttributeView = Files.getFileAttributeView(path, UserDefinedFileAttributeView.class); List<String> allAttrs = fileAttributeView.list(); for (String att : allAttrs) { System.out.println("att = " + att); } } }
@Override public void removeBlob(final String container, final String blobKey) { filesystemContainerNameValidator.validate(container); filesystemBlobKeyValidator.validate(blobKey); String fileName = buildPathStartingFromBaseDir(container, blobKey); logger.debug("Deleting blob %s", fileName); File fileToBeDeleted = new File(fileName); if (fileToBeDeleted.isDirectory()) { try { UserDefinedFileAttributeView view = getUserDefinedFileAttributeView(fileToBeDeleted.toPath()); if (view != null) { for (String s : view.list()) { view.delete(s); } } } catch (IOException e) { logger.debug("Could not delete attributes from %s: %s", fileToBeDeleted, e); } } try { delete(fileToBeDeleted); } catch (IOException e) { logger.debug("Could not delete %s: %s", fileToBeDeleted, e); } // now examine if the key of the blob is a complex key (with a directory structure) // and eventually remove empty directory removeDirectoriesTreeOfBlobKey(container, blobKey); }
for (String each : sourceAttributes.list()) { int size = sourceAttributes.size(each); if (buffer == null) { buffer = ByteBuffer.allocate(size); int read = sourceAttributes.read(each, buffer); if (read != size) { throw new FileSystemException(source.toString(), null,"could not read attribute: " + each); int written = targetAttributes.write(each, buffer); if (written != size) { throw new FileSystemException(target.toString(), null, "could not read attribute: " + each);
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 () ); }
/** Read the String representation of filesystem attribute, or return null if not present. */ private static String readStringAttributeIfPresent(UserDefinedFileAttributeView view, Set<String> attributes, String name) throws IOException { if (!attributes.contains(name)) { return null; } ByteBuffer buf = ByteBuffer.allocate(view.size(name)); view.read(name, buf); return new String(buf.array(), StandardCharsets.UTF_8); }
public static void main(String args[]) throws Exception { Path target = Paths.get("C:\\mytemp\\SomethingDifferent.txt"); Files.createFile(target); UserDefinedFileAttributeView view = Files.getFileAttributeView(target, UserDefinedFileAttributeView.class); view.write("user.mimetype", Charset.defaultCharset().encode("text/html")); String name = "user.mimetype"; 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); }
/** Write an filesystem attribute, if its value is non-null. */ private static void writeStringAttributeIfPresent(UserDefinedFileAttributeView view, String name, String value) throws IOException { if (value != null) { view.write(name, ByteBuffer.wrap(value.getBytes(StandardCharsets.UTF_8))); } }