Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updated java.7 home property for new java 7 SDK, pushed common test s…
…etup into new BaseFileTest class, added tests to DirectoryStreamTest, added FilterBuilder class added method to DirUtils created AsynchronousRecursiveDirectoryStream and test
- Loading branch information
Showing
9 changed files
with
467 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1 +1,4 @@ | |||
Source code for the Java 7 java.nio.file blog series. Please note that the pom file needs to have the path for Java 7 set as I'm still using Java 6 as my default and needed to specify in the pom where Java 7 lives for compilation and tests. | Source code for the Java 7 java.nio.file blog series. | ||
Please note that the pom file needs to have the path for Java 7 | |||
set as I'm still using Java 6 as my default JDK and needed to specify | |||
in the pom where Java 7 lives for compilation and tests. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
src/main/java/bbejeck/nio/files/directory/AsynchronousRecursiveDirectoryStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,117 @@ | |||
package bbejeck.nio.files.directory; | |||
|
|||
import bbejeck.nio.files.visitor.FunctionVisitor; | |||
import bbejeck.nio.util.FilterBuilder; | |||
import com.google.common.base.Function; | |||
|
|||
import java.io.IOException; | |||
import java.nio.file.DirectoryStream; | |||
import java.nio.file.FileVisitResult; | |||
import java.nio.file.Files; | |||
import java.nio.file.Path; | |||
import java.util.Iterator; | |||
import java.util.Objects; | |||
import java.util.concurrent.*; | |||
|
|||
/** | |||
* Created by IntelliJ IDEA. | |||
* User: bbejeck | |||
* Date: 2/5/12 | |||
* Time: 1:05 PM | |||
*/ | |||
public class AsynchronousRecursiveDirectoryStream implements DirectoryStream<Path> { | |||
|
|||
private LinkedBlockingQueue<Path> pathsBlockingQueue = new LinkedBlockingQueue<>(); | |||
private boolean closed = false; | |||
private FutureTask<Void> pathTask; | |||
private Path startPath; | |||
private Filter filter; | |||
|
|||
public AsynchronousRecursiveDirectoryStream(Path startPath, String pattern) throws IOException { | |||
this.filter = FilterBuilder.buildGlobFilter(Objects.requireNonNull(pattern)); | |||
this.startPath = Objects.requireNonNull(startPath); | |||
} | |||
|
|||
@Override | |||
public Iterator<Path> iterator() { | |||
confirmNotClosed(); | |||
findFiles(startPath, filter); | |||
return new Iterator<Path>() { | |||
Path path; | |||
@Override | |||
public boolean hasNext() { | |||
try { | |||
path = pathsBlockingQueue.poll(); | |||
while (!pathTask.isDone() && path == null) { | |||
path = pathsBlockingQueue.poll(5, TimeUnit.MILLISECONDS); | |||
} | |||
return (path != null) ? true : false; | |||
} catch (InterruptedException e) { | |||
Thread.currentThread().interrupt(); | |||
} | |||
return false; | |||
} | |||
|
|||
@Override | |||
public Path next() { | |||
return path; | |||
} | |||
|
|||
@Override | |||
public void remove() { | |||
throw new UnsupportedOperationException("Removal not supported"); | |||
} | |||
}; | |||
} | |||
|
|||
private void findFiles(final Path startPath, final Filter filter) { | |||
pathTask = new FutureTask<Void>(new Callable<Void>() { | |||
@Override | |||
public Void call() throws Exception { | |||
Files.walkFileTree(startPath, new FunctionVisitor(getFunction(filter))); | |||
return null; | |||
} | |||
}); | |||
start(pathTask); | |||
} | |||
|
|||
private Function<Path, FileVisitResult> getFunction(final Filter<Path> filter) { | |||
return new Function<Path, FileVisitResult>() { | |||
@Override | |||
public FileVisitResult apply(Path input) { | |||
try { | |||
if (filter.accept(input.getFileName())) { | |||
pathsBlockingQueue.offer(input); | |||
} | |||
} catch (IOException e) { | |||
throw new RuntimeException(e.getMessage()); | |||
} | |||
return (pathTask.isCancelled()) ? FileVisitResult.TERMINATE : FileVisitResult.CONTINUE; | |||
} | |||
}; | |||
} | |||
|
|||
|
|||
@Override | |||
public void close() throws IOException { | |||
if(pathTask !=null){ | |||
pathTask.cancel(true); | |||
} | |||
pathsBlockingQueue.clear(); | |||
pathsBlockingQueue = null; | |||
pathTask = null; | |||
filter = null; | |||
closed = true; | |||
} | |||
|
|||
private void start(FutureTask<Void> futureTask) { | |||
new Thread(futureTask).start(); | |||
} | |||
|
|||
private void confirmNotClosed() { | |||
if (closed) { | |||
throw new IllegalStateException("DirectoryStream has already been closed"); | |||
} | |||
} | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,45 @@ | |||
package bbejeck.nio.util; | |||
|
|||
import java.io.IOException; | |||
import java.nio.file.DirectoryStream; | |||
import java.nio.file.FileSystems; | |||
import java.nio.file.Path; | |||
import java.nio.file.PathMatcher; | |||
|
|||
/** | |||
* Created by IntelliJ IDEA. | |||
* User: bbejeck | |||
* Date: 2/2/12 | |||
* Time: 11:46 PM | |||
*/ | |||
|
|||
public class FilterBuilder { | |||
|
|||
private FilterBuilder() {} | |||
|
|||
public static DirectoryStream.Filter<Path> buildGlobFilter(String pattern) { | |||
final PathMatcher pathMatcher = getPathMatcher("glob:"+pattern); | |||
return new DirectoryStream.Filter<Path>() { | |||
@Override | |||
public boolean accept(Path entry) throws IOException { | |||
return pathMatcher.matches(entry); | |||
} | |||
}; | |||
} | |||
|
|||
public static DirectoryStream.Filter<Path> buildRegexFilter(String pattern) { | |||
final PathMatcher pathMatcher = getPathMatcher("regex:"+pattern); | |||
return new DirectoryStream.Filter<Path>() { | |||
@Override | |||
public boolean accept(Path entry) throws IOException { | |||
return pathMatcher.matches(entry); | |||
} | |||
}; | |||
} | |||
|
|||
|
|||
private static PathMatcher getPathMatcher(String pattern) { | |||
return FileSystems.getDefault().getPathMatcher(pattern); | |||
} | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,79 @@ | |||
package bbejeck.nio.files; | |||
|
|||
import bbejeck.nio.util.DirUtils; | |||
import bbejeck.nio.util.FileGenerator; | |||
import org.junit.Before; | |||
|
|||
import java.nio.file.Files; | |||
import java.nio.file.Path; | |||
import java.nio.file.Paths; | |||
|
|||
/** | |||
* Created by IntelliJ IDEA. | |||
* User: bbejeck | |||
* Date: 1/31/12 | |||
* Time: 9:31 PM | |||
*/ | |||
|
|||
public class BaseFileTest { | |||
protected String baseDir = "test-files"; | |||
protected String dir1 = "dir1"; | |||
protected String dir2 = "dir2"; | |||
protected String fileName; | |||
protected String fooDir = "foo"; | |||
protected String copyDir = "copy"; | |||
protected String tempDir = "temp"; | |||
protected Path basePath; | |||
protected Path dir1Path; | |||
protected Path dir2Path; | |||
protected Path fooPath; | |||
protected Path sourcePath; | |||
protected Path copyPath; | |||
protected String[] fakeJavaFiles = new String[]{"Code.java", "Foo.java", "Bar.java", "Baz.java"}; | |||
protected String[] textFileNames = new String[]{"persons.csv", "counts.csv", "CountyTaxes.csv"}; | |||
|
|||
|
|||
@Before | |||
public void setUp() throws Exception { | |||
basePath = Paths.get(baseDir); | |||
dir1Path = basePath.resolve(dir1); | |||
dir2Path = basePath.resolve(dir2); | |||
fileName = "test.txt"; | |||
sourcePath = basePath.resolve(fileName); | |||
copyPath = basePath.resolve(copyDir); | |||
fooPath = basePath.resolve(fooDir); | |||
cleanUp(); | |||
createDirectories(); | |||
generateFile(Paths.get(baseDir, fileName), 500); | |||
generateFiles(basePath, fakeJavaFiles, textFileNames); | |||
generateFiles(dir1Path, fakeJavaFiles); | |||
generateFiles(dir2Path, fakeJavaFiles); | |||
} | |||
|
|||
protected void cleanUp() throws Exception { | |||
DirUtils.deleteIfExists(basePath); | |||
Files.deleteIfExists(basePath.resolve(Paths.get(copyDir, fooDir))); | |||
Files.deleteIfExists(basePath.resolve(Paths.get(copyDir, tempDir))); | |||
Files.deleteIfExists(basePath.resolve(tempDir)); | |||
Files.deleteIfExists(basePath.resolve("tempII")); | |||
} | |||
|
|||
protected void createDirectories() throws Exception { | |||
Files.createDirectories(dir1Path); | |||
Files.createDirectories(dir2Path); | |||
Files.createDirectories(copyPath); | |||
Files.createDirectories(fooPath); | |||
} | |||
|
|||
protected void generateFile(Path path, int numberLines) throws Exception { | |||
FileGenerator.generate(path, numberLines); | |||
} | |||
|
|||
protected void generateFiles(Path path, String[]... fileNames) throws Exception { | |||
for (String[] fileNamesArray : fileNames) { | |||
for (String fileName : fileNamesArray) { | |||
generateFile(path.resolve(fileName), 10); | |||
} | |||
} | |||
} | |||
} |
Oops, something went wrong.