|
1 /* |
|
2 * Copyright (c) 2010 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 import java.io.*; |
|
33 import java.nio.*; |
|
34 import java.nio.channels.*; |
|
35 import java.nio.file.*; |
|
36 import java.nio.file.attribute.*; |
|
37 import java.net.*; |
|
38 import java.text.DateFormat; |
|
39 import java.text.SimpleDateFormat; |
|
40 import java.util.*; |
|
41 |
|
42 import static java.nio.file.StandardOpenOption.*; |
|
43 import static java.nio.file.StandardCopyOption.*; |
|
44 |
|
45 /* |
|
46 * ZipFileSystem usage demo |
|
47 * |
|
48 * java Demo action ZipfileName [...] |
|
49 * |
|
50 * @author Xueming Shen |
|
51 */ |
|
52 |
|
53 public class Demo { |
|
54 |
|
55 static enum Action { |
|
56 rename, // <java Demo rename zipfile src dst> |
|
57 // rename entry src to dst inside zipfile |
|
58 |
|
59 movein, // <java Demo movein zipfile src dst> |
|
60 // move an external src file into zipfile |
|
61 // as entry dst |
|
62 |
|
63 moveout, // <java Demo moveout zipfile src dst> |
|
64 // move a zipfile entry src out to dst |
|
65 |
|
66 copy, // <java Demo copy zipfile src dst> |
|
67 // copy entry src to dst inside zipfile |
|
68 |
|
69 copyin, // <java Demo copyin zipfile src dst> |
|
70 // copy an external src file into zipfile |
|
71 // as entry dst |
|
72 |
|
73 copyin_attrs, // <java Demo copyin_attrs zipfile src dst> |
|
74 // copy an external src file into zipfile |
|
75 // as entry dst, with attributes (timestamp) |
|
76 |
|
77 copyout, // <java Demo copyout zipfile src dst> |
|
78 // copy zipfile entry src" out to file dst |
|
79 |
|
80 copyout_attrs, // <java Demo copyout_attrs zipfile src dst> |
|
81 |
|
82 zzmove, // <java Demo zzmove zfsrc zfdst path> |
|
83 // move entry path/dir from zfsrc to zfdst |
|
84 |
|
85 zzcopy, // <java Demo zzcopy zfsrc zfdst path> |
|
86 // copy path from zipfile zfsrc to zipfile |
|
87 // zfdst |
|
88 |
|
89 attrs, // <java Demo attrs zipfile path> |
|
90 // printout the attributes of entry path |
|
91 |
|
92 attrsspace, // <java Demo attrsspace zipfile path> |
|
93 // printout the storespace attrs of entry path |
|
94 |
|
95 setmtime, // <java Demo setmtime zipfile "MM/dd/yy-HH:mm:ss" path...> |
|
96 // set the lastModifiedTime of entry path |
|
97 |
|
98 setatime, // <java Demo setatime zipfile "MM/dd/yy-HH:mm:ss" path...> |
|
99 setctime, // <java Demo setctime zipfile "MM/dd/yy-HH:mm:ss" path...> |
|
100 |
|
101 lsdir, // <java Demo lsdir zipfile dir> |
|
102 // list dir's direct child files/dirs |
|
103 |
|
104 mkdir, // <java Demo mkdir zipfile dir> |
|
105 |
|
106 mkdirs, // <java Demo mkdirs zipfile dir> |
|
107 |
|
108 rmdirs, // <java Demo rmdirs zipfile dir> |
|
109 |
|
110 list, // <java Demo list zipfile [dir]> |
|
111 // recursively list all entries of dir |
|
112 // via DirectoryStream |
|
113 |
|
114 tlist, // <java Demo tlist zipfile [dir]> |
|
115 // list with buildDirTree=true |
|
116 |
|
117 vlist, // <java Demo vlist zipfile [dir]> |
|
118 // recursively verbose list all entries of |
|
119 // dir via DirectoryStream |
|
120 |
|
121 walk, // <java Demo walk zipfile [dir]> |
|
122 // recursively walk all entries of dir |
|
123 // via Files.walkFileTree |
|
124 |
|
125 twalk, // <java Demo twalk zipfile [dir]> |
|
126 // walk with buildDirTree=true |
|
127 |
|
128 extract, // <java Demo extract zipfile file [...]> |
|
129 |
|
130 update, // <java Demo extract zipfile file [...]> |
|
131 |
|
132 delete, // <java Demo delete zipfile file [...]> |
|
133 |
|
134 add, // <java Demo add zipfile file [...]> |
|
135 |
|
136 create, // <java Demo create zipfile file [...]> |
|
137 // create a new zipfile if it doesn't exit |
|
138 // and then add the file(s) into it. |
|
139 |
|
140 attrs2, // <java Demo attrs2 zipfile file [...]> |
|
141 // test different ways to print attrs |
|
142 |
|
143 prof, |
|
144 } |
|
145 |
|
146 public static void main(String[] args) throws Throwable { |
|
147 |
|
148 Action action = Action.valueOf(args[0]); |
|
149 Map<String, Object> env = env = new HashMap<>(); |
|
150 if (action == Action.create) |
|
151 env.put("create", "true"); |
|
152 if (action == Action.tlist || action == Action.twalk) |
|
153 env.put("buildDirTree", true); |
|
154 FileSystem fs = FileSystems.newFileSystem(Paths.get(args[1]), env, null); |
|
155 |
|
156 try { |
|
157 FileSystem fs2; |
|
158 Path path, src, dst; |
|
159 boolean isRename = false; |
|
160 switch (action) { |
|
161 case rename: |
|
162 src = fs.getPath(args[2]); |
|
163 dst = fs.getPath(args[3]); |
|
164 src.moveTo(dst); |
|
165 break; |
|
166 case moveout: |
|
167 src = fs.getPath(args[2]); |
|
168 dst = Paths.get(args[3]); |
|
169 src.moveTo(dst); |
|
170 break; |
|
171 case movein: |
|
172 src = Paths.get(args[2]); |
|
173 dst = fs.getPath(args[3]); |
|
174 src.moveTo(dst); |
|
175 break; |
|
176 case copy: |
|
177 src = fs.getPath(args[2]); |
|
178 dst = fs.getPath(args[3]); |
|
179 src.copyTo(dst); |
|
180 break; |
|
181 case copyout: |
|
182 src = fs.getPath(args[2]); |
|
183 dst = Paths.get(args[3]); |
|
184 src.copyTo(dst); |
|
185 break; |
|
186 case copyin: |
|
187 src = Paths.get(args[2]); |
|
188 dst = fs.getPath(args[3]); |
|
189 src.copyTo(dst); |
|
190 break; |
|
191 case copyin_attrs: |
|
192 src = Paths.get(args[2]); |
|
193 dst = fs.getPath(args[3]); |
|
194 src.copyTo(dst, COPY_ATTRIBUTES); |
|
195 break; |
|
196 case copyout_attrs: |
|
197 src = fs.getPath(args[2]); |
|
198 dst = Paths.get(args[3]); |
|
199 src.copyTo(dst, COPY_ATTRIBUTES); |
|
200 break; |
|
201 case zzmove: |
|
202 fs2 = FileSystems.newFileSystem(Paths.get(args[2]), env, null); |
|
203 //sf1.getPath(args[3]).moveTo(fs2.getPath(args[3])); |
|
204 z2zmove(fs, fs2, args[3]); |
|
205 fs2.close(); |
|
206 break; |
|
207 case zzcopy: |
|
208 fs2 = FileSystems.newFileSystem(Paths.get(args[2]), env, null); |
|
209 //sf1.getPath(args[3]).copyTo(fs2.getPath(args[3])); |
|
210 z2zcopy(fs, fs2, args[3]); |
|
211 fs2.close(); |
|
212 break; |
|
213 case attrs: |
|
214 for (int i = 2; i < args.length; i++) { |
|
215 path = fs.getPath(args[i]); |
|
216 System.out.println(path); |
|
217 System.out.println( |
|
218 Attributes.readBasicFileAttributes(path).toString()); |
|
219 } |
|
220 break; |
|
221 case setmtime: |
|
222 DateFormat df = new SimpleDateFormat("MM/dd/yyyy-HH:mm:ss"); |
|
223 Date newDatetime = df.parse(args[2]); |
|
224 for (int i = 3; i < args.length; i++) { |
|
225 path = fs.getPath(args[i]); |
|
226 path.setAttribute("lastModifiedTime", |
|
227 FileTime.fromMillis(newDatetime.getTime())); |
|
228 System.out.println( |
|
229 Attributes.readBasicFileAttributes(path).toString()); |
|
230 } |
|
231 break; |
|
232 case setctime: |
|
233 df = new SimpleDateFormat("MM/dd/yyyy-HH:mm:ss"); |
|
234 newDatetime = df.parse(args[2]); |
|
235 for (int i = 3; i < args.length; i++) { |
|
236 path = fs.getPath(args[i]); |
|
237 path.setAttribute("creationTime", |
|
238 FileTime.fromMillis(newDatetime.getTime())); |
|
239 System.out.println( |
|
240 Attributes.readBasicFileAttributes(path).toString()); |
|
241 } |
|
242 break; |
|
243 case setatime: |
|
244 df = new SimpleDateFormat("MM/dd/yyyy-HH:mm:ss"); |
|
245 newDatetime = df.parse(args[2]); |
|
246 for (int i = 3; i < args.length; i++) { |
|
247 path = fs.getPath(args[i]); |
|
248 path.setAttribute("lastAccessTime", |
|
249 FileTime.fromMillis(newDatetime.getTime())); |
|
250 System.out.println( |
|
251 Attributes.readBasicFileAttributes(path).toString()); |
|
252 } |
|
253 break; |
|
254 case attrsspace: |
|
255 path = fs.getPath("/"); |
|
256 FileStore fstore = path.getFileStore(); |
|
257 //System.out.println(fstore.getFileStoreAttributeView(FileStoreSpaceAttributeView.class) |
|
258 // .readAttributes()); |
|
259 // or |
|
260 System.out.printf("filestore[%s]%n", fstore.name()); |
|
261 System.out.printf(" totalSpace: %d%n", |
|
262 (Long)fstore.getAttribute("space:totalSpace")); |
|
263 System.out.printf(" usableSpace: %d%n", |
|
264 (Long)fstore.getAttribute("space:usableSpace")); |
|
265 System.out.printf(" unallocSpace: %d%n", |
|
266 (Long)fstore.getAttribute("space:unallocatedSpace")); |
|
267 break; |
|
268 case list: |
|
269 case tlist: |
|
270 if (args.length < 3) |
|
271 list(fs.getPath("/"), false); |
|
272 else |
|
273 list(fs.getPath(args[2]), false); |
|
274 break; |
|
275 case vlist: |
|
276 if (args.length < 3) |
|
277 list(fs.getPath("/"), true); |
|
278 else |
|
279 list(fs.getPath(args[2]), true); |
|
280 break; |
|
281 case twalk: |
|
282 case walk: |
|
283 walk(fs.getPath((args.length > 2)? args[2] : "/")); |
|
284 break; |
|
285 case extract: |
|
286 if (args.length == 2) { |
|
287 extract(fs, "/"); |
|
288 } else { |
|
289 for (int i = 2; i < args.length; i++) { |
|
290 extract(fs, args[i]); |
|
291 } |
|
292 } |
|
293 break; |
|
294 case delete: |
|
295 for (int i = 2; i < args.length; i++) |
|
296 fs.getPath(args[i]).delete(); |
|
297 break; |
|
298 case create: |
|
299 case add: |
|
300 case update: |
|
301 for (int i = 2; i < args.length; i++) { |
|
302 update(fs, args[i]); |
|
303 } |
|
304 break; |
|
305 case lsdir: |
|
306 path = fs.getPath(args[2]); |
|
307 final String fStr = (args.length > 3)?args[3]:""; |
|
308 DirectoryStream<Path> ds = path.newDirectoryStream( |
|
309 new DirectoryStream.Filter<Path>() { |
|
310 public boolean accept(Path path) { |
|
311 return path.toString().contains(fStr); |
|
312 } |
|
313 }); |
|
314 for (Path p : ds) |
|
315 System.out.println(p); |
|
316 break; |
|
317 case mkdir: |
|
318 fs.getPath(args[2]).createDirectory(); |
|
319 break; |
|
320 case mkdirs: |
|
321 mkdirs(fs.getPath(args[2])); |
|
322 break; |
|
323 case attrs2: |
|
324 for (int i = 2; i < args.length; i++) { |
|
325 path = fs.getPath(args[i]); |
|
326 System.out.printf("%n%s%n", path); |
|
327 System.out.println("-------(1)---------"); |
|
328 System.out.println( |
|
329 Attributes.readBasicFileAttributes(path).toString()); |
|
330 System.out.println("-------(2)---------"); |
|
331 Map<String, ?> map = path.readAttributes("zip:*"); |
|
332 for (Map.Entry<String, ?> e : map.entrySet()) { |
|
333 System.out.printf(" %s : %s%n", e.getKey(), e.getValue()); |
|
334 } |
|
335 System.out.println("-------(3)---------"); |
|
336 map = path.readAttributes("size,lastModifiedTime,isDirectory"); |
|
337 for (Map.Entry<String, ?> e : map.entrySet()) { |
|
338 System.out.printf(" %s : %s%n", e.getKey(), e.getValue()); |
|
339 } |
|
340 } |
|
341 break; |
|
342 case prof: |
|
343 list(fs.getPath("/"), false); |
|
344 while (true) { |
|
345 Thread.sleep(10000); |
|
346 //list(fs.getPath("/"), true); |
|
347 System.out.println("sleeping..."); |
|
348 } |
|
349 } |
|
350 } catch (Exception x) { |
|
351 x.printStackTrace(); |
|
352 } finally { |
|
353 if (fs != null) |
|
354 fs.close(); |
|
355 } |
|
356 } |
|
357 |
|
358 private static byte[] getBytes(String name) { |
|
359 return name.getBytes(); |
|
360 } |
|
361 |
|
362 private static String getString(byte[] name) { |
|
363 return new String(name); |
|
364 } |
|
365 |
|
366 private static void walk(Path path) throws IOException |
|
367 { |
|
368 Files.walkFileTree( |
|
369 path, |
|
370 new SimpleFileVisitor<Path>() { |
|
371 private int indent = 0; |
|
372 private void indent() { |
|
373 int n = 0; |
|
374 while (n++ < indent) |
|
375 System.out.printf(" "); |
|
376 } |
|
377 |
|
378 @Override |
|
379 public FileVisitResult visitFile(Path file, |
|
380 BasicFileAttributes attrs) |
|
381 { |
|
382 indent(); |
|
383 System.out.printf("%s%n", file.getName().toString()); |
|
384 return FileVisitResult.CONTINUE; |
|
385 } |
|
386 |
|
387 @Override |
|
388 public FileVisitResult preVisitDirectory(Path dir, |
|
389 BasicFileAttributes attrs) |
|
390 { |
|
391 indent(); |
|
392 System.out.printf("[%s]%n", dir.toString()); |
|
393 indent += 2; |
|
394 return FileVisitResult.CONTINUE; |
|
395 } |
|
396 |
|
397 @Override |
|
398 public FileVisitResult postVisitDirectory(Path dir, |
|
399 IOException ioe) |
|
400 { |
|
401 indent -= 2; |
|
402 return FileVisitResult.CONTINUE; |
|
403 } |
|
404 }); |
|
405 } |
|
406 |
|
407 private static void update(FileSystem fs, String path) throws Throwable{ |
|
408 Path src = FileSystems.getDefault().getPath(path); |
|
409 if (Boolean.TRUE.equals(src.getAttribute("isDirectory"))) { |
|
410 DirectoryStream<Path> ds = src.newDirectoryStream(); |
|
411 for (Path child : ds) |
|
412 update(fs, child.toString()); |
|
413 ds.close(); |
|
414 } else { |
|
415 Path dst = fs.getPath(path); |
|
416 Path parent = dst.getParent(); |
|
417 if (parent != null && parent.notExists()) |
|
418 mkdirs(parent); |
|
419 src.copyTo(dst, REPLACE_EXISTING); |
|
420 } |
|
421 } |
|
422 |
|
423 private static void extract(FileSystem fs, String path) throws Throwable{ |
|
424 Path src = fs.getPath(path); |
|
425 if (Boolean.TRUE.equals(src.getAttribute("isDirectory"))) { |
|
426 DirectoryStream<Path> ds = src.newDirectoryStream(); |
|
427 for (Path child : ds) |
|
428 extract(fs, child.toString()); |
|
429 ds.close(); |
|
430 } else { |
|
431 if (path.startsWith("/")) |
|
432 path = path.substring(1); |
|
433 Path dst = FileSystems.getDefault().getPath(path); |
|
434 Path parent = dst.getParent(); |
|
435 if (parent.notExists()) |
|
436 mkdirs(parent); |
|
437 src.copyTo(dst, REPLACE_EXISTING); |
|
438 } |
|
439 } |
|
440 |
|
441 // use DirectoryStream |
|
442 private static void z2zcopy(FileSystem src, FileSystem dst, String path) |
|
443 throws IOException |
|
444 { |
|
445 Path srcPath = src.getPath(path); |
|
446 Path dstPath = dst.getPath(path); |
|
447 |
|
448 if (Boolean.TRUE.equals(srcPath.getAttribute("isDirectory"))) { |
|
449 if (!dstPath.exists()) { |
|
450 try { |
|
451 mkdirs(dstPath); |
|
452 } catch (FileAlreadyExistsException x) {} |
|
453 } |
|
454 DirectoryStream<Path> ds = srcPath.newDirectoryStream(); |
|
455 for (Path child : ds) { |
|
456 z2zcopy(src, dst, |
|
457 path + (path.endsWith("/")?"":"/") + child.getName()); |
|
458 } |
|
459 ds.close(); |
|
460 } else { |
|
461 //System.out.println("copying..." + path); |
|
462 srcPath.copyTo(dstPath); |
|
463 } |
|
464 } |
|
465 |
|
466 // use TreeWalk to move |
|
467 private static void z2zmove(FileSystem src, FileSystem dst, String path) |
|
468 throws IOException |
|
469 { |
|
470 final Path srcPath = src.getPath(path).toAbsolutePath(); |
|
471 final Path dstPath = dst.getPath(path).toAbsolutePath(); |
|
472 |
|
473 Files.walkFileTree(srcPath, new SimpleFileVisitor<Path>() { |
|
474 |
|
475 @Override |
|
476 public FileVisitResult visitFile(Path file, |
|
477 BasicFileAttributes attrs) |
|
478 { |
|
479 Path dst = srcPath.relativize(file); |
|
480 dst = dstPath.resolve(dst); |
|
481 try { |
|
482 Path parent = dstPath.getParent(); |
|
483 if (parent != null && parent.notExists()) |
|
484 mkdirs(parent); |
|
485 file.moveTo(dst); |
|
486 } catch (IOException x) { |
|
487 x.printStackTrace(); |
|
488 } |
|
489 return FileVisitResult.CONTINUE; |
|
490 } |
|
491 |
|
492 @Override |
|
493 public FileVisitResult preVisitDirectory(Path dir, |
|
494 BasicFileAttributes attrs) |
|
495 { |
|
496 Path dst = srcPath.relativize(dir); |
|
497 dst = dstPath.resolve(dst); |
|
498 try { |
|
499 |
|
500 if (dst.notExists()) |
|
501 mkdirs(dst); |
|
502 } catch (IOException x) { |
|
503 x.printStackTrace(); |
|
504 } |
|
505 return FileVisitResult.CONTINUE; |
|
506 } |
|
507 |
|
508 @Override |
|
509 public FileVisitResult postVisitDirectory(Path dir, |
|
510 IOException ioe) |
|
511 throws IOException |
|
512 { |
|
513 try { |
|
514 dir.delete(); |
|
515 } catch (IOException x) { |
|
516 //x.printStackTrace(); |
|
517 } |
|
518 return FileVisitResult.CONTINUE; |
|
519 } |
|
520 }); |
|
521 |
|
522 } |
|
523 |
|
524 private static void mkdirs(Path path) throws IOException { |
|
525 path = path.toAbsolutePath(); |
|
526 Path parent = path.getParent(); |
|
527 if (parent != null) { |
|
528 if (parent.notExists()) |
|
529 mkdirs(parent); |
|
530 } |
|
531 path.createDirectory(); |
|
532 } |
|
533 |
|
534 private static void rmdirs(Path path) throws IOException { |
|
535 while (path != null && path.getNameCount() != 0) { |
|
536 path.delete(); |
|
537 path = path.getParent(); |
|
538 } |
|
539 } |
|
540 |
|
541 private static void list(Path path, boolean verbose ) throws IOException { |
|
542 if (!"/".equals(path.toString())) { |
|
543 System.out.printf(" %s%n", path.toString()); |
|
544 if (verbose) |
|
545 System.out.println(Attributes.readBasicFileAttributes(path).toString()); |
|
546 } |
|
547 if (path.notExists()) |
|
548 return; |
|
549 if (Attributes.readBasicFileAttributes(path).isDirectory()) { |
|
550 DirectoryStream<Path> ds = path.newDirectoryStream(); |
|
551 for (Path child : ds) |
|
552 list(child, verbose); |
|
553 ds.close(); |
|
554 } |
|
555 } |
|
556 |
|
557 // check the content of two paths are equal |
|
558 private static void checkEqual(Path src, Path dst) throws IOException |
|
559 { |
|
560 //System.out.printf("checking <%s> vs <%s>...%n", |
|
561 // src.toString(), dst.toString()); |
|
562 |
|
563 //streams |
|
564 InputStream isSrc = src.newInputStream(); |
|
565 InputStream isDst = dst.newInputStream(); |
|
566 byte[] bufSrc = new byte[8192]; |
|
567 byte[] bufDst = new byte[8192]; |
|
568 |
|
569 try { |
|
570 int nSrc = 0; |
|
571 while ((nSrc = isSrc.read(bufSrc)) != -1) { |
|
572 int nDst = 0; |
|
573 while (nDst < nSrc) { |
|
574 int n = isDst.read(bufDst, nDst, nSrc - nDst); |
|
575 if (n == -1) { |
|
576 System.out.printf("checking <%s> vs <%s>...%n", |
|
577 src.toString(), dst.toString()); |
|
578 throw new RuntimeException("CHECK FAILED!"); |
|
579 } |
|
580 nDst += n; |
|
581 } |
|
582 while (--nSrc >= 0) { |
|
583 if (bufSrc[nSrc] != bufDst[nSrc]) { |
|
584 System.out.printf("checking <%s> vs <%s>...%n", |
|
585 src.toString(), dst.toString()); |
|
586 throw new RuntimeException("CHECK FAILED!"); |
|
587 } |
|
588 nSrc--; |
|
589 } |
|
590 } |
|
591 } finally { |
|
592 isSrc.close(); |
|
593 isDst.close(); |
|
594 } |
|
595 |
|
596 // channels |
|
597 SeekableByteChannel chSrc = src.newByteChannel(); |
|
598 SeekableByteChannel chDst = dst.newByteChannel(); |
|
599 if (chSrc.size() != chDst.size()) { |
|
600 System.out.printf("src[%s].size=%d, dst[%s].size=%d%n", |
|
601 chSrc.toString(), chSrc.size(), |
|
602 chDst.toString(), chDst.size()); |
|
603 throw new RuntimeException("CHECK FAILED!"); |
|
604 } |
|
605 ByteBuffer bbSrc = ByteBuffer.allocate(8192); |
|
606 ByteBuffer bbDst = ByteBuffer.allocate(8192); |
|
607 |
|
608 try { |
|
609 int nSrc = 0; |
|
610 while ((nSrc = chSrc.read(bbSrc)) != -1) { |
|
611 int nDst = chDst.read(bbDst); |
|
612 if (nSrc != nDst) { |
|
613 System.out.printf("checking <%s> vs <%s>...%n", |
|
614 src.toString(), dst.toString()); |
|
615 throw new RuntimeException("CHECK FAILED!"); |
|
616 } |
|
617 while (--nSrc >= 0) { |
|
618 if (bbSrc.get(nSrc) != bbDst.get(nSrc)) { |
|
619 System.out.printf("checking <%s> vs <%s>...%n", |
|
620 src.toString(), dst.toString()); |
|
621 throw new RuntimeException("CHECK FAILED!"); |
|
622 } |
|
623 nSrc--; |
|
624 } |
|
625 bbSrc.flip(); |
|
626 bbDst.flip(); |
|
627 } |
|
628 } catch (IOException x) { |
|
629 x.printStackTrace(); |
|
630 } finally { |
|
631 chSrc.close(); |
|
632 chDst.close(); |
|
633 } |
|
634 } |
|
635 |
|
636 private static void fchCopy(Path src, Path dst) throws IOException |
|
637 { |
|
638 Set<OpenOption> read = new HashSet<>(); |
|
639 read.add(READ); |
|
640 Set<OpenOption> openwrite = new HashSet<>(); |
|
641 openwrite.add(CREATE_NEW); |
|
642 openwrite.add(WRITE); |
|
643 |
|
644 FileChannel srcFc = src.getFileSystem() |
|
645 .provider() |
|
646 .newFileChannel(src, read); |
|
647 FileChannel dstFc = dst.getFileSystem() |
|
648 .provider() |
|
649 .newFileChannel(dst, openwrite); |
|
650 |
|
651 try { |
|
652 ByteBuffer bb = ByteBuffer.allocate(8192); |
|
653 while (srcFc.read(bb) >= 0) { |
|
654 bb.flip(); |
|
655 dstFc.write(bb); |
|
656 bb.clear(); |
|
657 } |
|
658 } finally { |
|
659 srcFc.close(); |
|
660 dstFc.close(); |
|
661 } |
|
662 } |
|
663 |
|
664 private static void chCopy(Path src, Path dst) throws IOException |
|
665 { |
|
666 Set<OpenOption> read = new HashSet<>(); |
|
667 read.add(READ); |
|
668 Set<OpenOption> openwrite = new HashSet<>(); |
|
669 openwrite.add(CREATE_NEW); |
|
670 openwrite.add(WRITE); |
|
671 |
|
672 SeekableByteChannel srcCh = src.newByteChannel(read); |
|
673 SeekableByteChannel dstCh = dst.newByteChannel(openwrite); |
|
674 |
|
675 try { |
|
676 ByteBuffer bb = ByteBuffer.allocate(8192); |
|
677 while (srcCh.read(bb) >= 0) { |
|
678 bb.flip(); |
|
679 dstCh.write(bb); |
|
680 bb.clear(); |
|
681 } |
|
682 } finally { |
|
683 srcCh.close(); |
|
684 dstCh.close(); |
|
685 } |
|
686 } |
|
687 |
|
688 private static void streamCopy(Path src, Path dst) throws IOException |
|
689 { |
|
690 InputStream isSrc = src.newInputStream(); |
|
691 OutputStream osDst = dst.newOutputStream(); |
|
692 byte[] buf = new byte[8192]; |
|
693 try { |
|
694 int n = 0; |
|
695 while ((n = isSrc.read(buf)) != -1) { |
|
696 osDst.write(buf, 0, n); |
|
697 } |
|
698 } finally { |
|
699 isSrc.close(); |
|
700 osDst.close(); |
|
701 } |
|
702 } |
|
703 } |