Example usage for java.io FileDescriptor valid

List of usage examples for java.io FileDescriptor valid

Introduction

In this page you can find the example usage for java.io FileDescriptor valid.

Prototype

public boolean valid() 

Source Link

Document

Tests if this file descriptor object is valid.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    FileOutputStream os = new FileOutputStream("outfilename");
    FileDescriptor fd = os.getFD();
    os.write(1);/* ww w  . j a  va  2 s . c  o  m*/
    os.flush();
    fd.valid();
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    FileInputStream fis = new FileInputStream("C://test.txt");

    // get file descriptor
    FileDescriptor fd = fis.getFD();

    // tests if the file is valid
    boolean bool = fd.valid();

    System.out.println("Valid file: " + bool);

}

From source file:Main.java

public static void main(String[] args) throws IOException {

    // create new file output stream
    FileOutputStream fos = new FileOutputStream("C://test.txt");

    // get file descriptor instance
    FileDescriptor fd = fos.getFD();

    // test if the file is valid
    boolean bool = fd.valid();

    // print/*  www  .  j  a v  a 2 s  . c  o m*/
    System.out.print("Is file valid? " + bool);

}

From source file:org.apache.hadoop.io.nativeio.TestNativeIO.java

@Test
public void testOpen() throws Exception {
    LOG.info("Open a missing file without O_CREAT and it should fail");
    try {//from   w  w  w .  j  a v  a 2s. com
        FileDescriptor fd = NativeIO.open(new File(TEST_DIR, "doesntexist").getAbsolutePath(),
                NativeIO.O_WRONLY, 0700);
        fail("Able to open a new file without O_CREAT");
    } catch (IOException ioe) {
        // expected
    }

    LOG.info("Test creating a file with O_CREAT");
    FileDescriptor fd = NativeIO.open(new File(TEST_DIR, "testWorkingOpen").getAbsolutePath(),
            NativeIO.O_WRONLY | NativeIO.O_CREAT, 0700);
    assertNotNull(true);
    assertTrue(fd.valid());
    FileOutputStream fos = new FileOutputStream(fd);
    fos.write("foo".getBytes());
    fos.close();

    assertFalse(fd.valid());

    LOG.info("Test exclusive create");
    try {
        fd = NativeIO.open(new File(TEST_DIR, "testWorkingOpen").getAbsolutePath(),
                NativeIO.O_WRONLY | NativeIO.O_CREAT | NativeIO.O_EXCL, 0700);
        fail("Was able to create existing file with O_EXCL");
    } catch (IOException ioe) {
        // expected
    }
}

From source file:org.apache.hadoop.io.nativeio.TestNativeIO.java

/**
 * Test that opens and closes a file 10000 times - this would crash with
 * "Too many open files" if we leaked fds using this access pattern.
 *//*from ww w  . j a  va 2 s . c o m*/
@Test
public void testFDDoesntLeak() throws IOException {
    for (int i = 0; i < 10000; i++) {
        FileDescriptor fd = NativeIO.open(new File(TEST_DIR, "testNoFdLeak").getAbsolutePath(),
                NativeIO.O_WRONLY | NativeIO.O_CREAT, 0700);
        assertNotNull(true);
        assertTrue(fd.valid());
        FileOutputStream fos = new FileOutputStream(fd);
        fos.write("foo".getBytes());
        fos.close();
    }
}