Demo.java revision 2830
2830N/A/*
2830N/A * Copyright (c) 2010 Oracle and/or its affiliates. All rights reserved.
2830N/A *
2830N/A * Redistribution and use in source and binary forms, with or without
2830N/A * modification, are permitted provided that the following conditions
2830N/A * are met:
2830N/A *
2830N/A * - Redistributions of source code must retain the above copyright
2830N/A * notice, this list of conditions and the following disclaimer.
2830N/A *
2830N/A * - Redistributions in binary form must reproduce the above copyright
2830N/A * notice, this list of conditions and the following disclaimer in the
2830N/A * documentation and/or other materials provided with the distribution.
2830N/A *
2830N/A * - Neither the name of Oracle nor the names of its
2830N/A * contributors may be used to endorse or promote products derived
2830N/A * from this software without specific prior written permission.
2830N/A *
2830N/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
2830N/A * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
2830N/A * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2830N/A * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
2830N/A * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2830N/A * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2830N/A * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
2830N/A * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
2830N/A * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2830N/A * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2830N/A * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2830N/A */
2830N/A
2830N/Aimport java.io.*;
2830N/Aimport java.nio.*;
2830N/Aimport java.nio.channels.*;
2830N/Aimport java.nio.file.*;
2830N/Aimport java.nio.file.attribute.*;
2830N/Aimport java.net.*;
2830N/Aimport java.text.DateFormat;
2830N/Aimport java.text.SimpleDateFormat;
2830N/Aimport java.util.*;
2830N/A
2830N/Aimport static java.nio.file.StandardOpenOption.*;
2830N/Aimport static java.nio.file.StandardCopyOption.*;
2830N/A
2830N/A/*
2830N/A * ZipFileSystem usage demo
2830N/A *
2830N/A * java [-cp .../zipfs.jar:./] Demo action ZipfileName [...]
2830N/A *
2830N/A * To deploy the provider, either copy the zipfs.jar into JDK/JRE
2830N/A * extensions directory or add
2830N/A * <JDK_HOME>/demo/nio/ZipFileSystem/zipfs.jar
2830N/A * into your class path as showed above.
2830N/A *
2830N/A * @author Xueming Shen
2830N/A */
2830N/A
2830N/Apublic class Demo {
2830N/A
2830N/A static enum Action {
2830N/A rename, // <java Demo rename zipfile src dst>
2830N/A // rename entry src to dst inside zipfile
2830N/A
2830N/A movein, // <java Demo movein zipfile src dst>
2830N/A // move an external src file into zipfile
2830N/A // as entry dst
2830N/A
2830N/A moveout, // <java Demo moveout zipfile src dst>
2830N/A // move a zipfile entry src out to dst
2830N/A
2830N/A copy, // <java Demo copy zipfile src dst>
2830N/A // copy entry src to dst inside zipfile
2830N/A
2830N/A copyin, // <java Demo copyin zipfile src dst>
2830N/A // copy an external src file into zipfile
2830N/A // as entry dst
2830N/A
2830N/A copyout, // <java Demo copyout zipfile src dst>
2830N/A // copy zipfile entry src" out to file dst
2830N/A
2830N/A zzmove, // <java Demo zzmove zfsrc zfdst path>
2830N/A // move entry path/dir from zfsrc to zfdst
2830N/A
2830N/A zzcopy, // <java Demo zzcopy zfsrc zfdst path>
2830N/A // copy path from zipfile zfsrc to zipfile
2830N/A // zfdst
2830N/A
2830N/A attrs, // <java Demo attrs zipfile path>
2830N/A // printout the attributes of entry path
2830N/A
2830N/A attrsspace, // <java Demo attrsspace zipfile path>
2830N/A // printout the storespace attrs of entry path
2830N/A
2830N/A setmtime, // <java Demo setmtime zipfile "MM/dd/yy-HH:mm:ss" path...>
2830N/A // set the lastModifiedTime of entry path
2830N/A
2830N/A lsdir, // <java Demo lsdir zipfile dir>
2830N/A // list dir's direct child files/dirs
2830N/A
2830N/A mkdir, // <java Demo mkdir zipfile dir>
2830N/A
2830N/A mkdirs, // <java Demo mkdirs zipfile dir>
2830N/A
2830N/A rmdirs, // <java Demo rmdirs zipfile dir>
2830N/A
2830N/A list, // <java Demo list zipfile [dir]>
2830N/A // recursively list all entries of dir
2830N/A // via DirectoryStream
2830N/A
2830N/A tlist, // <java Demo tlist zipfile [dir]>
2830N/A // list with buildDirTree=true
2830N/A
2830N/A vlist, // <java Demo vlist zipfile [dir]>
2830N/A // recursively verbose list all entries of
2830N/A // dir via DirectoryStream
2830N/A
2830N/A walk, // <java Demo walk zipfile [dir]>
2830N/A // recursively walk all entries of dir
2830N/A // via Files.walkFileTree
2830N/A
2830N/A twalk, // <java Demo twalk zipfile [dir]>
2830N/A // walk with buildDirTree=true
2830N/A
2830N/A extract, // <java Demo extract zipfile file [...]>
2830N/A
2830N/A update, // <java Demo extract zipfile file [...]>
2830N/A
2830N/A delete, // <java Demo delete zipfile file [...]>
2830N/A
2830N/A add, // <java Demo add zipfile file [...]>
2830N/A
2830N/A create, // <java Demo create zipfile file [...]>
2830N/A // create a new zipfile if it doesn't exit
2830N/A // and then add the file(s) into it.
2830N/A
2830N/A attrs2, // <java Demo attrs2 zipfile file [...]>
2830N/A // test different ways to print attrs
2830N/A }
2830N/A
2830N/A public static void main(String[] args) throws Throwable {
2830N/A
2830N/A Action action = Action.valueOf(args[0]);;
2830N/A Map<String, Object> env = env = new HashMap<String, Object>();
2830N/A if (action == Action.create)
2830N/A env.put("createNew", true);
2830N/A if (action == Action.tlist || action == Action.twalk)
2830N/A env.put("buildDirTree", true);
2830N/A
2830N/A FileSystem fs = FileSystems.newFileSystem(
2830N/A URI.create("zip" + Paths.get(args[1]).toUri().toString().substring(4)),
2830N/A env,
2830N/A null);
2830N/A try {
2830N/A FileSystem fs2;
2830N/A Path path, src, dst;
2830N/A boolean isRename = false;
2830N/A switch (action) {
2830N/A case rename:
2830N/A src = fs.getPath(args[2]);
2830N/A dst = fs.getPath(args[3]);
2830N/A src.moveTo(dst);
2830N/A break;
2830N/A case moveout:
2830N/A src = fs.getPath(args[2]);
2830N/A dst = Paths.get(args[3]);
2830N/A src.moveTo(dst);
2830N/A break;
2830N/A case movein:
2830N/A src = Paths.get(args[2]);
2830N/A dst = fs.getPath(args[3]);
2830N/A src.moveTo(dst);
2830N/A break;
2830N/A case copy:
2830N/A src = fs.getPath(args[2]);
2830N/A dst = fs.getPath(args[3]);
2830N/A src.copyTo(dst);
2830N/A break;
2830N/A case copyout:
2830N/A src = fs.getPath(args[2]);
2830N/A dst = Paths.get(args[3]);
2830N/A src.copyTo(dst);
2830N/A break;
2830N/A case copyin:
2830N/A src = Paths.get(args[2]);
2830N/A dst = fs.getPath(args[3]);
2830N/A src.copyTo(dst);
2830N/A break;
2830N/A case zzmove:
2830N/A fs2 = FileSystems.newFileSystem(
2830N/A URI.create("zip" + Paths.get(args[2]).toUri().toString().substring(4)),
2830N/A env,
2830N/A null);
2830N/A //sf1.getPath(args[3]).moveTo(fs2.getPath(args[3]));
2830N/A z2zmove(fs, fs2, args[3]);
2830N/A fs2.close();
2830N/A break;
2830N/A case zzcopy:
2830N/A fs2 = FileSystems.newFileSystem(
2830N/A URI.create("zip" + Paths.get(args[2]).toUri().toString().substring(4)),
2830N/A env,
2830N/A null);
2830N/A //sf1.getPath(args[3]).copyTo(fs2.getPath(args[3]));
2830N/A z2zcopy(fs, fs2, args[3]);
2830N/A fs2.close();
2830N/A break;
2830N/A case attrs:
2830N/A for (int i = 2; i < args.length; i++) {
2830N/A path = fs.getPath(args[i]);
2830N/A System.out.println(
2830N/A Attributes.readBasicFileAttributes(path).toString());
2830N/A }
2830N/A break;
2830N/A case setmtime:
2830N/A DateFormat df = new SimpleDateFormat("MM/dd/yyyy-HH:mm:ss");
2830N/A Date newDatetime = df.parse(args[2]);
2830N/A for (int i = 3; i < args.length; i++) {
2830N/A path = fs.getPath(args[i]);
2830N/A path.setAttribute("lastModifiedTime",
2830N/A FileTime.fromMillis(newDatetime.getTime()));
2830N/A System.out.println(
2830N/A Attributes.readBasicFileAttributes(path).toString());
2830N/A }
2830N/A break;
2830N/A case attrsspace:
2830N/A path = fs.getPath("/");
2830N/A FileStore fstore = path.getFileStore();
2830N/A //System.out.println(fstore.getFileStoreAttributeView(FileStoreSpaceAttributeView.class)
2830N/A // .readAttributes());
2830N/A // or
2830N/A System.out.printf("filestore[%s]%n", fstore.name());
2830N/A System.out.printf(" totalSpace: %d%n",
2830N/A (Long)fstore.getAttribute("space:totalSpace"));
2830N/A System.out.printf(" usableSpace: %d%n",
2830N/A (Long)fstore.getAttribute("space:usableSpace"));
2830N/A System.out.printf(" unallocSpace: %d%n",
2830N/A (Long)fstore.getAttribute("space:unallocatedSpace"));
2830N/A break;
2830N/A case list:
2830N/A case tlist:
2830N/A if (args.length < 3)
2830N/A list(fs.getPath("/"), false);
2830N/A else
2830N/A list(fs.getPath(args[2]), false);
2830N/A break;
2830N/A case vlist:
2830N/A if (args.length < 3)
2830N/A list(fs.getPath("/"), true);
2830N/A else
2830N/A list(fs.getPath(args[2]), true);
2830N/A break;
2830N/A case twalk:
2830N/A case walk:
2830N/A walk(fs.getPath((args.length > 2)? args[2] : "/"));
2830N/A break;
2830N/A case extract:
2830N/A if (args.length == 2) {
2830N/A extract(fs, "/");
2830N/A } else {
2830N/A for (int i = 2; i < args.length; i++) {
2830N/A extract(fs, args[i]);
2830N/A }
2830N/A }
2830N/A break;
2830N/A case delete:
2830N/A for (int i = 2; i < args.length; i++)
2830N/A fs.getPath(args[i]).delete();
2830N/A break;
2830N/A case create:
2830N/A case add:
2830N/A case update:
2830N/A for (int i = 2; i < args.length; i++) {
2830N/A update(fs, args[i]);
2830N/A }
2830N/A break;
2830N/A case lsdir:
2830N/A path = fs.getPath(args[2]);
2830N/A final String fStr = (args.length > 3)?args[3]:"";
2830N/A DirectoryStream<Path> ds = path.newDirectoryStream(
2830N/A new DirectoryStream.Filter<Path>() {
2830N/A public boolean accept(Path path) {
2830N/A return path.toString().contains(fStr);
2830N/A }
2830N/A });
2830N/A for (Path p : ds)
2830N/A System.out.println(p);
2830N/A break;
2830N/A case mkdir:
2830N/A fs.getPath(args[2]).createDirectory();
2830N/A break;
2830N/A case mkdirs:
2830N/A mkdirs(fs.getPath(args[2]));
2830N/A break;
2830N/A case attrs2:
2830N/A for (int i = 2; i < args.length; i++) {
2830N/A path = fs.getPath(args[i]);
2830N/A System.out.println("-------(1)---------");
2830N/A System.out.println(
2830N/A Attributes.readBasicFileAttributes(path).toString());
2830N/A System.out.println("-------(2)---------");
2830N/A Map<String, ?> map = path.readAttributes("zip:*");
2830N/A for (Map.Entry<String, ?> e : map.entrySet()) {
2830N/A System.out.printf(" %s : %s%n", e.getKey(), e.getValue());
2830N/A }
2830N/A System.out.println("-------(3)---------");
2830N/A map = path.readAttributes("size,lastModifiedTime,isDirectory");
2830N/A for (Map.Entry<String, ?> e : map.entrySet()) {
2830N/A System.out.printf(" %s : %s%n", e.getKey(), e.getValue());
2830N/A }
2830N/A }
2830N/A break;
2830N/A }
2830N/A } catch (Exception x) {
2830N/A x.printStackTrace();
2830N/A } finally {
2830N/A if (fs != null)
2830N/A fs.close();
2830N/A }
2830N/A }
2830N/A
2830N/A private static byte[] getBytes(String name) {
2830N/A return name.getBytes();
2830N/A }
2830N/A
2830N/A private static String getString(byte[] name) {
2830N/A return new String(name);
2830N/A }
2830N/A
2830N/A private static void walk(Path path) throws IOException
2830N/A {
2830N/A Files.walkFileTree(
2830N/A path,
2830N/A new SimpleFileVisitor<Path>() {
2830N/A private int indent = 0;
2830N/A private void indent() {
2830N/A int n = 0;
2830N/A while (n++ < indent)
2830N/A System.out.printf(" ");
2830N/A }
2830N/A
2830N/A @Override
2830N/A public FileVisitResult visitFile(Path file,
2830N/A BasicFileAttributes attrs)
2830N/A {
2830N/A indent();
2830N/A System.out.printf("%s%n", file.getName().toString());
2830N/A return FileVisitResult.CONTINUE;
2830N/A }
2830N/A
2830N/A @Override
2830N/A public FileVisitResult preVisitDirectory(Path dir,
2830N/A BasicFileAttributes attrs)
2830N/A {
2830N/A indent();
2830N/A System.out.printf("[%s]%n", dir.toString());
2830N/A indent += 2;
2830N/A return FileVisitResult.CONTINUE;
2830N/A }
2830N/A
2830N/A @Override
2830N/A public FileVisitResult postVisitDirectory(Path dir,
2830N/A IOException ioe)
2830N/A {
2830N/A indent -= 2;
2830N/A return FileVisitResult.CONTINUE;
2830N/A }
2830N/A });
2830N/A }
2830N/A
2830N/A private static void update(FileSystem fs, String path) throws Throwable{
2830N/A Path src = FileSystems.getDefault().getPath(path);
2830N/A if (Boolean.TRUE.equals(src.getAttribute("isDirectory"))) {
2830N/A DirectoryStream<Path> ds = src.newDirectoryStream();
2830N/A for (Path child : ds)
2830N/A update(fs, child.toString());
2830N/A ds.close();
2830N/A } else {
2830N/A Path dst = fs.getPath(path);
2830N/A Path parent = dst.getParent();
2830N/A if (parent != null && parent.notExists())
2830N/A mkdirs(parent);
2830N/A src.copyTo(dst, REPLACE_EXISTING);
2830N/A }
2830N/A }
2830N/A
2830N/A private static void extract(FileSystem fs, String path) throws Throwable{
2830N/A Path src = fs.getPath(path);
2830N/A if (Boolean.TRUE.equals(src.getAttribute("isDirectory"))) {
2830N/A DirectoryStream<Path> ds = src.newDirectoryStream();
2830N/A for (Path child : ds)
2830N/A extract(fs, child.toString());
2830N/A ds.close();
2830N/A } else {
2830N/A if (path.startsWith("/"))
2830N/A path = path.substring(1);
2830N/A Path dst = FileSystems.getDefault().getPath(path);
2830N/A Path parent = dst.getParent();
2830N/A if (parent.notExists())
2830N/A mkdirs(parent);
2830N/A src.copyTo(dst, REPLACE_EXISTING);
2830N/A }
2830N/A }
2830N/A
2830N/A // use DirectoryStream
2830N/A private static void z2zcopy(FileSystem src, FileSystem dst, String path)
2830N/A throws IOException
2830N/A {
2830N/A Path srcPath = src.getPath(path);
2830N/A Path dstPath = dst.getPath(path);
2830N/A
2830N/A if (Boolean.TRUE.equals(srcPath.getAttribute("isDirectory"))) {
2830N/A if (!dstPath.exists()) {
2830N/A try {
2830N/A mkdirs(dstPath);
2830N/A } catch (FileAlreadyExistsException x) {}
2830N/A }
2830N/A DirectoryStream<Path> ds = srcPath.newDirectoryStream();
2830N/A for (Path child : ds) {
2830N/A z2zcopy(src, dst,
2830N/A path + (path.endsWith("/")?"":"/") + child.getName());
2830N/A }
2830N/A ds.close();
2830N/A } else {
2830N/A //System.out.println("copying..." + path);
2830N/A srcPath.copyTo(dstPath);
2830N/A }
2830N/A }
2830N/A
2830N/A // use TreeWalk to move
2830N/A private static void z2zmove(FileSystem src, FileSystem dst, String path)
2830N/A throws IOException
2830N/A {
2830N/A final Path srcPath = src.getPath(path).toAbsolutePath();
2830N/A final Path dstPath = dst.getPath(path).toAbsolutePath();
2830N/A
2830N/A Files.walkFileTree(srcPath, new SimpleFileVisitor<Path>() {
2830N/A
2830N/A @Override
2830N/A public FileVisitResult visitFile(Path file,
2830N/A BasicFileAttributes attrs)
2830N/A {
2830N/A Path dst = srcPath.relativize(file);
2830N/A dst = dstPath.resolve(dst);
2830N/A try {
2830N/A Path parent = dstPath.getParent();
2830N/A if (parent != null && parent.notExists())
2830N/A mkdirs(parent);
2830N/A file.moveTo(dst);
2830N/A } catch (IOException x) {
2830N/A x.printStackTrace();
2830N/A }
2830N/A return FileVisitResult.CONTINUE;
2830N/A }
2830N/A
2830N/A @Override
2830N/A public FileVisitResult preVisitDirectory(Path dir,
2830N/A BasicFileAttributes attrs)
2830N/A {
2830N/A Path dst = srcPath.relativize(dir);
2830N/A dst = dstPath.resolve(dst);
2830N/A try {
2830N/A
2830N/A if (dst.notExists())
2830N/A mkdirs(dst);
2830N/A } catch (IOException x) {
2830N/A x.printStackTrace();
2830N/A }
2830N/A return FileVisitResult.CONTINUE;
2830N/A }
2830N/A
2830N/A @Override
2830N/A public FileVisitResult postVisitDirectory(Path dir,
2830N/A IOException ioe)
2830N/A throws IOException
2830N/A {
2830N/A try {
2830N/A dir.delete();
2830N/A } catch (IOException x) {
2830N/A //x.printStackTrace();
2830N/A }
2830N/A return FileVisitResult.CONTINUE;
2830N/A }
2830N/A });
2830N/A
2830N/A }
2830N/A
2830N/A private static void mkdirs(Path path) throws IOException {
2830N/A path = path.toAbsolutePath();
2830N/A Path parent = path.getParent();
2830N/A if (parent != null) {
2830N/A if (parent.notExists())
2830N/A mkdirs(parent);
2830N/A }
2830N/A path.createDirectory();
2830N/A }
2830N/A
2830N/A private static void rmdirs(Path path) throws IOException {
2830N/A while (path != null && path.getNameCount() != 0) {
2830N/A path.delete();
2830N/A path = path.getParent();
2830N/A }
2830N/A }
2830N/A
2830N/A private static void list(Path path, boolean verbose ) throws IOException {
2830N/A if (verbose)
2830N/A System.out.println(Attributes.readBasicFileAttributes(path).toString());
2830N/A else
2830N/A System.out.printf(" %s%n", path.toString());
2830N/A if (path.notExists())
2830N/A return;
2830N/A if (Attributes.readBasicFileAttributes(path).isDirectory()) {
2830N/A DirectoryStream<Path> ds = path.newDirectoryStream();
2830N/A for (Path child : ds)
2830N/A list(child, verbose);
2830N/A ds.close();
2830N/A }
2830N/A }
2830N/A
2830N/A // check the content of two paths are equal
2830N/A private static void checkEqual(Path src, Path dst) throws IOException
2830N/A {
2830N/A //System.out.printf("checking <%s> vs <%s>...%n",
2830N/A // src.toString(), dst.toString());
2830N/A
2830N/A //streams
2830N/A InputStream isSrc = src.newInputStream();
2830N/A InputStream isDst = dst.newInputStream();
2830N/A byte[] bufSrc = new byte[8192];
2830N/A byte[] bufDst = new byte[8192];
2830N/A
2830N/A try {
2830N/A int nSrc = 0;
2830N/A while ((nSrc = isSrc.read(bufSrc)) != -1) {
2830N/A int nDst = 0;
2830N/A while (nDst < nSrc) {
2830N/A int n = isDst.read(bufDst, nDst, nSrc - nDst);
2830N/A if (n == -1) {
2830N/A System.out.printf("checking <%s> vs <%s>...%n",
2830N/A src.toString(), dst.toString());
2830N/A throw new RuntimeException("CHECK FAILED!");
2830N/A }
2830N/A nDst += n;
2830N/A }
2830N/A while (--nSrc >= 0) {
2830N/A if (bufSrc[nSrc] != bufDst[nSrc]) {
2830N/A System.out.printf("checking <%s> vs <%s>...%n",
2830N/A src.toString(), dst.toString());
2830N/A throw new RuntimeException("CHECK FAILED!");
2830N/A }
2830N/A nSrc--;
2830N/A }
2830N/A }
2830N/A } finally {
2830N/A isSrc.close();
2830N/A isDst.close();
2830N/A }
2830N/A
2830N/A // channels
2830N/A SeekableByteChannel chSrc = src.newByteChannel();
2830N/A SeekableByteChannel chDst = dst.newByteChannel();
2830N/A if (chSrc.size() != chDst.size()) {
2830N/A System.out.printf("src[%s].size=%d, dst[%s].size=%d%n",
2830N/A chSrc.toString(), chSrc.size(),
2830N/A chDst.toString(), chDst.size());
2830N/A throw new RuntimeException("CHECK FAILED!");
2830N/A }
2830N/A ByteBuffer bbSrc = ByteBuffer.allocate(8192);
2830N/A ByteBuffer bbDst = ByteBuffer.allocate(8192);
2830N/A
2830N/A try {
2830N/A int nSrc = 0;
2830N/A while ((nSrc = chSrc.read(bbSrc)) != -1) {
2830N/A int nDst = chDst.read(bbDst);
2830N/A if (nSrc != nDst) {
2830N/A System.out.printf("checking <%s> vs <%s>...%n",
2830N/A src.toString(), dst.toString());
2830N/A throw new RuntimeException("CHECK FAILED!");
2830N/A }
2830N/A while (--nSrc >= 0) {
2830N/A if (bbSrc.get(nSrc) != bbDst.get(nSrc)) {
2830N/A System.out.printf("checking <%s> vs <%s>...%n",
2830N/A src.toString(), dst.toString());
2830N/A throw new RuntimeException("CHECK FAILED!");
2830N/A }
2830N/A nSrc--;
2830N/A }
2830N/A bbSrc.flip();
2830N/A bbDst.flip();
2830N/A }
2830N/A } catch (IOException x) {
2830N/A x.printStackTrace();
2830N/A } finally {
2830N/A chSrc.close();
2830N/A chDst.close();
2830N/A }
2830N/A }
2830N/A
2830N/A private static void fchCopy(Path src, Path dst) throws IOException
2830N/A {
2830N/A Set<OpenOption> read = new HashSet<>();
2830N/A read.add(READ);
2830N/A Set<OpenOption> openwrite = new HashSet<>();
2830N/A openwrite.add(CREATE_NEW);
2830N/A openwrite.add(WRITE);
2830N/A
2830N/A FileChannel srcFc = src.getFileSystem()
2830N/A .provider()
2830N/A .newFileChannel(src, read);
2830N/A FileChannel dstFc = dst.getFileSystem()
2830N/A .provider()
2830N/A .newFileChannel(dst, openwrite);
2830N/A
2830N/A try {
2830N/A ByteBuffer bb = ByteBuffer.allocate(8192);
2830N/A while (srcFc.read(bb) >= 0) {
2830N/A bb.flip();
2830N/A dstFc.write(bb);
2830N/A bb.clear();
2830N/A }
2830N/A } finally {
2830N/A srcFc.close();
2830N/A dstFc.close();
2830N/A }
2830N/A }
2830N/A
2830N/A private static void chCopy(Path src, Path dst) throws IOException
2830N/A {
2830N/A Set<OpenOption> read = new HashSet<>();
2830N/A read.add(READ);
2830N/A Set<OpenOption> openwrite = new HashSet<>();
2830N/A openwrite.add(CREATE_NEW);
2830N/A openwrite.add(WRITE);
2830N/A
2830N/A SeekableByteChannel srcCh = src.newByteChannel(read);
2830N/A SeekableByteChannel dstCh = dst.newByteChannel(openwrite);
2830N/A
2830N/A try {
2830N/A ByteBuffer bb = ByteBuffer.allocate(8192);
2830N/A while (srcCh.read(bb) >= 0) {
2830N/A bb.flip();
2830N/A dstCh.write(bb);
2830N/A bb.clear();
2830N/A }
2830N/A } finally {
2830N/A srcCh.close();
2830N/A dstCh.close();
2830N/A }
2830N/A }
2830N/A
2830N/A private static void streamCopy(Path src, Path dst) throws IOException
2830N/A {
2830N/A InputStream isSrc = src.newInputStream();
2830N/A OutputStream osDst = dst.newOutputStream();
2830N/A byte[] buf = new byte[8192];
2830N/A try {
2830N/A int n = 0;
2830N/A while ((n = isSrc.read(buf)) != -1) {
2830N/A osDst.write(buf, 0, n);
2830N/A }
2830N/A } finally {
2830N/A isSrc.close();
2830N/A osDst.close();
2830N/A }
2830N/A }
2830N/A}