Crossfire JXClient, Trunk
Copy.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * - Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * - Neither the name of Oracle nor the names of its
16  * contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * This source code is provided to illustrate the usage of a given feature
34  * or technique and has been deliberately simplified. Additional steps
35  * required for a production-quality application, such as security checks,
36  * input validation and proper error handling, might not be present in
37  * this sample code.
38  */
39 
40 
41 import java.nio.file.*;
42 import static java.nio.file.StandardCopyOption.*;
43 import java.nio.file.attribute.*;
44 import static java.nio.file.FileVisitResult.*;
45 import java.io.IOException;
46 import java.util.*;
47 
52 public class Copy {
53 
57  static boolean okayToOverwrite(Path file) {
58  String answer = System.console().readLine("overwrite %s (yes/no)? ", file);
59  return (answer.equalsIgnoreCase("y") || answer.equalsIgnoreCase("yes"));
60  }
61 
67  static void copyFile(Path source, Path target, boolean prompt, boolean preserve) {
68  CopyOption[] options = (preserve) ?
69  new CopyOption[] { COPY_ATTRIBUTES, REPLACE_EXISTING } :
70  new CopyOption[] { REPLACE_EXISTING };
71  if (!prompt || Files.notExists(target) || okayToOverwrite(target)) {
72  try {
73  Files.copy(source, target, options);
74  } catch (IOException x) {
75  System.err.format("Unable to copy: %s: %s%n", source, x);
76  }
77  }
78  }
79 
83  static class TreeCopier implements FileVisitor<Path> {
84  private final Path source;
85  private final Path target;
86  private final boolean prompt;
87  private final boolean preserve;
88 
89  TreeCopier(Path source, Path target, boolean prompt, boolean preserve) {
90  this.source = source;
91  this.target = target;
92  this.prompt = prompt;
93  this.preserve = preserve;
94  }
95 
96  @Override
97  public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
98  // before visiting entries in a directory we copy the directory
99  // (okay if directory already exists).
100  CopyOption[] options = (preserve) ?
101  new CopyOption[] { COPY_ATTRIBUTES } : new CopyOption[0];
102 
103  Path newdir = target.resolve(source.relativize(dir));
104  try {
105  Files.copy(dir, newdir, options);
106  } catch (FileAlreadyExistsException x) {
107  // ignore
108  } catch (IOException x) {
109  System.err.format("Unable to create: %s: %s%n", newdir, x);
110  return SKIP_SUBTREE;
111  }
112  return CONTINUE;
113  }
114 
115  @Override
116  public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
117  copyFile(file, target.resolve(source.relativize(file)),
118  prompt, preserve);
119  return CONTINUE;
120  }
121 
122  @Override
123  public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
124  // fix up modification time of directory when done
125  if (exc == null && preserve) {
126  Path newdir = target.resolve(source.relativize(dir));
127  try {
128  FileTime time = Files.getLastModifiedTime(dir);
129  Files.setLastModifiedTime(newdir, time);
130  } catch (IOException x) {
131  System.err.format("Unable to copy all attributes to: %s: %s%n", newdir, x);
132  }
133  }
134  return CONTINUE;
135  }
136 
137  @Override
138  public FileVisitResult visitFileFailed(Path file, IOException exc) {
139  if (exc instanceof FileSystemLoopException) {
140  System.err.println("cycle detected: " + file);
141  } else {
142  System.err.format("Unable to copy: %s: %s%n", file, exc);
143  }
144  return CONTINUE;
145  }
146  }
147 
148  static void usage() {
149  System.err.println("java Copy [-ip] source... target");
150  System.err.println("java Copy -r [-ip] source-dir... target");
151  System.exit(-1);
152  }
153 
154  public static void main(String[] args) throws IOException {
155  boolean recursive = false;
156  boolean prompt = false;
157  boolean preserve = false;
158 
159  // process options
160  int argi = 0;
161  while (argi < args.length) {
162  String arg = args[argi];
163  if (!arg.startsWith("-"))
164  break;
165  if (arg.length() < 2)
166  usage();
167  for (int i=1; i<arg.length(); i++) {
168  char c = arg.charAt(i);
169  switch (c) {
170  case 'r' : recursive = true; break;
171  case 'i' : prompt = true; break;
172  case 'p' : preserve = true; break;
173  default : usage();
174  }
175  }
176  argi++;
177  }
178 
179  // remaining arguments are the source files(s) and the target location
180  int remaining = args.length - argi;
181  if (remaining < 2)
182  usage();
183  Path[] source = new Path[remaining-1];
184  int i=0;
185  while (remaining > 1) {
186  source[i++] = Paths.get(args[argi++]);
187  remaining--;
188  }
189  Path target = Paths.get(args[argi]);
190 
191  // check if target is a directory
192  boolean isDir = Files.isDirectory(target);
193 
194  // copy each source file/directory to target
195  for (i=0; i<source.length; i++) {
196  Path dest = (isDir) ? target.resolve(source[i].getFileName()) : target;
197 
198  if (recursive) {
199  // follow links when copying files
200  EnumSet<FileVisitOption> opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
201  TreeCopier tc = new TreeCopier(source[i], dest, prompt, preserve);
202  Files.walkFileTree(source[i], opts, Integer.MAX_VALUE, tc);
203  } else {
204  // not recursive so source must not be a directory
205  if (Files.isDirectory(source[i])) {
206  System.err.format("%s: is a directory%n", source[i]);
207  continue;
208  }
209  copyFile(source[i], dest, prompt, preserve);
210  }
211  }
212  }
213 }
file
Once a FileSystem is created then classes in the java nio file package can be used to access files in the zip JAR file
Definition: README.txt:19
Copy.TreeCopier.prompt
final boolean prompt
Definition: Copy.java:86
Copy.TreeCopier.target
final Path target
Definition: Copy.java:85
Copy.TreeCopier.TreeCopier
TreeCopier(Path source, Path target, boolean prompt, boolean preserve)
Definition: Copy.java:89
Copy.copyFile
static void copyFile(Path source, Path target, boolean prompt, boolean preserve)
Definition: Copy.java:67
Copy.TreeCopier.preserve
final boolean preserve
Definition: Copy.java:87
Copy.TreeCopier.visitFile
FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
Definition: Copy.java:116
prompt
where< script-file-path > is the path of your script file to load If you don t specify the file then the load function shows file dialog box to choose the script file to load How do I get help on script global functions If you use then there is a global function called help that prints one line help messages on global functions In script console prompt
Definition: README.txt:57
Copy.TreeCopier.postVisitDirectory
FileVisitResult postVisitDirectory(Path dir, IOException exc)
Definition: Copy.java:123
Copy
Definition: Copy.java:52
Copy.usage
static void usage()
Definition: Copy.java:148
Copy.TreeCopier.visitFileFailed
FileVisitResult visitFileFailed(Path file, IOException exc)
Definition: Copy.java:138
Copy.TreeCopier.source
final Path source
Definition: Copy.java:84
Copy.TreeCopier
Definition: Copy.java:83
Copy.okayToOverwrite
static boolean okayToOverwrite(Path file)
Definition: Copy.java:57
Copy.TreeCopier.preVisitDirectory
FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
Definition: Copy.java:97
Copy.main
static void main(String[] args)
Definition: Copy.java:154
time
Font2DTest To run then you should either specify the complete path to the commands or update your PATH environment variable as described in the installation instructions for the load Font2DTest html If you wish to modify any of the source you may want to extract the contents of the Font2DTest jar file by executing this the browser plugin viewer needs following permissions given in order to run but some of its features will be limited To enable all please add these permissions with policytool Introduction Font2DTest is an encompassing application for testing various fonts found on the user s system A number of controls are available to change many attributes of the current font including and rendering hints The user can select from multiple display such as one Unicode range at a time
Definition: README.txt:44