下面列出了java.nio.file.DirectoryStream#close ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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();
}
}
}
/**
* 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();
}
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());
}
}
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());
}
}
@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());
}
};
}
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);
}
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();
}
};
}
@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;
}
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());
}
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);
}
/**
* 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());
}
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);
}
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();
}
};
}
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();
}
};
}
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();
}
};
}
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();
}
};
}
/**
* 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;
}
/**
* 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;
}
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();
}
};
}
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();
}
};
}