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
4378N/A/*
4378N/A * This source code is provided to illustrate the usage of a given feature
4378N/A * or technique and has been deliberately simplified. Additional steps
4378N/A * required for a production-quality application, such as security checks,
4378N/A * input validation and proper error handling, might not be present in
4378N/A * this sample code.
4378N/A */
4378N/A
4378N/A
2830N/Aimport java.io.*;
2830N/Aimport java.nio.*;
2830N/Aimport java.nio.channels.*;
2830N/Aimport java.nio.file.*;
3478N/Aimport java.nio.file.spi.*;
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 * ZipFileSystem usage demo
2830N/A *
3216N/A * java Demo action ZipfileName [...]
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
3060N/A copyin_attrs, // <java Demo copyin_attrs zipfile src dst>
3060N/A // copy an external src file into zipfile
3060N/A // as entry dst, with attributes (timestamp)
3060N/A
2830N/A copyout, // <java Demo copyout zipfile src dst>
2830N/A // copy zipfile entry src" out to file dst
2830N/A
3060N/A copyout_attrs, // <java Demo copyout_attrs zipfile src dst>
3060N/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
3060N/A setatime, // <java Demo setatime zipfile "MM/dd/yy-HH:mm:ss" path...>
3060N/A setctime, // <java Demo setctime zipfile "MM/dd/yy-HH:mm:ss" path...>
3060N/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
3060N/A
3060N/A prof,
2830N/A }
2830N/A
2830N/A public static void main(String[] args) throws Throwable {
3478N/A FileSystemProvider provider = getZipFSProvider();
3478N/A if (provider == null) {
3478N/A System.err.println("ZIP filesystem provider is not installed");
3478N/A System.exit(1);
3478N/A }
2830N/A
3060N/A Action action = Action.valueOf(args[0]);
3060N/A Map<String, Object> env = env = new HashMap<>();
2830N/A if (action == Action.create)
3216N/A env.put("create", "true");
3478N/A try (FileSystem fs = provider.newFileSystem(Paths.get(args[1]), env)) {
2830N/A Path path, src, dst;
2830N/A switch (action) {
2830N/A case rename:
2830N/A src = fs.getPath(args[2]);
2830N/A dst = fs.getPath(args[3]);
3478N/A Files.move(src, dst);
2830N/A break;
2830N/A case moveout:
2830N/A src = fs.getPath(args[2]);
2830N/A dst = Paths.get(args[3]);
3478N/A Files.move(src, dst);
2830N/A break;
2830N/A case movein:
2830N/A src = Paths.get(args[2]);
2830N/A dst = fs.getPath(args[3]);
3478N/A Files.move(src, dst);
2830N/A break;
2830N/A case copy:
2830N/A src = fs.getPath(args[2]);
2830N/A dst = fs.getPath(args[3]);
3478N/A Files.copy(src, dst);
2830N/A break;
2830N/A case copyout:
2830N/A src = fs.getPath(args[2]);
2830N/A dst = Paths.get(args[3]);
3478N/A Files.copy(src, dst);
2830N/A break;
2830N/A case copyin:
2830N/A src = Paths.get(args[2]);
2830N/A dst = fs.getPath(args[3]);
3478N/A Files.copy(src, dst);
2830N/A break;
3060N/A case copyin_attrs:
3060N/A src = Paths.get(args[2]);
3060N/A dst = fs.getPath(args[3]);
3478N/A Files.copy(src, dst, COPY_ATTRIBUTES);
3060N/A break;
3060N/A case copyout_attrs:
3060N/A src = fs.getPath(args[2]);
3060N/A dst = Paths.get(args[3]);
3478N/A Files.copy(src, dst, COPY_ATTRIBUTES);
3060N/A break;
2830N/A case zzmove:
3503N/A try (FileSystem fs2 = provider.newFileSystem(Paths.get(args[2]), env)) {
3478N/A z2zmove(fs, fs2, args[3]);
3478N/A }
2830N/A break;
2830N/A case zzcopy:
3503N/A try (FileSystem fs2 = provider.newFileSystem(Paths.get(args[2]), env)) {
3478N/A z2zcopy(fs, fs2, args[3]);
3478N/A }
2830N/A break;
2830N/A case attrs:
2830N/A for (int i = 2; i < args.length; i++) {
2830N/A path = fs.getPath(args[i]);
3060N/A System.out.println(path);
2830N/A System.out.println(
3478N/A Files.readAttributes(path, BasicFileAttributes.class).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]);
3478N/A Files.setAttribute(path, "lastModifiedTime",
3478N/A FileTime.fromMillis(newDatetime.getTime()));
2830N/A System.out.println(
3478N/A Files.readAttributes(path, BasicFileAttributes.class).toString());
2830N/A }
2830N/A break;
3060N/A case setctime:
3060N/A df = new SimpleDateFormat("MM/dd/yyyy-HH:mm:ss");
3060N/A newDatetime = df.parse(args[2]);
3060N/A for (int i = 3; i < args.length; i++) {
3060N/A path = fs.getPath(args[i]);
3478N/A Files.setAttribute(path, "creationTime",
3478N/A FileTime.fromMillis(newDatetime.getTime()));
3060N/A System.out.println(
3478N/A Files.readAttributes(path, BasicFileAttributes.class).toString());
3060N/A }
3060N/A break;
3060N/A case setatime:
3060N/A df = new SimpleDateFormat("MM/dd/yyyy-HH:mm:ss");
3060N/A newDatetime = df.parse(args[2]);
3060N/A for (int i = 3; i < args.length; i++) {
3060N/A path = fs.getPath(args[i]);
3478N/A Files.setAttribute(path, "lastAccessTime",
3478N/A FileTime.fromMillis(newDatetime.getTime()));
3060N/A System.out.println(
3478N/A Files.readAttributes(path, BasicFileAttributes.class).toString());
3060N/A }
3060N/A break;
2830N/A case attrsspace:
2830N/A path = fs.getPath("/");
3478N/A FileStore fstore = Files.getFileStore(path);
2830N/A System.out.printf("filestore[%s]%n", fstore.name());
2830N/A System.out.printf(" totalSpace: %d%n",
3478N/A (Long)fstore.getAttribute("totalSpace"));
2830N/A System.out.printf(" usableSpace: %d%n",
3478N/A (Long)fstore.getAttribute("usableSpace"));
2830N/A System.out.printf(" unallocSpace: %d%n",
3478N/A (Long)fstore.getAttribute("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++)
3478N/A Files.delete(fs.getPath(args[i]));
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]:"";
3478N/A try (DirectoryStream<Path> ds = Files.newDirectoryStream(path,
2830N/A new DirectoryStream.Filter<Path>() {
4123N/A @Override
2830N/A public boolean accept(Path path) {
2830N/A return path.toString().contains(fStr);
2830N/A }
3478N/A }))
3478N/A {
3478N/A for (Path p : ds)
3478N/A System.out.println(p);
3478N/A }
2830N/A break;
2830N/A case mkdir:
3478N/A Files.createDirectory(fs.getPath(args[2]));
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]);
3060N/A System.out.printf("%n%s%n", path);
2830N/A System.out.println("-------(1)---------");
2830N/A System.out.println(
3478N/A Files.readAttributes(path, BasicFileAttributes.class).toString());
2830N/A System.out.println("-------(2)---------");
3478N/A Map<String, Object> map = Files.readAttributes(path, "zip:*");
3478N/A for (Map.Entry<String, Object> e : map.entrySet()) {
2830N/A System.out.printf(" %s : %s%n", e.getKey(), e.getValue());
2830N/A }
2830N/A System.out.println("-------(3)---------");
3478N/A map = Files.readAttributes(path, "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;
3060N/A case prof:
3060N/A list(fs.getPath("/"), false);
3060N/A while (true) {
3060N/A Thread.sleep(10000);
3060N/A //list(fs.getPath("/"), true);
3060N/A System.out.println("sleeping...");
3060N/A }
2830N/A }
2830N/A } catch (Exception x) {
2830N/A x.printStackTrace();
2830N/A }
2830N/A }
2830N/A
3478N/A private static FileSystemProvider getZipFSProvider() {
3478N/A for (FileSystemProvider provider : FileSystemProvider.installedProviders()) {
3478N/A if ("jar".equals(provider.getScheme()))
3478N/A return provider;
3478N/A }
3478N/A return null;
3478N/A }
3478N/A
4123N/A @SuppressWarnings("unused")
4123N/A /**
4123N/A * Not used in demo, but included for demonstrational purposes.
4123N/A */
2830N/A private static byte[] getBytes(String name) {
2830N/A return name.getBytes();
2830N/A }
2830N/A
4123N/A @SuppressWarnings("unused")
4123N/A /**
4123N/A * Not used in demo, but included for demonstrational purposes.
4123N/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();
3478N/A System.out.printf("%s%n", file.getFileName().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);
3478N/A if (Files.isDirectory(src)) {
3478N/A try (DirectoryStream<Path> ds = Files.newDirectoryStream(src)) {
3478N/A for (Path child : ds)
3478N/A update(fs, child.toString());
3478N/A }
2830N/A } else {
2830N/A Path dst = fs.getPath(path);
2830N/A Path parent = dst.getParent();
3478N/A if (parent != null && Files.notExists(parent))
2830N/A mkdirs(parent);
3478N/A Files.copy(src, 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);
3478N/A if (Files.isDirectory(src)) {
3478N/A try (DirectoryStream<Path> ds = Files.newDirectoryStream(src)) {
3478N/A for (Path child : ds)
3478N/A extract(fs, child.toString());
3478N/A }
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();
3478N/A if (Files.notExists(parent))
2830N/A mkdirs(parent);
3478N/A Files.copy(src, 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
3478N/A if (Files.isDirectory(srcPath)) {
3478N/A if (!Files.exists(dstPath)) {
2830N/A try {
2830N/A mkdirs(dstPath);
2830N/A } catch (FileAlreadyExistsException x) {}
2830N/A }
3478N/A try (DirectoryStream<Path> ds = Files.newDirectoryStream(srcPath)) {
3478N/A for (Path child : ds) {
3478N/A z2zcopy(src, dst,
3478N/A path + (path.endsWith("/")?"":"/") + child.getFileName());
3478N/A }
2830N/A }
2830N/A } else {
2830N/A //System.out.println("copying..." + path);
3478N/A Files.copy(srcPath, 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();
3478N/A if (parent != null && Files.notExists(parent))
2830N/A mkdirs(parent);
3478N/A Files.move(file, 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
3478N/A if (Files.notExists(dst))
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 {
3478N/A Files.delete(dir);
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) {
3478N/A if (Files.notExists(parent))
2830N/A mkdirs(parent);
2830N/A }
3478N/A Files.createDirectory(path);
2830N/A }
2830N/A
4123N/A @SuppressWarnings("unused")
4123N/A /**
4123N/A * Not used in demo, but included for demonstrational purposes.
4123N/A */
2830N/A private static void rmdirs(Path path) throws IOException {
2830N/A while (path != null && path.getNameCount() != 0) {
3478N/A Files.delete(path);
2830N/A path = path.getParent();
2830N/A }
2830N/A }
2830N/A
2830N/A private static void list(Path path, boolean verbose ) throws IOException {
3060N/A if (!"/".equals(path.toString())) {
3060N/A System.out.printf(" %s%n", path.toString());
3060N/A if (verbose)
3478N/A System.out.println(Files.readAttributes(path, BasicFileAttributes.class).toString());
3060N/A }
3478N/A if (Files.notExists(path))
2830N/A return;
3478N/A if (Files.isDirectory(path)) {
3478N/A try (DirectoryStream<Path> ds = Files.newDirectoryStream(path)) {
3478N/A for (Path child : ds)
3478N/A list(child, verbose);
3478N/A }
2830N/A }
2830N/A }
2830N/A
4123N/A @SuppressWarnings("unused")
4123N/A /**
4123N/A * Checks that the content of two paths are equal.
4123N/A * Not used in demo, but included for demonstrational purposes.
4123N/A */
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 byte[] bufSrc = new byte[8192];
2830N/A byte[] bufDst = new byte[8192];
3478N/A try (InputStream isSrc = Files.newInputStream(src);
3478N/A InputStream isDst = Files.newInputStream(dst))
3478N/A {
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 }
2830N/A
2830N/A // channels
2830N/A
3478N/A try (SeekableByteChannel chSrc = Files.newByteChannel(src);
3478N/A SeekableByteChannel chDst = Files.newByteChannel(dst))
3478N/A {
3478N/A if (chSrc.size() != chDst.size()) {
3478N/A System.out.printf("src[%s].size=%d, dst[%s].size=%d%n",
3478N/A chSrc.toString(), chSrc.size(),
3478N/A chDst.toString(), chDst.size());
3478N/A throw new RuntimeException("CHECK FAILED!");
3478N/A }
3478N/A ByteBuffer bbSrc = ByteBuffer.allocate(8192);
3478N/A ByteBuffer bbDst = ByteBuffer.allocate(8192);
3478N/A
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 }
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
3478N/A try (FileChannel srcFc = src.getFileSystem().provider().newFileChannel(src, read);
3478N/A FileChannel dstFc = dst.getFileSystem().provider().newFileChannel(dst, openwrite))
3478N/A {
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 }
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
3478N/A try (SeekableByteChannel srcCh = Files.newByteChannel(src, read);
3478N/A SeekableByteChannel dstCh = Files.newByteChannel(dst, openwrite))
3478N/A {
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 }
2830N/A }
2830N/A
2830N/A private static void streamCopy(Path src, Path dst) throws IOException
2830N/A {
2830N/A byte[] buf = new byte[8192];
3478N/A try (InputStream isSrc = Files.newInputStream(src);
3478N/A OutputStream osDst = Files.newOutputStream(dst))
3478N/A {
2830N/A int n = 0;
2830N/A while ((n = isSrc.read(buf)) != -1) {
2830N/A osDst.write(buf, 0, n);
2830N/A }
2830N/A }
2830N/A }
2830N/A}