blob: abc48ccf5ad7fb74f5a05de7aae9da3910315297 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska24c849c2014-10-27 09:53:05 -070015 */
tom5f38b3a2014-08-27 23:50:54 -070016package org.onlab.util;
17
Madan Jampani2bfa94c2015-04-11 05:03:49 -070018import static java.nio.file.Files.delete;
19import static java.nio.file.Files.walkFileTree;
20import static org.onlab.util.GroupedThreadFactory.groupedThreadFactory;
21import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHI683e9782014-11-25 17:26:36 -080022
tom53efab52014-10-07 17:43:48 -070023import java.io.BufferedReader;
24import java.io.File;
Ray Milkey705d9bc2014-11-18 08:19:00 -080025import java.io.FileInputStream;
tom53efab52014-10-07 17:43:48 -070026import java.io.IOException;
Ray Milkey705d9bc2014-11-18 08:19:00 -080027import java.io.InputStreamReader;
Madan Jampani27b69c62015-05-15 15:49:02 -070028import java.nio.ByteBuffer;
Ray Milkey705d9bc2014-11-18 08:19:00 -080029import java.nio.charset.StandardCharsets;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080030import java.nio.file.FileVisitResult;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080031import java.nio.file.Files;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080032import java.nio.file.Path;
33import java.nio.file.Paths;
34import java.nio.file.SimpleFileVisitor;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080035import java.nio.file.StandardCopyOption;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080036import java.nio.file.attribute.BasicFileAttributes;
tom53efab52014-10-07 17:43:48 -070037import java.util.ArrayList;
Madan Jampani27b69c62015-05-15 15:49:02 -070038import java.util.Arrays;
Brian O'Connore2eac102015-02-12 18:30:22 -080039import java.util.Collection;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070040import java.util.Dictionary;
tom53efab52014-10-07 17:43:48 -070041import java.util.List;
Thomas Vachuskaadba1522015-06-04 15:08:30 -070042import java.util.Random;
Madan Jampani27b69c62015-05-15 15:49:02 -070043import java.util.concurrent.CompletableFuture;
Madan Jampani2bfa94c2015-04-11 05:03:49 -070044import java.util.concurrent.ExecutionException;
45import java.util.concurrent.Future;
tom5f38b3a2014-08-27 23:50:54 -070046import java.util.concurrent.ThreadFactory;
Madan Jampani2bfa94c2015-04-11 05:03:49 -070047import java.util.concurrent.TimeUnit;
48import java.util.concurrent.TimeoutException;
Madan Jampania29c6772015-08-17 13:17:07 -070049import java.util.function.Function;
50import java.util.function.Supplier;
HIGUCHI Yutabfc8b7a2015-07-01 23:47:43 -070051import java.util.stream.Stream;
52import java.util.stream.StreamSupport;
tom5f38b3a2014-08-27 23:50:54 -070053
Madan Jampani2bfa94c2015-04-11 05:03:49 -070054import org.slf4j.Logger;
55
56import com.google.common.base.Strings;
57import com.google.common.primitives.UnsignedLongs;
58import com.google.common.util.concurrent.ThreadFactoryBuilder;
Ray Milkey705d9bc2014-11-18 08:19:00 -080059
Thomas Vachuskac13b90a2015-02-18 18:19:55 -080060/**
61 * Miscellaneous utility methods.
62 */
tom5f38b3a2014-08-27 23:50:54 -070063public abstract class Tools {
64
65 private Tools() {
66 }
67
Thomas Vachuska02aeb032015-01-06 22:36:30 -080068 private static final Logger log = getLogger(Tools.class);
Yuta HIGUCHI683e9782014-11-25 17:26:36 -080069
Thomas Vachuskaadba1522015-06-04 15:08:30 -070070 private static Random random = new Random();
71
tom5f38b3a2014-08-27 23:50:54 -070072 /**
73 * Returns a thread factory that produces threads named according to the
74 * supplied name pattern.
75 *
76 * @param pattern name pattern
77 * @return thread factory
78 */
79 public static ThreadFactory namedThreads(String pattern) {
Yuta HIGUCHI683e9782014-11-25 17:26:36 -080080 return new ThreadFactoryBuilder()
81 .setNameFormat(pattern)
Thomas Vachuska480adad2015-03-06 10:27:09 -080082 .setUncaughtExceptionHandler((t, e) -> log.error("Uncaught exception on " + t.getName(), e))
83 .build();
Thomas Vachuska9c17a6d2015-02-17 23:36:43 -080084 }
Yuta HIGUCHI683e9782014-11-25 17:26:36 -080085
Thomas Vachuska9c17a6d2015-02-17 23:36:43 -080086 /**
87 * Returns a thread factory that produces threads named according to the
88 * supplied name pattern and from the specified thread-group. The thread
89 * group name is expected to be specified in slash-delimited format, e.g.
Thomas Vachuskac13b90a2015-02-18 18:19:55 -080090 * {@code onos/intent}. The thread names will be produced by converting
91 * the thread group name into dash-delimited format and pre-pended to the
92 * specified pattern.
Thomas Vachuska9c17a6d2015-02-17 23:36:43 -080093 *
94 * @param groupName group name in slash-delimited format to indicate hierarchy
95 * @param pattern name pattern
96 * @return thread factory
97 */
98 public static ThreadFactory groupedThreads(String groupName, String pattern) {
99 return new ThreadFactoryBuilder()
100 .setThreadFactory(groupedThreadFactory(groupName))
Thomas Vachuskac13b90a2015-02-18 18:19:55 -0800101 .setNameFormat(groupName.replace(GroupedThreadFactory.DELIMITER, "-") + "-" + pattern)
Thomas Vachuska480adad2015-03-06 10:27:09 -0800102 .setUncaughtExceptionHandler((t, e) -> log.error("Uncaught exception on " + t.getName(), e))
103 .build();
tom5f38b3a2014-08-27 23:50:54 -0700104 }
105
tom782a7cf2014-09-11 23:58:38 -0700106 /**
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800107 * Returns a thread factory that produces threads with MIN_PRIORITY.
108 *
109 * @param factory backing ThreadFactory
110 * @return thread factory
111 */
112 public static ThreadFactory minPriority(ThreadFactory factory) {
113 return new ThreadFactoryBuilder()
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800114 .setThreadFactory(factory)
115 .setPriority(Thread.MIN_PRIORITY)
116 .build();
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800117 }
118
119 /**
Brian O'Connore2eac102015-02-12 18:30:22 -0800120 * Returns true if the collection is null or is empty.
121 *
122 * @param collection collection to test
123 * @return true if null or empty; false otherwise
124 */
125 public static boolean isNullOrEmpty(Collection collection) {
126 return collection == null || collection.isEmpty();
127 }
128
129 /**
Ray Milkeyd43fe452015-05-29 09:35:12 -0700130 * Returns the specified item if that item is not null; otherwise throws
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700131 * not found exception.
132 *
133 * @param item item to check
134 * @param message not found message
135 * @param <T> item type
136 * @return item if not null
137 * @throws org.onlab.util.ItemNotFoundException if item is null
138 */
139 public static <T> T nullIsNotFound(T item, String message) {
140 if (item == null) {
141 throw new ItemNotFoundException(message);
142 }
143 return item;
144 }
145
146 /**
Ray Milkeyd43fe452015-05-29 09:35:12 -0700147 * Returns the specified item if that item is not null; otherwise throws
148 * bad argument exception.
149 *
150 * @param item item to check
151 * @param message not found message
152 * @param <T> item type
153 * @return item if not null
154 * @throws IllegalArgumentException if item is null
155 */
156 public static <T> T nullIsIllegal(T item, String message) {
157 if (item == null) {
158 throw new IllegalArgumentException(message);
159 }
160 return item;
161 }
162
163 /**
tom782a7cf2014-09-11 23:58:38 -0700164 * Converts a string from hex to long.
165 *
166 * @param string hex number in string form; sans 0x
167 * @return long value
168 */
169 public static long fromHex(String string) {
170 return UnsignedLongs.parseUnsignedLong(string, 16);
171 }
172
173 /**
174 * Converts a long value to hex string; 16 wide and sans 0x.
175 *
176 * @param value long value
177 * @return hex string
178 */
179 public static String toHex(long value) {
180 return Strings.padStart(UnsignedLongs.toString(value, 16), 16, '0');
181 }
182
183 /**
184 * Converts a long value to hex string; 16 wide and sans 0x.
185 *
186 * @param value long value
187 * @param width string width; zero padded
188 * @return hex string
189 */
190 public static String toHex(long value, int width) {
191 return Strings.padStart(UnsignedLongs.toString(value, 16), width, '0');
192 }
tomf110fff2014-09-26 00:38:18 -0700193
194 /**
Madan Jampani9eb55d12015-08-14 07:47:56 -0700195 * Returns a copy of the input byte array.
196 *
197 * @param original input
198 * @return copy of original
199 */
200 public static byte[] copyOf(byte[] original) {
201 return Arrays.copyOf(original, original.length);
202 }
203
204 /**
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700205 * Get property as a string value.
206 *
207 * @param properties properties to be looked up
208 * @param propertyName the name of the property to look up
209 * @return value when the propertyName is defined or return null
210 */
211 public static String get(Dictionary<?, ?> properties, String propertyName) {
212 Object v = properties.get(propertyName);
213 String s = (v instanceof String) ? (String) v :
214 v != null ? v.toString() : null;
215 return Strings.isNullOrEmpty(s) ? null : s.trim();
216 }
217
218 /**
tomf110fff2014-09-26 00:38:18 -0700219 * Suspends the current thread for a specified number of millis.
220 *
221 * @param ms number of millis
222 */
223 public static void delay(int ms) {
224 try {
225 Thread.sleep(ms);
226 } catch (InterruptedException e) {
227 throw new RuntimeException("Interrupted", e);
228 }
229 }
230
tom53efab52014-10-07 17:43:48 -0700231 /**
Madan Jampania29c6772015-08-17 13:17:07 -0700232 * Returns a function that retries execution on failure.
233 * @param base base function
234 * @param exceptionClass type of exception for which to retry
235 * @param maxRetries max number of retries before giving up
236 * @param maxDelayBetweenRetries max delay between successive retries. The actual delay is randomly picked from
237 * the interval (0, maxDelayBetweenRetries]
238 * @return function
Thomas Vachuska87ae1d92015-08-19 17:39:11 -0700239 * @param <U> type of function input
240 * @param <V> type of function output
Madan Jampania29c6772015-08-17 13:17:07 -0700241 */
242 public static <U, V> Function<U, V> retryable(Function<U, V> base,
243 Class<? extends Throwable> exceptionClass,
244 int maxRetries,
245 int maxDelayBetweenRetries) {
246 return new RetryingFunction<>(base, exceptionClass, maxRetries, maxDelayBetweenRetries);
247 }
248
249 /**
250 * Returns a Supplier that retries execution on failure.
251 * @param base base supplier
252 * @param exceptionClass type of exception for which to retry
253 * @param maxRetries max number of retries before giving up
254 * @param maxDelayBetweenRetries max delay between successive retries. The actual delay is randomly picked from
255 * the interval (0, maxDelayBetweenRetries]
256 * @return supplier
Thomas Vachuska87ae1d92015-08-19 17:39:11 -0700257 * @param <V> type of supplied result
Madan Jampania29c6772015-08-17 13:17:07 -0700258 */
259 public static <V> Supplier<V> retryable(Supplier<V> base,
260 Class<? extends Throwable> exceptionClass,
261 int maxRetries,
262 int maxDelayBetweenRetries) {
263 return () -> new RetryingFunction<>(v -> base.get(),
264 exceptionClass,
265 maxRetries,
266 maxDelayBetweenRetries).apply(null);
267 }
268
269 /**
Thomas Vachuskaadba1522015-06-04 15:08:30 -0700270 * Suspends the current thread for a random number of millis between 0 and
271 * the indicated limit.
272 *
273 * @param ms max number of millis
274 */
275 public static void randomDelay(int ms) {
276 try {
277 Thread.sleep(random.nextInt(ms));
278 } catch (InterruptedException e) {
279 throw new RuntimeException("Interrupted", e);
280 }
281 }
282
283 /**
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700284 * Suspends the current thread for a specified number of millis and nanos.
285 *
286 * @param ms number of millis
287 * @param nanos number of nanos
288 */
289 public static void delay(int ms, int nanos) {
290 try {
291 Thread.sleep(ms, nanos);
292 } catch (InterruptedException e) {
293 throw new RuntimeException("Interrupted", e);
294 }
295 }
296
297 /**
tom53efab52014-10-07 17:43:48 -0700298 * Slurps the contents of a file into a list of strings, one per line.
299 *
300 * @param path file path
301 * @return file contents
302 */
303 public static List<String> slurp(File path) {
Ray Milkey705d9bc2014-11-18 08:19:00 -0800304 try {
305 BufferedReader br = new BufferedReader(
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800306 new InputStreamReader(new FileInputStream(path), StandardCharsets.UTF_8));
Ray Milkey705d9bc2014-11-18 08:19:00 -0800307
tom53efab52014-10-07 17:43:48 -0700308 List<String> lines = new ArrayList<>();
309 String line;
310 while ((line = br.readLine()) != null) {
311 lines.add(line);
312 }
313 return lines;
314
315 } catch (IOException e) {
316 return null;
317 }
318 }
319
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800320 /**
321 * Purges the specified directory path.&nbsp;Use with great caution since
322 * no attempt is made to check for symbolic links, which could result in
323 * deletion of unintended files.
324 *
325 * @param path directory to be removed
326 * @throws java.io.IOException if unable to remove contents
327 */
328 public static void removeDirectory(String path) throws IOException {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800329 DirectoryDeleter visitor = new DirectoryDeleter();
Thomas Vachuskaf9c84362015-04-15 11:20:45 -0700330 File dir = new File(path);
331 if (dir.exists() && dir.isDirectory()) {
332 walkFileTree(Paths.get(path), visitor);
333 if (visitor.exception != null) {
334 throw visitor.exception;
335 }
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800336 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800337 }
338
339 /**
340 * Purges the specified directory path.&nbsp;Use with great caution since
341 * no attempt is made to check for symbolic links, which could result in
342 * deletion of unintended files.
343 *
344 * @param dir directory to be removed
345 * @throws java.io.IOException if unable to remove contents
346 */
347 public static void removeDirectory(File dir) throws IOException {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800348 DirectoryDeleter visitor = new DirectoryDeleter();
Thomas Vachuskaf9c84362015-04-15 11:20:45 -0700349 if (dir.exists() && dir.isDirectory()) {
350 walkFileTree(Paths.get(dir.getAbsolutePath()), visitor);
351 if (visitor.exception != null) {
352 throw visitor.exception;
353 }
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800354 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800355 }
356
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800357 // Auxiliary path visitor for recursive directory structure removal.
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800358 private static class DirectoryDeleter extends SimpleFileVisitor<Path> {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800359
360 private IOException exception;
361
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800362 @Override
363 public FileVisitResult visitFile(Path file, BasicFileAttributes attributes)
364 throws IOException {
365 if (attributes.isRegularFile()) {
366 delete(file);
367 }
368 return FileVisitResult.CONTINUE;
369 }
370
371 @Override
372 public FileVisitResult postVisitDirectory(Path directory, IOException ioe)
373 throws IOException {
374 delete(directory);
375 return FileVisitResult.CONTINUE;
376 }
377
378 @Override
379 public FileVisitResult visitFileFailed(Path file, IOException ioe)
380 throws IOException {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800381 this.exception = ioe;
382 return FileVisitResult.TERMINATE;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800383 }
384 }
385
Madan Jampani30a57f82015-03-02 12:19:41 -0800386 /**
387 * Returns a human friendly time ago string for a specified system time.
Thomas Vachuska480adad2015-03-06 10:27:09 -0800388 *
Madan Jampani30a57f82015-03-02 12:19:41 -0800389 * @param unixTime system time in millis
390 * @return human friendly time ago
391 */
392 public static String timeAgo(long unixTime) {
393 long deltaMillis = System.currentTimeMillis() - unixTime;
394 long secondsSince = (long) (deltaMillis / 1000.0);
395 long minsSince = (long) (deltaMillis / (1000.0 * 60));
396 long hoursSince = (long) (deltaMillis / (1000.0 * 60 * 60));
397 long daysSince = (long) (deltaMillis / (1000.0 * 60 * 60 * 24));
398 if (daysSince > 0) {
399 return String.format("%dd ago", daysSince);
400 } else if (hoursSince > 0) {
401 return String.format("%dh ago", hoursSince);
402 } else if (minsSince > 0) {
403 return String.format("%dm ago", minsSince);
404 } else if (secondsSince > 0) {
405 return String.format("%ds ago", secondsSince);
406 } else {
407 return "just now";
408 }
409 }
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800410
411 /**
412 * Copies the specified directory path.&nbsp;Use with great caution since
413 * no attempt is made to check for symbolic links, which could result in
414 * copy of unintended files.
415 *
416 * @param src directory to be copied
417 * @param dst destination directory to be removed
418 * @throws java.io.IOException if unable to remove contents
419 */
420 public static void copyDirectory(String src, String dst) throws IOException {
421 walkFileTree(Paths.get(src), new DirectoryCopier(src, dst));
422 }
423
424 /**
425 * Copies the specified directory path.&nbsp;Use with great caution since
426 * no attempt is made to check for symbolic links, which could result in
427 * copy of unintended files.
428 *
429 * @param src directory to be copied
430 * @param dst destination directory to be removed
431 * @throws java.io.IOException if unable to remove contents
432 */
433 public static void copyDirectory(File src, File dst) throws IOException {
434 walkFileTree(Paths.get(src.getAbsolutePath()),
435 new DirectoryCopier(src.getAbsolutePath(),
436 dst.getAbsolutePath()));
437 }
438
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700439 /**
440 * Returns the future value when complete or if future
441 * completes exceptionally returns the defaultValue.
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700442 *
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700443 * @param future future
444 * @param defaultValue default value
445 * @param <T> future value type
446 * @return future value when complete or if future
447 * completes exceptionally returns the defaultValue.
448 */
449 public static <T> T futureGetOrElse(Future<T> future, T defaultValue) {
450 try {
451 return future.get();
452 } catch (InterruptedException e) {
453 Thread.currentThread().interrupt();
454 return defaultValue;
455 } catch (ExecutionException e) {
456 return defaultValue;
457 }
458 }
459
460 /**
461 * Returns the future value when complete or if future
462 * completes exceptionally returns the defaultValue.
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700463 *
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700464 * @param future future
465 * @param timeout time to wait for successful completion
466 * @param timeUnit time unit
467 * @param defaultValue default value
468 * @param <T> future value type
469 * @return future value when complete or if future
470 * completes exceptionally returns the defaultValue.
471 */
472 public static <T> T futureGetOrElse(Future<T> future,
473 long timeout,
474 TimeUnit timeUnit,
475 T defaultValue) {
476 try {
477 return future.get(timeout, timeUnit);
478 } catch (InterruptedException e) {
479 Thread.currentThread().interrupt();
480 return defaultValue;
481 } catch (ExecutionException | TimeoutException e) {
482 return defaultValue;
483 }
484 }
485
Madan Jampani27b69c62015-05-15 15:49:02 -0700486 /**
487 * Returns a future that is completed exceptionally.
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700488 *
Madan Jampani27b69c62015-05-15 15:49:02 -0700489 * @param t exception
490 * @param <T> future value type
491 * @return future
492 */
493 public static <T> CompletableFuture<T> exceptionalFuture(Throwable t) {
494 CompletableFuture<T> future = new CompletableFuture<>();
495 future.completeExceptionally(t);
496 return future;
497 }
498
499 /**
500 * Returns the contents of {@code ByteBuffer} as byte array.
501 * <p>
502 * WARNING: There is a performance cost due to array copy
503 * when using this method.
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700504 *
Madan Jampani27b69c62015-05-15 15:49:02 -0700505 * @param buffer byte buffer
506 * @return byte array containing the byte buffer contents
507 */
508 public static byte[] byteBuffertoArray(ByteBuffer buffer) {
509 int length = buffer.remaining();
510 if (buffer.hasArray()) {
511 int offset = buffer.arrayOffset() + buffer.position();
512 return Arrays.copyOfRange(buffer.array(), offset, offset + length);
513 }
514 byte[] bytes = new byte[length];
515 buffer.duplicate().get(bytes);
516 return bytes;
517 }
518
HIGUCHI Yutabfc8b7a2015-07-01 23:47:43 -0700519 /**
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700520 * Converts an iterable to a stream.
HIGUCHI Yutabfc8b7a2015-07-01 23:47:43 -0700521 *
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700522 * @param it iterable to convert
523 * @param <T> type if item
524 * @return iterable as a stream
HIGUCHI Yutabfc8b7a2015-07-01 23:47:43 -0700525 */
526 public static <T> Stream<T> stream(Iterable<T> it) {
527 return StreamSupport.stream(it.spliterator(), false);
528 }
529
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800530 // Auxiliary path visitor for recursive directory structure copying.
531 private static class DirectoryCopier extends SimpleFileVisitor<Path> {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800532 private Path src;
533 private Path dst;
534 private StandardCopyOption copyOption = StandardCopyOption.REPLACE_EXISTING;
535
536 DirectoryCopier(String src, String dst) {
537 this.src = Paths.get(src);
538 this.dst = Paths.get(dst);
539 }
540
541 @Override
542 public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
543 Path targetPath = dst.resolve(src.relativize(dir));
544 if (!Files.exists(targetPath)) {
545 Files.createDirectory(targetPath);
546 }
547 return FileVisitResult.CONTINUE;
548 }
549
550 @Override
551 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
552 Files.copy(file, dst.resolve(src.relativize(file)), copyOption);
553 return FileVisitResult.CONTINUE;
554 }
555 }
556
tom5f38b3a2014-08-27 23:50:54 -0700557}