Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 16 | package org.onlab.util; |
| 17 | |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 18 | import static java.nio.file.Files.delete; |
| 19 | import static java.nio.file.Files.walkFileTree; |
| 20 | import static org.onlab.util.GroupedThreadFactory.groupedThreadFactory; |
| 21 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | 683e978 | 2014-11-25 17:26:36 -0800 | [diff] [blame] | 22 | |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 23 | import java.io.BufferedReader; |
| 24 | import java.io.File; |
Ray Milkey | 705d9bc | 2014-11-18 08:19:00 -0800 | [diff] [blame] | 25 | import java.io.FileInputStream; |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 26 | import java.io.IOException; |
Ray Milkey | 705d9bc | 2014-11-18 08:19:00 -0800 | [diff] [blame] | 27 | import java.io.InputStreamReader; |
| 28 | import java.nio.charset.StandardCharsets; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 29 | import java.nio.file.FileVisitResult; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 30 | import java.nio.file.Files; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 31 | import java.nio.file.Path; |
| 32 | import java.nio.file.Paths; |
| 33 | import java.nio.file.SimpleFileVisitor; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 34 | import java.nio.file.StandardCopyOption; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 35 | import java.nio.file.attribute.BasicFileAttributes; |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 36 | import java.util.ArrayList; |
Brian O'Connor | e2eac10 | 2015-02-12 18:30:22 -0800 | [diff] [blame] | 37 | import java.util.Collection; |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 38 | import java.util.Dictionary; |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 39 | import java.util.List; |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 40 | import java.util.concurrent.ExecutionException; |
| 41 | import java.util.concurrent.Future; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 42 | import java.util.concurrent.ThreadFactory; |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 43 | import java.util.concurrent.TimeUnit; |
| 44 | import java.util.concurrent.TimeoutException; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 45 | |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 46 | import org.slf4j.Logger; |
| 47 | |
| 48 | import com.google.common.base.Strings; |
| 49 | import com.google.common.primitives.UnsignedLongs; |
| 50 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
Ray Milkey | 705d9bc | 2014-11-18 08:19:00 -0800 | [diff] [blame] | 51 | |
Thomas Vachuska | c13b90a | 2015-02-18 18:19:55 -0800 | [diff] [blame] | 52 | /** |
| 53 | * Miscellaneous utility methods. |
| 54 | */ |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 55 | public abstract class Tools { |
| 56 | |
| 57 | private Tools() { |
| 58 | } |
| 59 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 60 | private static final Logger log = getLogger(Tools.class); |
Yuta HIGUCHI | 683e978 | 2014-11-25 17:26:36 -0800 | [diff] [blame] | 61 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 62 | /** |
| 63 | * Returns a thread factory that produces threads named according to the |
| 64 | * supplied name pattern. |
| 65 | * |
| 66 | * @param pattern name pattern |
| 67 | * @return thread factory |
| 68 | */ |
| 69 | public static ThreadFactory namedThreads(String pattern) { |
Yuta HIGUCHI | 683e978 | 2014-11-25 17:26:36 -0800 | [diff] [blame] | 70 | return new ThreadFactoryBuilder() |
| 71 | .setNameFormat(pattern) |
Thomas Vachuska | 480adad | 2015-03-06 10:27:09 -0800 | [diff] [blame] | 72 | .setUncaughtExceptionHandler((t, e) -> log.error("Uncaught exception on " + t.getName(), e)) |
| 73 | .build(); |
Thomas Vachuska | 9c17a6d | 2015-02-17 23:36:43 -0800 | [diff] [blame] | 74 | } |
Yuta HIGUCHI | 683e978 | 2014-11-25 17:26:36 -0800 | [diff] [blame] | 75 | |
Thomas Vachuska | 9c17a6d | 2015-02-17 23:36:43 -0800 | [diff] [blame] | 76 | /** |
| 77 | * Returns a thread factory that produces threads named according to the |
| 78 | * supplied name pattern and from the specified thread-group. The thread |
| 79 | * group name is expected to be specified in slash-delimited format, e.g. |
Thomas Vachuska | c13b90a | 2015-02-18 18:19:55 -0800 | [diff] [blame] | 80 | * {@code onos/intent}. The thread names will be produced by converting |
| 81 | * the thread group name into dash-delimited format and pre-pended to the |
| 82 | * specified pattern. |
Thomas Vachuska | 9c17a6d | 2015-02-17 23:36:43 -0800 | [diff] [blame] | 83 | * |
| 84 | * @param groupName group name in slash-delimited format to indicate hierarchy |
| 85 | * @param pattern name pattern |
| 86 | * @return thread factory |
| 87 | */ |
| 88 | public static ThreadFactory groupedThreads(String groupName, String pattern) { |
| 89 | return new ThreadFactoryBuilder() |
| 90 | .setThreadFactory(groupedThreadFactory(groupName)) |
Thomas Vachuska | c13b90a | 2015-02-18 18:19:55 -0800 | [diff] [blame] | 91 | .setNameFormat(groupName.replace(GroupedThreadFactory.DELIMITER, "-") + "-" + pattern) |
Thomas Vachuska | 480adad | 2015-03-06 10:27:09 -0800 | [diff] [blame] | 92 | .setUncaughtExceptionHandler((t, e) -> log.error("Uncaught exception on " + t.getName(), e)) |
| 93 | .build(); |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 94 | } |
| 95 | |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 96 | /** |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 97 | * Returns a thread factory that produces threads with MIN_PRIORITY. |
| 98 | * |
| 99 | * @param factory backing ThreadFactory |
| 100 | * @return thread factory |
| 101 | */ |
| 102 | public static ThreadFactory minPriority(ThreadFactory factory) { |
| 103 | return new ThreadFactoryBuilder() |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 104 | .setThreadFactory(factory) |
| 105 | .setPriority(Thread.MIN_PRIORITY) |
| 106 | .build(); |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
Brian O'Connor | e2eac10 | 2015-02-12 18:30:22 -0800 | [diff] [blame] | 110 | * Returns true if the collection is null or is empty. |
| 111 | * |
| 112 | * @param collection collection to test |
| 113 | * @return true if null or empty; false otherwise |
| 114 | */ |
| 115 | public static boolean isNullOrEmpty(Collection collection) { |
| 116 | return collection == null || collection.isEmpty(); |
| 117 | } |
| 118 | |
| 119 | /** |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 120 | * Returns the specified item if that items is null; otherwise throws |
| 121 | * not found exception. |
| 122 | * |
| 123 | * @param item item to check |
| 124 | * @param message not found message |
| 125 | * @param <T> item type |
| 126 | * @return item if not null |
| 127 | * @throws org.onlab.util.ItemNotFoundException if item is null |
| 128 | */ |
| 129 | public static <T> T nullIsNotFound(T item, String message) { |
| 130 | if (item == null) { |
| 131 | throw new ItemNotFoundException(message); |
| 132 | } |
| 133 | return item; |
| 134 | } |
| 135 | |
| 136 | /** |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 137 | * Converts a string from hex to long. |
| 138 | * |
| 139 | * @param string hex number in string form; sans 0x |
| 140 | * @return long value |
| 141 | */ |
| 142 | public static long fromHex(String string) { |
| 143 | return UnsignedLongs.parseUnsignedLong(string, 16); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Converts a long value to hex string; 16 wide and sans 0x. |
| 148 | * |
| 149 | * @param value long value |
| 150 | * @return hex string |
| 151 | */ |
| 152 | public static String toHex(long value) { |
| 153 | return Strings.padStart(UnsignedLongs.toString(value, 16), 16, '0'); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Converts a long value to hex string; 16 wide and sans 0x. |
| 158 | * |
| 159 | * @param value long value |
| 160 | * @param width string width; zero padded |
| 161 | * @return hex string |
| 162 | */ |
| 163 | public static String toHex(long value, int width) { |
| 164 | return Strings.padStart(UnsignedLongs.toString(value, 16), width, '0'); |
| 165 | } |
tom | f110fff | 2014-09-26 00:38:18 -0700 | [diff] [blame] | 166 | |
| 167 | /** |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 168 | * Get property as a string value. |
| 169 | * |
| 170 | * @param properties properties to be looked up |
| 171 | * @param propertyName the name of the property to look up |
| 172 | * @return value when the propertyName is defined or return null |
| 173 | */ |
| 174 | public static String get(Dictionary<?, ?> properties, String propertyName) { |
| 175 | Object v = properties.get(propertyName); |
| 176 | String s = (v instanceof String) ? (String) v : |
| 177 | v != null ? v.toString() : null; |
| 178 | return Strings.isNullOrEmpty(s) ? null : s.trim(); |
| 179 | } |
| 180 | |
| 181 | /** |
tom | f110fff | 2014-09-26 00:38:18 -0700 | [diff] [blame] | 182 | * Suspends the current thread for a specified number of millis. |
| 183 | * |
| 184 | * @param ms number of millis |
| 185 | */ |
| 186 | public static void delay(int ms) { |
| 187 | try { |
| 188 | Thread.sleep(ms); |
| 189 | } catch (InterruptedException e) { |
| 190 | throw new RuntimeException("Interrupted", e); |
| 191 | } |
| 192 | } |
| 193 | |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 194 | /** |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame^] | 195 | * Suspends the current thread for a specified number of millis and nanos. |
| 196 | * |
| 197 | * @param ms number of millis |
| 198 | * @param nanos number of nanos |
| 199 | */ |
| 200 | public static void delay(int ms, int nanos) { |
| 201 | try { |
| 202 | Thread.sleep(ms, nanos); |
| 203 | } catch (InterruptedException e) { |
| 204 | throw new RuntimeException("Interrupted", e); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /** |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 209 | * Slurps the contents of a file into a list of strings, one per line. |
| 210 | * |
| 211 | * @param path file path |
| 212 | * @return file contents |
| 213 | */ |
| 214 | public static List<String> slurp(File path) { |
Ray Milkey | 705d9bc | 2014-11-18 08:19:00 -0800 | [diff] [blame] | 215 | try { |
| 216 | BufferedReader br = new BufferedReader( |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 217 | new InputStreamReader(new FileInputStream(path), StandardCharsets.UTF_8)); |
Ray Milkey | 705d9bc | 2014-11-18 08:19:00 -0800 | [diff] [blame] | 218 | |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 219 | List<String> lines = new ArrayList<>(); |
| 220 | String line; |
| 221 | while ((line = br.readLine()) != null) { |
| 222 | lines.add(line); |
| 223 | } |
| 224 | return lines; |
| 225 | |
| 226 | } catch (IOException e) { |
| 227 | return null; |
| 228 | } |
| 229 | } |
| 230 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 231 | |
| 232 | /** |
| 233 | * Purges the specified directory path. Use with great caution since |
| 234 | * no attempt is made to check for symbolic links, which could result in |
| 235 | * deletion of unintended files. |
| 236 | * |
| 237 | * @param path directory to be removed |
| 238 | * @throws java.io.IOException if unable to remove contents |
| 239 | */ |
| 240 | public static void removeDirectory(String path) throws IOException { |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 241 | DirectoryDeleter visitor = new DirectoryDeleter(); |
| 242 | walkFileTree(Paths.get(path), visitor); |
| 243 | if (visitor.exception != null) { |
| 244 | throw visitor.exception; |
| 245 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Purges the specified directory path. Use with great caution since |
| 250 | * no attempt is made to check for symbolic links, which could result in |
| 251 | * deletion of unintended files. |
| 252 | * |
| 253 | * @param dir directory to be removed |
| 254 | * @throws java.io.IOException if unable to remove contents |
| 255 | */ |
| 256 | public static void removeDirectory(File dir) throws IOException { |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 257 | DirectoryDeleter visitor = new DirectoryDeleter(); |
| 258 | walkFileTree(Paths.get(dir.getAbsolutePath()), visitor); |
| 259 | if (visitor.exception != null) { |
| 260 | throw visitor.exception; |
| 261 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 264 | // Auxiliary path visitor for recursive directory structure removal. |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 265 | private static class DirectoryDeleter extends SimpleFileVisitor<Path> { |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 266 | |
| 267 | private IOException exception; |
| 268 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 269 | @Override |
| 270 | public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) |
| 271 | throws IOException { |
| 272 | if (attributes.isRegularFile()) { |
| 273 | delete(file); |
| 274 | } |
| 275 | return FileVisitResult.CONTINUE; |
| 276 | } |
| 277 | |
| 278 | @Override |
| 279 | public FileVisitResult postVisitDirectory(Path directory, IOException ioe) |
| 280 | throws IOException { |
| 281 | delete(directory); |
| 282 | return FileVisitResult.CONTINUE; |
| 283 | } |
| 284 | |
| 285 | @Override |
| 286 | public FileVisitResult visitFileFailed(Path file, IOException ioe) |
| 287 | throws IOException { |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 288 | this.exception = ioe; |
| 289 | return FileVisitResult.TERMINATE; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 293 | /** |
| 294 | * Returns a human friendly time ago string for a specified system time. |
Thomas Vachuska | 480adad | 2015-03-06 10:27:09 -0800 | [diff] [blame] | 295 | * |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 296 | * @param unixTime system time in millis |
| 297 | * @return human friendly time ago |
| 298 | */ |
| 299 | public static String timeAgo(long unixTime) { |
| 300 | long deltaMillis = System.currentTimeMillis() - unixTime; |
| 301 | long secondsSince = (long) (deltaMillis / 1000.0); |
| 302 | long minsSince = (long) (deltaMillis / (1000.0 * 60)); |
| 303 | long hoursSince = (long) (deltaMillis / (1000.0 * 60 * 60)); |
| 304 | long daysSince = (long) (deltaMillis / (1000.0 * 60 * 60 * 24)); |
| 305 | if (daysSince > 0) { |
| 306 | return String.format("%dd ago", daysSince); |
| 307 | } else if (hoursSince > 0) { |
| 308 | return String.format("%dh ago", hoursSince); |
| 309 | } else if (minsSince > 0) { |
| 310 | return String.format("%dm ago", minsSince); |
| 311 | } else if (secondsSince > 0) { |
| 312 | return String.format("%ds ago", secondsSince); |
| 313 | } else { |
| 314 | return "just now"; |
| 315 | } |
| 316 | } |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 317 | |
| 318 | /** |
| 319 | * Copies the specified directory path. Use with great caution since |
| 320 | * no attempt is made to check for symbolic links, which could result in |
| 321 | * copy of unintended files. |
| 322 | * |
| 323 | * @param src directory to be copied |
| 324 | * @param dst destination directory to be removed |
| 325 | * @throws java.io.IOException if unable to remove contents |
| 326 | */ |
| 327 | public static void copyDirectory(String src, String dst) throws IOException { |
| 328 | walkFileTree(Paths.get(src), new DirectoryCopier(src, dst)); |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Copies the specified directory path. Use with great caution since |
| 333 | * no attempt is made to check for symbolic links, which could result in |
| 334 | * copy of unintended files. |
| 335 | * |
| 336 | * @param src directory to be copied |
| 337 | * @param dst destination directory to be removed |
| 338 | * @throws java.io.IOException if unable to remove contents |
| 339 | */ |
| 340 | public static void copyDirectory(File src, File dst) throws IOException { |
| 341 | walkFileTree(Paths.get(src.getAbsolutePath()), |
| 342 | new DirectoryCopier(src.getAbsolutePath(), |
| 343 | dst.getAbsolutePath())); |
| 344 | } |
| 345 | |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 346 | /** |
| 347 | * Returns the future value when complete or if future |
| 348 | * completes exceptionally returns the defaultValue. |
| 349 | * @param future future |
| 350 | * @param defaultValue default value |
| 351 | * @param <T> future value type |
| 352 | * @return future value when complete or if future |
| 353 | * completes exceptionally returns the defaultValue. |
| 354 | */ |
| 355 | public static <T> T futureGetOrElse(Future<T> future, T defaultValue) { |
| 356 | try { |
| 357 | return future.get(); |
| 358 | } catch (InterruptedException e) { |
| 359 | Thread.currentThread().interrupt(); |
| 360 | return defaultValue; |
| 361 | } catch (ExecutionException e) { |
| 362 | return defaultValue; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Returns the future value when complete or if future |
| 368 | * completes exceptionally returns the defaultValue. |
| 369 | * @param future future |
| 370 | * @param timeout time to wait for successful completion |
| 371 | * @param timeUnit time unit |
| 372 | * @param defaultValue default value |
| 373 | * @param <T> future value type |
| 374 | * @return future value when complete or if future |
| 375 | * completes exceptionally returns the defaultValue. |
| 376 | */ |
| 377 | public static <T> T futureGetOrElse(Future<T> future, |
| 378 | long timeout, |
| 379 | TimeUnit timeUnit, |
| 380 | T defaultValue) { |
| 381 | try { |
| 382 | return future.get(timeout, timeUnit); |
| 383 | } catch (InterruptedException e) { |
| 384 | Thread.currentThread().interrupt(); |
| 385 | return defaultValue; |
| 386 | } catch (ExecutionException | TimeoutException e) { |
| 387 | return defaultValue; |
| 388 | } |
| 389 | } |
| 390 | |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 391 | // Auxiliary path visitor for recursive directory structure copying. |
| 392 | private static class DirectoryCopier extends SimpleFileVisitor<Path> { |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 393 | private Path src; |
| 394 | private Path dst; |
| 395 | private StandardCopyOption copyOption = StandardCopyOption.REPLACE_EXISTING; |
| 396 | |
| 397 | DirectoryCopier(String src, String dst) { |
| 398 | this.src = Paths.get(src); |
| 399 | this.dst = Paths.get(dst); |
| 400 | } |
| 401 | |
| 402 | @Override |
| 403 | public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { |
| 404 | Path targetPath = dst.resolve(src.relativize(dir)); |
| 405 | if (!Files.exists(targetPath)) { |
| 406 | Files.createDirectory(targetPath); |
| 407 | } |
| 408 | return FileVisitResult.CONTINUE; |
| 409 | } |
| 410 | |
| 411 | @Override |
| 412 | public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { |
| 413 | Files.copy(file, dst.resolve(src.relativize(file)), copyOption); |
| 414 | return FileVisitResult.CONTINUE; |
| 415 | } |
| 416 | } |
| 417 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 418 | } |