Java Code Examples for java.nio.file.DirectoryStream#close()

The following examples show how to use java.nio.file.DirectoryStream#close() . 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: BundleFileSystemProvider.java    From incubator-taverna-language with Apache License 2.0 6 votes vote down vote up
@Override
public DirectoryStream<Path> newDirectoryStream(Path dir,
		final Filter<? super Path> filter) throws IOException {
	final BundleFileSystem fs = (BundleFileSystem) dir.getFileSystem();
	final DirectoryStream<Path> stream = origProvider(dir)
			.newDirectoryStream(fs.unwrap(dir), new Filter<Path>() {
				@Override
				public boolean accept(Path entry) throws IOException {
					return filter.accept(fs.wrap(entry));
				}
			});
	return new DirectoryStream<Path>() {
		@Override
		public void close() throws IOException {
			stream.close();
		}

		@Override
		public Iterator<Path> iterator() {
			return fs.wrapIterator(stream.iterator());
		}
	};
}
 
Example 2
Source File: PathDirectoryStreamTest.java    From openjdk-systemtest with Apache License 2.0 6 votes vote down vote up
public void testDirectoryStreamFilterAllItems() throws IOException {		
	final int EXPECTED_NUMBER_OF_FILES = 10;
	Path newDirectory = DIRECTORY_STREAM_FILE_DIR;		
	List<String> dirGlob = new LinkedList<String>();		
	DirectoryStream<Path> directoryContents = Files.newDirectoryStream(newDirectory, new DirectoryStream.Filter<Path>() {
		@Override
		public boolean accept(Path entry) throws IOException {
			return true;
		}		
	});
	
	try {
		for (Path path: directoryContents) {
			dirGlob.add(path.toString());
		}
	} finally {
		directoryContents.close();
	}
	
	if(!ReiserSpotter.getIsReiser()){
		assertEquals("Checking for correct number of files return (filtered on all)  found " + Arrays.toString(dirGlob.toArray()) +
				" actual total list is " + Arrays.toString(files.toArray()), 
				EXPECTED_NUMBER_OF_FILES, dirGlob.size());
	}		
}
 
Example 3
Source File: PathDirectoryStreamTest.java    From openjdk-systemtest with Apache License 2.0 6 votes vote down vote up
public void testDirectoryStreamGlobSomeItems() throws IOException {		
	final int EXPECTED_NUMBER_OF_FILES = 1;
	Path newDirectory = DIRECTORY_STREAM_FILE_DIR;		
	List<String> dirGlob = new LinkedList<String>();		
	DirectoryStream<Path> directoryContents = Files.newDirectoryStream(newDirectory, "*1.txt");
	
	try {
		for (Path path: directoryContents) {
			dirGlob.add(path.toString());
		}
	} finally {
		directoryContents.close();
	}
	
	if(!ReiserSpotter.getIsReiser()){
		assertEquals("Checking for correct number of files return (globbed on ends in '1') found " + Arrays.toString(dirGlob.toArray()) +
				" actual total list is (from which we expect a subset)" + Arrays.toString(files.toArray()), 
				EXPECTED_NUMBER_OF_FILES, dirGlob.size());
	}		
}
 
Example 4
Source File: TestDirectoryStream.java    From jsr203-hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * According to Java documentation of NIO API, if we try to call
 * <code>next</code> on iterator of closed directory stream, there should be
 * an {@see NoSuchElementException} exception.
 * 
 * @throws IOException
 *           if we can't get the DirectoryStream
 */
@Test(expected = NoSuchElementException.class)
public void testNextOnClosedDirectoryStreamIterator() throws IOException {
  Path dir = Paths.get(clusterUri);
  // create file
  Path file = dir.resolve("foo");
  if (Files.exists(file)) {
    Files.delete(file);
  }
  Files.createFile(file);

  DirectoryStream<Path> stream = Files.newDirectoryStream(dir);
  Iterator<Path> it = stream.iterator();
  stream.close();
  Assert.assertFalse(it.hasNext());
  // This one throws the exception
  it.next();
}
 
Example 5
Source File: MiscUtils.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
public static void deleteHeapTempFiles() {
    if (Platform.isWindows()) { // this is workaroud for JDK bug #6359560

        try {
            File tempDir = new File(System.getProperty("java.io.tmpdir")); // NOI18N
            DirectoryStream<Path> files = Files.newDirectoryStream(tempDir.toPath());

            try {
                for (Path p : files) {
                    String fname = p.toFile().getName();

                    if (fname.startsWith("NBProfiler") && (fname.endsWith(".map") || fname.endsWith(".ref") || fname.endsWith(".gc"))) { // NOI18N
                        Files.delete(p);
                    }
                }
            } finally {
                files.close();
            }
        } catch (IOException ex) {
            System.err.println("deleteHeapTempFiles failed");   // NOI18N
            ex.printStackTrace();
        }
    }
}
 
Example 6
Source File: CodeStoreAndPathTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static void checkCompiledScripts(final DirectoryStream<Path> stream, final int numberOfScripts) throws IOException {
    int n = numberOfScripts;
    for (@SuppressWarnings("unused") final Path file : stream) {
        n--;
    }
    stream.close();
    assertEquals(n, 0);
}
 
Example 7
Source File: WilcardDirectoryStreamTest.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Test method for {@link org.jetel.util.file.stream.WildcardDirectoryStream#hasNext()}.
 */
public void testHasNext() throws IOException {
	int count = 0;
	for (it = stream.iterator(); it.hasNext(); ) {
		DirectoryStream<Input> s = it.next();
		count++;
		s.close();
	}
	assertEquals(1, count);
	stream.close();
	
	stream = newInstance(contextUrl, "");
	it = stream.iterator();
	assertFalse(it.hasNext());
}
 
Example 8
Source File: CodeStoreAndPathTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void checkCompiledScripts(final DirectoryStream<Path> stream, final int numberOfScripts) throws IOException {
    int n = numberOfScripts;
    for (@SuppressWarnings("unused") final Path file : stream) {
        n--;
    }
    stream.close();
    assertEquals(n, 0);
}
 
Example 9
Source File: ArchiveDirectoryStreamTestCase.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testClose() throws IOException {
	URL contextUrl = null;
	byte[] data = prepareData();
	
	final MockInputStream mockInputStream = new MockInputStream(data);
	
	DirectoryStream<Input> parent = createParentDirectoryStream(contextUrl, mockInputStream);
	DirectoryStream<?> stream = openDirectoryStream(parent, "*");
	stream.iterator().next(); // opens the underlying InputStream
	stream.close(); // should close the underlying InputStream
	assertTrue(mockInputStream.isClosed());
}
 
Example 10
Source File: OnDiskHashedIndex.java    From db with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Iterator<String> loadIndexStream(String app, String table, String column, String columnValue) throws OperationException {
    columnValue = "" + columnValue.hashCode();
    Iterator<String> iterator;
    Path path = Paths.get(PathUtil.indexColumnValueFolder(app, table, column, columnValue));
    try {
        DirectoryStream<Path> directoryStream = Files.newDirectoryStream(path);
        iterator = new Iterator<String>() {
            Iterator<Path> innerIterator = directoryStream.iterator();

            @Override
            public boolean hasNext() {
                boolean hasNext = innerIterator.hasNext();
                if (!hasNext) {
                    try {
                        directoryStream.close();
                    } catch (IOException ex) {
                        Logger.getLogger(OnDiskHashedIndex.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                return hasNext;
            }

            @Override
            public String next() {
                return innerIterator.next().getFileName().toString();
            }

            @Override
            public void remove() {
                innerIterator.remove();
            }
        };
    } catch (IOException ex) {
        return null;
    }

    return iterator;

}
 
Example 11
Source File: EncryptedFileSystemProvider.java    From encfs4j with Apache License 2.0 5 votes vote down vote up
private DirectoryStream<Path> mantle(final DirectoryStream<Path> stream) {
	return new DirectoryStream<Path>() {
		@Override
		public Iterator<Path> iterator() {
			final Iterator<Path> itr = stream.iterator();
			return new Iterator<Path>() {
				@Override
				public boolean hasNext() {
					return itr.hasNext();
				}

				@Override
				public Path next() {
					return new EncryptedFileSystemPath(encFileSystem,
							itr.next());
				}

				@Override
				public void remove() {
					itr.remove();
				}
			};
		}

		@Override
		public void close() throws IOException {
			stream.close();
		}
	};
}
 
Example 12
Source File: CodeStoreAndPathTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void checkCompiledScripts(final DirectoryStream<Path> stream, final int numberOfScripts) throws IOException {
    int n = numberOfScripts;
    for (@SuppressWarnings("unused") final Path file : stream) {
        n--;
    }
    stream.close();
    assertEquals(n, 0);
}
 
Example 13
Source File: OnDiskUniqueIndex.java    From db with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Loads an iterator over pk's for all index values matching the specified filter criteria
 *
 * @param app the BlobCity application id
 * @param table the name of the table within the BlobCity application
 * @param column the name of the column within the specified table
 * @param filter a file filter to select only files that match the specified condition
 * @return <code>Iterator<String></code> containing all pk's that match the filter criteria; an empty iterator if no
 * value matches the search criteria
 * @throws OperationException if a files system error occurs when reading the index.
 */
@Override
public Iterator<String> loadIndexStream(String app, String table, String column, OperatorFileFilter filter) throws OperationException {
    Path path = Paths.get(PathUtil.indexColumnFolder(app, table, column));
    Iterator<String> iterator;
    
    if(filter instanceof EQFilenameFilter) {
        return loadIndexStream(app, table, column, filter.getTypeConvertedReferenceValue().toString());
    }
    
    try {
        DirectoryStream<Path> fileStream = Files.newDirectoryStream(path, filter);
        iterator = new Iterator<String>() {
            Iterator<Path> fileIterator = fileStream.iterator();

            @Override
            public boolean hasNext() {
                boolean hasNext = fileIterator.hasNext();
                if(!hasNext) {
                    try {
                        fileStream.close();
                    } catch (IOException ex) {
                        Logger.getLogger(OnDiskUniqueIndex.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                return fileIterator.hasNext();
            }

            @Override
            public String next() {
                if (!fileIterator.hasNext()) {
                    try {
                        fileStream.close();
                    } catch (IOException ex) {
                        Logger.getLogger(OnDiskUniqueIndex.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    throw new NoSuchElementException();
                }
                String fileName = fileIterator.next().getFileName().toString();
                try {
                    return FileNameEncoding.decode(fileName);
                } catch (OperationException ex) {
                    LoggerFactory.getLogger(OnDiskBTreeIndex.class.getName()).error("Failed to decode string: " + fileName, ex);
                    throw new DbRuntimeException(ex);
                }
            }

            @Override
            public void remove() {
                fileIterator.remove();
            }
        };
    } catch (IOException ex) {
        throw new OperationException(ErrorCode.INTERNAL_OPERATION_ERROR);
    }

    return iterator;
}
 
Example 14
Source File: OnDiskHashedIndex.java    From db with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Loads an iterator over pk's for all index values matching the specified filter criteria
 *
 * @param app the BlobCity application id
 * @param table the name of the table within the BlobCity application
 * @param column the name of the column within the specified table
 * @param filter a file filter to select only files that match the specified condition
 * @return <code>Iterator<String></code> containing all pk's that match the filter criteria; an empty iterator if no
 * value matches the search criteria
 * @throws OperationException if a files system error occurs when reading the index.
 */
@Override
public Iterator<String> loadIndexStream(String app, String table, String column, OperatorFileFilter filter) throws OperationException {
    Path path = Paths.get(PathUtil.indexColumnFolder(app, table, column));
    Iterator<String> iterator;
    
    if(filter instanceof EQFilenameFilter) {
        return loadIndexStream(app, table, column, filter.getTypeConvertedReferenceValue().toString());
    }
    
    try {
        DirectoryStream<Path> folderStream = Files.newDirectoryStream(path, filter);
        iterator = new Iterator<String>() {
            Iterator<Path> folderIterator = folderStream.iterator();
            DirectoryStream<Path> fileStream = null;
            Iterator<Path> fileIterator = null;

            @Override
            public boolean hasNext() {
                if (fileIterator == null || !fileIterator.hasNext()) {
                    if (!attemptNextFolder()) {
                        try {
                            folderStream.close();
                        } catch (IOException ex) {
                            Logger.getLogger(OnDiskHashedIndex.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        return false;
                    }
                }
                return fileIterator.hasNext();
            }

            @Override
            public String next() {
                if (fileIterator == null || !fileIterator.hasNext()) {
                    if (!attemptNextFolder()) {
                        throw new NoSuchElementException();
                    }
                }
                return fileIterator.next().getFileName().toString();
            }

            @Override
            public void remove() {
                folderIterator.remove();
            }

            private boolean attemptNextFolder() {
                if (folderIterator.hasNext()) {
                    if (fileStream != null) {
                        try {
                            fileStream.close();
                        } catch (IOException ex) {
                            Logger.getLogger(OnDiskHashedIndex.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                    try {
                        fileStream = Files.newDirectoryStream(folderIterator.next());
                        fileIterator = fileStream.iterator();
                        return true;
                    } catch (IOException ex) {
                        LoggerFactory.getLogger(OnDiskBTreeIndex.class.getName()).error(null, ex);

                        //TODO: An unexpected situation. However a way of reporting this needs to be identified
                    }
                } else {
                    if (fileStream != null) {
                        try {
                            fileStream.close();
                        } catch (IOException ex) {
                            Logger.getLogger(OnDiskHashedIndex.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
                return false;
            }
        };
    } catch (IOException ex) {
        throw new OperationException(ErrorCode.INTERNAL_OPERATION_ERROR);
    }

    return iterator;
}
 
Example 15
Source File: FaultyFileSystem.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
private DirectoryStream<Path> wrap(final DirectoryStream<Path> stream) {
    return new DirectoryStream<Path>() {
        @Override
        public Iterator<Path> iterator() {
            final Iterator<Path> itr = stream.iterator();
            return new Iterator<Path>() {
                private Path next = null;
                @Override
                public boolean hasNext() {
                    if (next == null) {
                        if (itr.hasNext()) {
                            next = itr.next();
                        } else {
                            return false;
                        }
                    }
                    if (next != null) {
                        try {
                            triggerEx(next, "DirectoryIteratorException");
                        } catch (IOException ioe) {
                            throw new DirectoryIteratorException(ioe);
                        } catch (SecurityException se) {
                            // ??? Does DS throw SecurityException during iteration?
                            next = null;
                            return hasNext();
                        }
                    }
                    return (next != null);
                }
                @Override
                public Path next() {
                    try {
                        if (next != null || hasNext()) {
                            return new PassThroughFileSystem.PassThroughPath(delegate, next);
                        } else {
                            throw new NoSuchElementException();
                        }
                    } finally {
                        next = null;
                    }
                }

                @Override
                public void remove() {
                    itr.remove();
                }
            };
        }
        @Override
        public void close() throws IOException {
            stream.close();
        }
    };
}
 
Example 16
Source File: FaultyFileSystem.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
private DirectoryStream<Path> wrap(final DirectoryStream<Path> stream) {
    return new DirectoryStream<Path>() {
        @Override
        public Iterator<Path> iterator() {
            final Iterator<Path> itr = stream.iterator();
            return new Iterator<Path>() {
                private Path next = null;
                @Override
                public boolean hasNext() {
                    if (next == null) {
                        if (itr.hasNext()) {
                            next = itr.next();
                        } else {
                            return false;
                        }
                    }
                    if (next != null) {
                        try {
                            triggerEx(next, "DirectoryIteratorException");
                        } catch (IOException ioe) {
                            throw new DirectoryIteratorException(ioe);
                        } catch (SecurityException se) {
                            // ??? Does DS throw SecurityException during iteration?
                            next = null;
                            return hasNext();
                        }
                    }
                    return (next != null);
                }
                @Override
                public Path next() {
                    try {
                        if (next != null || hasNext()) {
                            return new PassThroughFileSystem.PassThroughPath(delegate, next);
                        } else {
                            throw new NoSuchElementException();
                        }
                    } finally {
                        next = null;
                    }
                }

                @Override
                public void remove() {
                    itr.remove();
                }
            };
        }
        @Override
        public void close() throws IOException {
            stream.close();
        }
    };
}
 
Example 17
Source File: FaultyFileSystem.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
private DirectoryStream<Path> wrap(final DirectoryStream<Path> stream) {
    return new DirectoryStream<Path>() {
        @Override
        public Iterator<Path> iterator() {
            final Iterator<Path> itr = stream.iterator();
            return new Iterator<Path>() {
                private Path next = null;
                @Override
                public boolean hasNext() {
                    if (next == null) {
                        if (itr.hasNext()) {
                            next = itr.next();
                        } else {
                            return false;
                        }
                    }
                    if (next != null) {
                        try {
                            triggerEx(next, "DirectoryIteratorException");
                        } catch (IOException ioe) {
                            throw new DirectoryIteratorException(ioe);
                        } catch (SecurityException se) {
                            // ??? Does DS throw SecurityException during iteration?
                            next = null;
                            return hasNext();
                        }
                    }
                    return (next != null);
                }
                @Override
                public Path next() {
                    try {
                        if (next != null || hasNext()) {
                            return new PassThroughFileSystem.PassThroughPath(delegate, next);
                        } else {
                            throw new NoSuchElementException();
                        }
                    } finally {
                        next = null;
                    }
                }

                @Override
                public void remove() {
                    itr.remove();
                }
            };
        }
        @Override
        public void close() throws IOException {
            stream.close();
        }
    };
}
 
Example 18
Source File: FaultyFileSystem.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private DirectoryStream<Path> wrap(final DirectoryStream<Path> stream) {
    return new DirectoryStream<Path>() {
        @Override
        public Iterator<Path> iterator() {
            final Iterator<Path> itr = stream.iterator();
            return new Iterator<Path>() {
                private Path next = null;
                @Override
                public boolean hasNext() {
                    if (next == null) {
                        if (itr.hasNext()) {
                            next = itr.next();
                        } else {
                            return false;
                        }
                    }
                    if (next != null) {
                        try {
                            triggerEx(next, "DirectoryIteratorException");
                        } catch (IOException ioe) {
                            throw new DirectoryIteratorException(ioe);
                        } catch (SecurityException se) {
                            // ??? Does DS throw SecurityException during iteration?
                            next = null;
                            return hasNext();
                        }
                    }
                    return (next != null);
                }
                @Override
                public Path next() {
                    try {
                        if (next != null || hasNext()) {
                            return new PassThroughFileSystem.PassThroughPath(delegate, next);
                        } else {
                            throw new NoSuchElementException();
                        }
                    } finally {
                        next = null;
                    }
                }

                @Override
                public void remove() {
                    itr.remove();
                }
            };
        }
        @Override
        public void close() throws IOException {
            stream.close();
        }
    };
}
 
Example 19
Source File: FaultyFileSystem.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
private DirectoryStream<Path> wrap(final DirectoryStream<Path> stream) {
    return new DirectoryStream<Path>() {
        @Override
        public Iterator<Path> iterator() {
            final Iterator<Path> itr = stream.iterator();
            return new Iterator<Path>() {
                private Path next = null;
                @Override
                public boolean hasNext() {
                    if (next == null) {
                        if (itr.hasNext()) {
                            next = itr.next();
                        } else {
                            return false;
                        }
                    }
                    if (next != null) {
                        try {
                            triggerEx(next, "DirectoryIteratorException");
                        } catch (IOException ioe) {
                            throw new DirectoryIteratorException(ioe);
                        } catch (SecurityException se) {
                            // ??? Does DS throw SecurityException during iteration?
                            next = null;
                            return hasNext();
                        }
                    }
                    return (next != null);
                }
                @Override
                public Path next() {
                    try {
                        if (next != null || hasNext()) {
                            return new PassThroughFileSystem.PassThroughPath(delegate, next);
                        } else {
                            throw new NoSuchElementException();
                        }
                    } finally {
                        next = null;
                    }
                }

                @Override
                public void remove() {
                    itr.remove();
                }
            };
        }
        @Override
        public void close() throws IOException {
            stream.close();
        }
    };
}
 
Example 20
Source File: FaultyFileSystem.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
private DirectoryStream<Path> wrap(final DirectoryStream<Path> stream) {
    return new DirectoryStream<Path>() {
        @Override
        public Iterator<Path> iterator() {
            final Iterator<Path> itr = stream.iterator();
            return new Iterator<Path>() {
                private Path next = null;
                @Override
                public boolean hasNext() {
                    if (next == null) {
                        if (itr.hasNext()) {
                            next = itr.next();
                        } else {
                            return false;
                        }
                    }
                    if (next != null) {
                        try {
                            triggerEx(next, "DirectoryIteratorException");
                        } catch (IOException ioe) {
                            throw new DirectoryIteratorException(ioe);
                        } catch (SecurityException se) {
                            // ??? Does DS throw SecurityException during iteration?
                            next = null;
                            return hasNext();
                        }
                    }
                    return (next != null);
                }
                @Override
                public Path next() {
                    try {
                        if (next != null || hasNext()) {
                            return new PassThroughFileSystem.PassThroughPath(delegate, next);
                        } else {
                            throw new NoSuchElementException();
                        }
                    } finally {
                        next = null;
                    }
                }

                @Override
                public void remove() {
                    itr.remove();
                }
            };
        }
        @Override
        public void close() throws IOException {
            stream.close();
        }
    };
}