use of java.nio.file.FileVisitResult in project dex2jar by pxb1988.
the class WebApp method main.
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
if (args.length < 2) {
System.out.println("webapp pathToWebApp config [ignoreJarConfig]");
return;
}
File webApp = new File(args[0]);
File config = new File(args[1]);
Path jarIgnore = args.length > 2 ? new File(args[2]).toPath() : null;
Path clz = new File(webApp, "WEB-INF/classes").toPath();
Path tmpClz = new File(webApp, "WEB-INF/tmp-classes").toPath();
final InvocationWeaver ro = (InvocationWeaver) new InvocationWeaver().withConfig(config.toPath());
Files.deleteIfExists(tmpClz);
copyDirectory(clz, tmpClz);
System.out.println("InvocationWeaver from [" + tmpClz + "] to [" + clz + "]");
ro.wave(tmpClz, clz);
Files.deleteIfExists(tmpClz);
final File lib = new File(webApp, "WEB-INF/lib");
Path tmpLib = new File(webApp, "WEB-INF/Nlib").toPath();
final Set<String> ignores = new HashSet<String>();
if (jarIgnore != null && Files.exists(jarIgnore)) {
ignores.addAll(Files.readAllLines(jarIgnore, StandardCharsets.UTF_8));
} else {
System.out.println("ignoreJarConfig ignored");
}
Files.deleteIfExists(tmpLib);
copyDirectory(lib.toPath(), tmpLib);
Files.walkFileTree(tmpLib, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.getFileName().toString().endsWith(".jar")) {
final String s = file.getFileName().toString();
boolean ignore = false;
for (String i : ignores) {
if (s.startsWith(i)) {
ignore = true;
break;
}
}
if (!ignore) {
Path nJar = new File(lib, s).toPath();
System.out.println("InvocationWeaver from [" + file + "] to [" + nJar + "]");
ro.wave(file, nJar);
}
}
return super.visitFile(file, attrs);
}
});
Files.deleteIfExists(tmpLib);
}
use of java.nio.file.FileVisitResult in project robovm by robovm.
the class AfcClient method upload.
/**
* Uploads a local file or directory to the device.
*
* @param localFile the file or directory to upload.
* @param targetPath the path of the directory on the device where to place
* the uploaded files.
* @param callback callback which will receive progress and status updates.
* If <code>null</code> no progress will be reported.
*/
public void upload(File localFile, final String targetPath, final UploadProgressCallback callback) throws IOException {
makeDirectory(targetPath);
final Path root = localFile.toPath().getParent();
// 64k seems to be a good buffer size. If smaller we will not get
// acceptable write speeds.
final byte[] buffer = new byte[64 * 1024];
class FileCounterVisitor extends SimpleFileVisitor<Path> {
int count;
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
count++;
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
count++;
return FileVisitResult.CONTINUE;
}
}
FileCounterVisitor visitor = new FileCounterVisitor();
if (callback != null) {
Files.walkFileTree(localFile.toPath(), visitor);
}
try {
final int fileCount = visitor.count;
Files.walkFileTree(localFile.toPath(), new SimpleFileVisitor<Path>() {
int filesUploaded = 0;
private void reportProgress(Path path) {
if (callback != null) {
callback.progress(path.toFile(), 100 * filesUploaded / fileCount);
}
filesUploaded++;
}
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
reportProgress(dir);
String deviceDir = toAbsoluteDevicePath(targetPath, root.relativize(dir));
makeDirectory(deviceDir);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
reportProgress(file);
String deviceFile = toAbsoluteDevicePath(targetPath, root.relativize(file));
if (Files.isSymbolicLink(file)) {
Path linkTargetPath = Files.readSymbolicLink(file);
makeLink(AfcLinkType.AFC_SYMLINK, toRelativeDevicePath(linkTargetPath), deviceFile);
} else if (Files.isRegularFile(file, LinkOption.NOFOLLOW_LINKS)) {
long fd = fileOpen(deviceFile, AfcFileMode.AFC_FOPEN_WRONLY);
try (InputStream is = Files.newInputStream(file)) {
int n = 0;
while ((n = is.read(buffer)) != -1) {
fileWrite(fd, buffer, 0, n);
}
} finally {
fileClose(fd);
}
}
return FileVisitResult.CONTINUE;
}
});
if (callback != null) {
callback.success();
}
} catch (IOException e) {
if (callback != null) {
callback.error(e.getMessage());
}
throw e;
} catch (LibIMobileDeviceException e) {
if (callback != null) {
callback.error(e.getMessage());
}
throw e;
}
}
use of java.nio.file.FileVisitResult in project wire by square.
the class ParsingTester method main.
public static void main(String... args) throws IOException {
final AtomicLong total = new AtomicLong();
final AtomicLong failed = new AtomicLong();
Files.walkFileTree(ROOT, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.getFileName().toString().endsWith(".proto")) {
total.incrementAndGet();
String data = new String(Files.readAllBytes(file), UTF_8);
Location location = Location.get(ROOT.toString(), file.toString());
try {
ProtoParser.parse(location, data);
} catch (Exception e) {
e.printStackTrace();
failed.incrementAndGet();
}
}
return FileVisitResult.CONTINUE;
}
});
System.out.println("\nTotal: " + total.get() + " Failed: " + failed.get());
if (failed.get() == 0) {
new SchemaLoader().addSource(ROOT).load();
System.out.println("All files linked successfully.");
}
}
use of java.nio.file.FileVisitResult in project bazel by bazelbuild.
the class SimpleJavaLibraryBuilder method setUpSourceJars.
/**
* Extracts the all source jars from the build request into the temporary directory specified in
* the build request. Empties the temporary directory, if it exists.
*/
private void setUpSourceJars(JavaLibraryBuildRequest build) throws IOException {
String sourcesDir = build.getTempDir();
Path sourceDirFile = Paths.get(sourcesDir);
if (Files.exists(sourceDirFile)) {
cleanupDirectory(sourceDirFile);
}
if (build.getSourceJars().isEmpty()) {
return;
}
final ByteArrayOutputStream protobufMetadataBuffer = new ByteArrayOutputStream();
for (String sourceJar : build.getSourceJars()) {
for (Path root : getJarFileSystem(Paths.get(sourceJar)).getRootDirectories()) {
Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
String fileName = path.getFileName().toString();
if (fileName.endsWith(".java")) {
build.getSourceFiles().add(path);
} else if (fileName.equals(PROTOBUF_META_NAME)) {
Files.copy(path, protobufMetadataBuffer);
}
return FileVisitResult.CONTINUE;
}
});
}
}
Path output = Paths.get(build.getClassDir(), PROTOBUF_META_NAME);
if (protobufMetadataBuffer.size() > 0) {
try (OutputStream outputStream = Files.newOutputStream(output)) {
protobufMetadataBuffer.writeTo(outputStream);
}
} else if (Files.exists(output)) {
// Delete stalled meta file.
Files.delete(output);
}
}
use of java.nio.file.FileVisitResult in project jdk8u_jdk by JetBrains.
the class TestWsImport method main.
public static void main(String[] args) throws IOException {
String javaHome = System.getProperty("java.home");
if (javaHome.endsWith("jre")) {
javaHome = new File(javaHome).getParent();
}
String wsimport = javaHome + File.separator + "bin" + File.separator + "wsimport";
if (System.getProperty("os.name").startsWith("Windows")) {
wsimport = wsimport.concat(".exe");
}
Endpoint endpoint = Endpoint.create(new TestService());
HttpServer httpServer = null;
try {
// Manually create HttpServer here using ephemeral address for port
// so as to not end up with attempt to bind to an in-use port
httpServer = HttpServer.create(new InetSocketAddress(0), 0);
HttpContext httpContext = httpServer.createContext("/hello");
int port = httpServer.getAddress().getPort();
System.out.println("port = " + port);
httpServer.start();
endpoint.publish(httpContext);
String address = "http://localhost:" + port + "/hello";
Service service = Service.create(new URL(address + "?wsdl"), new QName("http://test/jaxws/sample/", "TestService"));
String[] wsargs = { wsimport, "-p", "wstest", "-J-Djavax.xml.accessExternalSchema=all", "-J-Dcom.sun.tools.internal.ws.Invoker.noSystemProxies=true", address + "?wsdl", "-clientjar", "wsjar.jar" };
ProcessBuilder pb = new ProcessBuilder(wsargs);
pb.redirectErrorStream(true);
Process p = pb.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String s = r.readLine();
while (s != null) {
System.out.println(s.trim());
s = r.readLine();
}
p.waitFor();
p.destroy();
try (JarFile jarFile = new JarFile("wsjar.jar")) {
for (Enumeration em = jarFile.entries(); em.hasMoreElements(); ) {
String fileName = em.nextElement().toString();
if (fileName.contains("\\")) {
throw new RuntimeException("\"\\\" character detected in jar file: " + fileName);
}
}
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
} finally {
endpoint.stop();
if (httpServer != null) {
httpServer.stop(0);
}
Path p = Paths.get("wsjar.jar");
Files.deleteIfExists(p);
p = Paths.get("wstest");
if (Files.exists(p)) {
try {
Files.walkFileTree(p, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
if (exc == null) {
Files.delete(dir);
return CONTINUE;
} else {
throw exc;
}
}
});
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}
Aggregations