Search in sources :

Example 1 with UserDefinedFileAttributeView

use of java.nio.file.attribute.UserDefinedFileAttributeView in project uavstack by uavorg.

the class ReliableTaildirEventReader method getInode.

private long getInode(File file) throws IOException {
    UserDefinedFileAttributeView view = null;
    // windows system and file customer Attribute
    if (TaildirSourceConfigurationConstants.OS_WINDOWS.equals(os)) {
        // 把文件的内容属性值放置在view里面?
        view = Files.getFileAttributeView(file.toPath(), UserDefinedFileAttributeView.class);
        try {
            // view.size得到inode属性值大小
            ByteBuffer buffer = ByteBuffer.allocate(view.size(inode));
            // 把属性值放置在buffer中
            view.read(inode, buffer);
            buffer.flip();
            // 返回编码后的inode的属性值
            return Long.parseLong(Charset.defaultCharset().decode(buffer).toString());
        } catch (NoSuchFileException e) {
            long winode = random.nextLong();
            view.write(inode, Charset.defaultCharset().encode(String.valueOf(winode)));
            return winode;
        }
    }
    // 返回unix的inode的属性值
    long inode = (long) Files.getAttribute(file.toPath(), "unix:ino");
    return inode;
}
Also used : NoSuchFileException(java.nio.file.NoSuchFileException) UserDefinedFileAttributeView(java.nio.file.attribute.UserDefinedFileAttributeView) ByteBuffer(java.nio.ByteBuffer)

Example 2 with UserDefinedFileAttributeView

use of java.nio.file.attribute.UserDefinedFileAttributeView in project uavstack by uavorg.

the class DoTestRuleFilterFactory method setCustomerAttr.

// suppose fix bug
@Test
public void setCustomerAttr() throws IOException {
    Path target = Paths.get("/Users/fathead/temp/file4");
    UserDefinedFileAttributeView view = Files.getFileAttributeView(target, UserDefinedFileAttributeView.class);
    view.write(name, Charset.defaultCharset().encode("pinelet"));
}
Also used : Path(java.nio.file.Path) UserDefinedFileAttributeView(java.nio.file.attribute.UserDefinedFileAttributeView) Test(org.junit.Test)

Example 3 with UserDefinedFileAttributeView

use of java.nio.file.attribute.UserDefinedFileAttributeView in project jimfs by google.

the class UserDefinedAttributeProviderTest method testView.

@Test
public void testView() throws IOException {
    UserDefinedFileAttributeView view = provider.view(fileLookup(), NO_INHERITED_VIEWS);
    assertNotNull(view);
    assertThat(view.name()).isEqualTo("user");
    assertThat(view.list()).isEmpty();
    byte[] b1 = { 0, 1, 2 };
    byte[] b2 = { 0, 1, 2, 3, 4 };
    view.write("b1", ByteBuffer.wrap(b1));
    view.write("b2", ByteBuffer.wrap(b2));
    assertThat(view.list()).containsAtLeast("b1", "b2");
    assertThat(file.getAttributeKeys()).containsExactly("user:b1", "user:b2");
    assertThat(view.size("b1")).isEqualTo(3);
    assertThat(view.size("b2")).isEqualTo(5);
    ByteBuffer buf1 = ByteBuffer.allocate(view.size("b1"));
    ByteBuffer buf2 = ByteBuffer.allocate(view.size("b2"));
    view.read("b1", buf1);
    view.read("b2", buf2);
    assertThat(Arrays.equals(b1, buf1.array())).isTrue();
    assertThat(Arrays.equals(b2, buf2.array())).isTrue();
    view.delete("b2");
    assertThat(view.list()).containsExactly("b1");
    assertThat(file.getAttributeKeys()).containsExactly("user:b1");
    try {
        view.size("b2");
        fail();
    } catch (IllegalArgumentException expected) {
        assertThat(expected.getMessage()).contains("not set");
    }
    try {
        view.read("b2", ByteBuffer.allocate(10));
        fail();
    } catch (IllegalArgumentException expected) {
        assertThat(expected.getMessage()).contains("not set");
    }
    view.write("b1", ByteBuffer.wrap(b2));
    assertThat(view.size("b1")).isEqualTo(5);
    // succeeds
    view.delete("b2");
}
Also used : UserDefinedFileAttributeView(java.nio.file.attribute.UserDefinedFileAttributeView) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 4 with UserDefinedFileAttributeView

use of java.nio.file.attribute.UserDefinedFileAttributeView in project uavstack by uavorg.

the class ReliableTaildirEventReader method getInode.

private long getInode(File file) throws IOException {
    UserDefinedFileAttributeView view = null;
    // windows system and file customer Attribute
    if (OS_WINDOWS.equals(os)) {
        // 把文件的内容属性值放置在view里面?
        view = Files.getFileAttributeView(file.toPath(), UserDefinedFileAttributeView.class);
        try {
            // view.size得到inode属性值大小
            ByteBuffer buffer = ByteBuffer.allocate(view.size(INODE));
            // 把属性值放置在buffer中
            view.read(INODE, buffer);
            buffer.flip();
            // 返回编码后的inode的属性值
            return Long.parseLong(Charset.defaultCharset().decode(buffer).toString());
        } catch (NoSuchFileException e) {
            long winode = random.nextLong();
            view.write(INODE, Charset.defaultCharset().encode(String.valueOf(winode)));
            return winode;
        }
    }
    // 返回unix的inode的属性值
    long inode = (long) Files.getAttribute(file.toPath(), "unix:ino");
    return inode;
}
Also used : NoSuchFileException(java.nio.file.NoSuchFileException) UserDefinedFileAttributeView(java.nio.file.attribute.UserDefinedFileAttributeView) ByteBuffer(java.nio.ByteBuffer)

Example 5 with UserDefinedFileAttributeView

use of java.nio.file.attribute.UserDefinedFileAttributeView in project uavstack by uavorg.

the class DoTestRuleFilterFactory method getCustomerAttr.

// suppose fix bug
@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);
}
Also used : Path(java.nio.file.Path) UserDefinedFileAttributeView(java.nio.file.attribute.UserDefinedFileAttributeView) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

UserDefinedFileAttributeView (java.nio.file.attribute.UserDefinedFileAttributeView)6 ByteBuffer (java.nio.ByteBuffer)4 NoSuchFileException (java.nio.file.NoSuchFileException)3 Test (org.junit.Test)3 Path (java.nio.file.Path)2 RetryException (com.github.rholder.retry.RetryException)1 ProcessFailedException (com.hubspot.singularity.runner.base.shared.ProcessFailedException)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1