Java Code Examples for java.nio.file.attribute.UserDefinedFileAttributeView#read()

The following examples show how to use java.nio.file.attribute.UserDefinedFileAttributeView#read() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReliableTaildirEventReader.java    From uavstack with Apache License 2.0 6 votes vote down vote up
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;
    }
 
Example 2
Source File: SingularityUploader.java    From Singularity with Apache License 2.0 6 votes vote down vote up
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.empty();
      }
      return Optional.of(Long.parseLong(value));
    } catch (Exception e) {
      LOG.error("Error getting extra file metadata for {}", file, e);
      return Optional.empty();
    }
  } else {
    return Optional.empty();
  }
}
 
Example 3
Source File: FileSystemProcessInstances.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public String getMetadata(Path file, String key) {
    
    if (supportsUserDefinedAttributes(file)) {
        UserDefinedFileAttributeView view = Files.getFileAttributeView(file, UserDefinedFileAttributeView.class);
        try {
            ByteBuffer bb = ByteBuffer.allocate(view.size(key));
            view.read(key, bb);
            bb.flip();
            return Charset.defaultCharset().decode(bb).toString();
        } catch (IOException e) {
            return null;
        }
    }
    return null;
}
 
Example 4
Source File: FileAttributes.java    From yfs with Apache License 2.0 5 votes vote down vote up
private static String getAttrValue(UserDefinedFileAttributeView view, String name) throws IOException {
    int attrSize = view.size(name);
    ByteBuffer buffer = ByteBuffer.allocateDirect(attrSize);
    view.read(name, buffer);
    buffer.flip();
    return Charset.defaultCharset().decode(buffer).toString();
}
 
Example 5
Source File: FileObjTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example 6
Source File: Executables.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
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 () );
}
 
Example 7
Source File: DoTestRuleFilterFactory.java    From uavstack with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 8
Source File: UserExtendedAttributes.java    From yfs with Apache License 2.0 3 votes vote down vote up
/**
 * @param view the attribute view of the extendet attribute for a specific
 * file
 * @param name the name of the attribute for which the value should be
 * returned
 * @return the value of an user extended attribute from the given attribute
 * view
 */
private static String getAttrValue(UserDefinedFileAttributeView view, String name) throws IOException {
    int attrSize = view.size(name);
    ByteBuffer buffer = ByteBuffer.allocateDirect(attrSize);
    view.read(name, buffer);
    buffer.flip();
    return Charset.defaultCharset().decode(buffer).toString();
}