@Test public void testOpenDirectoryStreamsClosed() throws IOException { Path p = fs.getPath("/foo"); Files.createDirectory(p); try (DirectoryStream<Path> stream = Files.newDirectoryStream(p)) { fs.close(); try { stream.iterator(); fail(); } catch (ClosedDirectoryStreamException expected) { } } }
@Override public synchronized Iterator<Path> iterator() { if (isClosed) throw new ClosedDirectoryStreamException(); if (itr != null) throw new IllegalStateException("Iterator has already been returned"); try { itr = zipfs.iteratorOf(path, filter); } catch (IOException e) { throw new IllegalStateException(e); } return new Iterator<Path>() { private Path next; @Override public boolean hasNext() { if (isClosed) return false; return itr.hasNext(); } @Override public synchronized Path next() { if (isClosed) throw new NoSuchElementException(); return itr.next(); } @Override public void remove() { throw new UnsupportedOperationException(); } }; }
@Override public synchronized Iterator<Path> iterator() { if (isClosed) throw new ClosedDirectoryStreamException(); if (itr != null) throw new IllegalStateException("Iterator has already been returned"); try { itr = dir.jrtfs.iteratorOf(dir, filter); } catch (IOException e) { throw new IllegalStateException(e); } return new Iterator<Path>() { @Override public boolean hasNext() { synchronized (JrtDirectoryStream.this) { if (isClosed) return false; return itr.hasNext(); } } @Override public Path next() { synchronized (JrtDirectoryStream.this) { if (isClosed) throw new NoSuchElementException(); return itr.next(); } } }; }
@Override public synchronized Iterator<Path> iterator() { if (isClosed) throw new ClosedDirectoryStreamException(); if (itr != null) throw new IllegalStateException("Iterator has already been returned"); try { itr = dir.jrtfs.iteratorOf(dir, filter); } catch (IOException e) { throw new IllegalStateException(e); } return new Iterator<Path>() { private Path next; @Override public synchronized boolean hasNext() { if (isClosed) return false; return itr.hasNext(); } @Override public synchronized Path next() { if (isClosed) throw new NoSuchElementException(); return itr.next(); } @Override public void remove() { throw new UnsupportedOperationException(); } }; }
@Override public void assertNotClosed() throws FileSystemException { if(closed) { throw new ClosedDirectoryStreamException(); } }
@Override public synchronized Iterator<Path> iterator() { if (isClosed) throw new ClosedDirectoryStreamException(); if (itr != null) throw new IllegalStateException("Iterator has already been returned"); try { itr = jrtfs.iteratorOf(path, childPrefix, filter); } catch (IOException e) { throw new IllegalStateException(e); } return new Iterator<Path>() { @Override public boolean hasNext() { if (isClosed) return false; return itr.hasNext(); } @Override public synchronized Path next() { if (isClosed) throw new NoSuchElementException(); return itr.next(); } @Override public void remove() { throw new UnsupportedOperationException(); } }; }
@Override public synchronized Iterator<Path> iterator() { if (isClosed) { throw new ClosedDirectoryStreamException(); } if (itr != null) { throw new IllegalStateException("Iterator has already been returned"); } try { itr = tarfs.iteratorOf(path, filter); } catch (IOException e) { throw new IllegalStateException(e); } return new Iterator<Path>() { @SuppressWarnings("unused") private Path next; @Override public boolean hasNext() { if (isClosed) { return false; } return itr.hasNext(); } @Override public synchronized Path next() { if (isClosed) { throw new NoSuchElementException(); } return itr.next(); } @Override public void remove() { throw new UnsupportedOperationException(); } }; }
@Test( expected = ClosedDirectoryStreamException.class ) @Category( Closable.class ) @SuppressWarnings( "PMD.UnusedLocalVariable" ) public void testReadFromDirStreamOfClosedFSThrows() throws IOException { for ( Path kid : getClosedDirStream()) { } }
void checkClosed() { if (dir == null) { throw new ClosedDirectoryStreamException(); } }
@Nonnull @Override public Iterator<Path> iterator() { final Iterator<String> childrenIt = children.iterator(); return new Iterator<Path>() { private Path next; private boolean findNext() { while(childrenIt.hasNext()) { String child = childrenIt.next(); GitPath childPath = parent.resolve(child); try { if(filter == null || filter.accept(childPath)) { next = childPath; return true; } } catch(IOException ignore) { } } return false; } @Override public boolean hasNext() throws ClosedDirectoryStreamException { checkNotClosed(); return next != null || findNext(); } @Nonnull @Override public Path next() throws ClosedDirectoryStreamException, NoSuchElementException { checkNotClosed(); if(next != null || hasNext()) { Path ret = next; next = null; return ret; } throw new NoSuchElementException(); } @Override public void remove() throws UnsupportedOperationException { throw new UnsupportedOperationException(); } }; }
private void checkNotClosed() throws ClosedDirectoryStreamException { if(closed) throw new ClosedDirectoryStreamException(); }
protected synchronized void checkOpen() { if (!open) { throw new ClosedDirectoryStreamException(); } }