### Question:
NativeIO { public static native Stat fstat(FileDescriptor fd) throws IOException;  static boolean isAvailable(); static native FileDescriptor open(String path, int flags, int mode); static native Stat fstat(FileDescriptor fd); static native void chmod(String path, int mode); static final int O_RDONLY; static final int O_WRONLY; static final int O_RDWR; static final int O_CREAT; static final int O_EXCL; static final int O_NOCTTY; static final int O_TRUNC; static final int O_APPEND; static final int O_NONBLOCK; static final int O_SYNC; static final int O_ASYNC; static final int O_FSYNC; static final int O_NDELAY; }### Answer:
@Test public void testFstat() throws Exception { FileOutputStream fos = new FileOutputStream( new File(TEST_DIR, "testfstat")); NativeIO.Stat stat = NativeIO.fstat(fos.getFD()); fos.close(); LOG.info("Stat: " + String.valueOf(stat)); assertEquals(System.getProperty("user.name"), stat.getOwner()); assertNotNull(stat.getGroup()); assertTrue(!"".equals(stat.getGroup())); assertEquals("Stat mode field should indicate a regular file", NativeIO.Stat.S_IFREG, stat.getMode() & NativeIO.Stat.S_IFMT); }
@Test public void testMultiThreadedFstat() throws Exception { final FileOutputStream fos = new FileOutputStream( new File(TEST_DIR, "testfstat")); final AtomicReference<Throwable> thrown = new AtomicReference<Throwable>(); List<Thread> statters = new ArrayList<Thread>(); for (int i = 0; i < 10; i++) { Thread statter = new Thread() { public void run() { long et = System.currentTimeMillis() + 5000; while (System.currentTimeMillis() < et) { try { NativeIO.Stat stat = NativeIO.fstat(fos.getFD()); assertEquals(System.getProperty("user.name"), stat.getOwner()); assertNotNull(stat.getGroup()); assertTrue(!"".equals(stat.getGroup())); assertEquals("Stat mode field should indicate a regular file", NativeIO.Stat.S_IFREG, stat.getMode() & NativeIO.Stat.S_IFMT); } catch (Throwable t) { thrown.set(t); } } } }; statters.add(statter); statter.start(); } for (Thread t : statters) { t.join(); } fos.close(); if (thrown.get() != null) { throw new RuntimeException(thrown.get()); } }
@Test public void testFstatClosedFd() throws Exception { FileOutputStream fos = new FileOutputStream( new File(TEST_DIR, "testfstat2")); fos.close(); try { NativeIO.Stat stat = NativeIO.fstat(fos.getFD()); } catch (NativeIOException nioe) { LOG.info("Got expected exception", nioe); assertEquals(Errno.EBADF, nioe.getErrno()); } }