ZipFSTester.java revision 3569
c869993e79c1eafbec61a56bf6cea848fe754c71xy/*
c869993e79c1eafbec61a56bf6cea848fe754c71xy * Copyright (c) 2010 Oracle and/or its affiliates. All rights reserved.
c869993e79c1eafbec61a56bf6cea848fe754c71xy * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c869993e79c1eafbec61a56bf6cea848fe754c71xy *
c869993e79c1eafbec61a56bf6cea848fe754c71xy * This code is free software; you can redistribute it and/or modify it
c869993e79c1eafbec61a56bf6cea848fe754c71xy * under the terms of the GNU General Public License version 2 only, as
c869993e79c1eafbec61a56bf6cea848fe754c71xy * published by the Free Software Foundation.
c869993e79c1eafbec61a56bf6cea848fe754c71xy *
c869993e79c1eafbec61a56bf6cea848fe754c71xy * This code is distributed in the hope that it will be useful, but WITHOUT
c869993e79c1eafbec61a56bf6cea848fe754c71xy * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c869993e79c1eafbec61a56bf6cea848fe754c71xy * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
c869993e79c1eafbec61a56bf6cea848fe754c71xy * version 2 for more details (a copy is included in the LICENSE file that
c869993e79c1eafbec61a56bf6cea848fe754c71xy * accompanied this code).
c869993e79c1eafbec61a56bf6cea848fe754c71xy *
c869993e79c1eafbec61a56bf6cea848fe754c71xy * You should have received a copy of the GNU General Public License version
c869993e79c1eafbec61a56bf6cea848fe754c71xy * 2 along with this work; if not, write to the Free Software Foundation,
c869993e79c1eafbec61a56bf6cea848fe754c71xy * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c869993e79c1eafbec61a56bf6cea848fe754c71xy *
c869993e79c1eafbec61a56bf6cea848fe754c71xy * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c869993e79c1eafbec61a56bf6cea848fe754c71xy * or visit www.oracle.com if you need additional information or have any
c869993e79c1eafbec61a56bf6cea848fe754c71xy * questions.
c869993e79c1eafbec61a56bf6cea848fe754c71xy */
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xyimport java.io.*;
c869993e79c1eafbec61a56bf6cea848fe754c71xyimport java.nio.*;
c869993e79c1eafbec61a56bf6cea848fe754c71xyimport java.nio.channels.*;
c869993e79c1eafbec61a56bf6cea848fe754c71xyimport java.nio.file.*;
c869993e79c1eafbec61a56bf6cea848fe754c71xyimport java.nio.file.spi.*;
c869993e79c1eafbec61a56bf6cea848fe754c71xyimport java.nio.file.attribute.*;
c869993e79c1eafbec61a56bf6cea848fe754c71xyimport java.net.*;
c869993e79c1eafbec61a56bf6cea848fe754c71xyimport java.util.*;
c869993e79c1eafbec61a56bf6cea848fe754c71xyimport java.util.zip.*;
fa25784ca4b51c206177d891a654f1d36a25d41fxy
c869993e79c1eafbec61a56bf6cea848fe754c71xyimport static java.nio.file.StandardOpenOption.*;
c869993e79c1eafbec61a56bf6cea848fe754c71xyimport static java.nio.file.StandardCopyOption.*;
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy/*
c869993e79c1eafbec61a56bf6cea848fe754c71xy * Tests various zipfs operations.
c869993e79c1eafbec61a56bf6cea848fe754c71xy */
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xypublic class ZipFSTester {
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy public static void main(String[] args) throws Throwable {
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy try (FileSystem fs = newZipFileSystem(Paths.get(args[0]),
c869993e79c1eafbec61a56bf6cea848fe754c71xy new HashMap<String, Object>()))
c869993e79c1eafbec61a56bf6cea848fe754c71xy {
c869993e79c1eafbec61a56bf6cea848fe754c71xy test0(fs);
c869993e79c1eafbec61a56bf6cea848fe754c71xy test1(fs);
c869993e79c1eafbec61a56bf6cea848fe754c71xy test2(fs); // more tests
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy static void test0(FileSystem fs)
c869993e79c1eafbec61a56bf6cea848fe754c71xy throws Exception
c869993e79c1eafbec61a56bf6cea848fe754c71xy {
c869993e79c1eafbec61a56bf6cea848fe754c71xy List<String> list = new LinkedList<>();
c869993e79c1eafbec61a56bf6cea848fe754c71xy try (ZipFile zf = new ZipFile(fs.toString())) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy Enumeration<? extends ZipEntry> zes = zf.entries();
c869993e79c1eafbec61a56bf6cea848fe754c71xy while (zes.hasMoreElements()) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy list.add(zes.nextElement().getName());
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy for (String pname : list) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy Path path = fs.getPath(pname);
c869993e79c1eafbec61a56bf6cea848fe754c71xy if (!Files.exists(path))
c869993e79c1eafbec61a56bf6cea848fe754c71xy throw new RuntimeException("path existence check failed!");
c869993e79c1eafbec61a56bf6cea848fe754c71xy while ((path = path.getParent()) != null) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy if (!Files.exists(path))
c869993e79c1eafbec61a56bf6cea848fe754c71xy throw new RuntimeException("parent existence check failed!");
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy static void test1(FileSystem fs0)
c869993e79c1eafbec61a56bf6cea848fe754c71xy throws Exception
c869993e79c1eafbec61a56bf6cea848fe754c71xy {
c869993e79c1eafbec61a56bf6cea848fe754c71xy Random rdm = new Random();
c869993e79c1eafbec61a56bf6cea848fe754c71xy // clone a fs and test on it
c869993e79c1eafbec61a56bf6cea848fe754c71xy Path tmpfsPath = getTempPath();
c869993e79c1eafbec61a56bf6cea848fe754c71xy Map<String, Object> env = new HashMap<String, Object>();
c869993e79c1eafbec61a56bf6cea848fe754c71xy env.put("create", "true");
c869993e79c1eafbec61a56bf6cea848fe754c71xy try (FileSystem copy = newZipFileSystem(tmpfsPath, env)) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy z2zcopy(fs0, copy, "/", 0);
fa25784ca4b51c206177d891a654f1d36a25d41fxy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy try (FileSystem fs = newZipFileSystem(tmpfsPath, new HashMap<String, Object>())) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy FileSystemProvider provider = fs.provider();
c869993e79c1eafbec61a56bf6cea848fe754c71xy // newFileSystem(path...) should not throw exception
c869993e79c1eafbec61a56bf6cea848fe754c71xy try (FileSystem fsPath = provider.newFileSystem(tmpfsPath, new HashMap<String, Object>())){}
c869993e79c1eafbec61a56bf6cea848fe754c71xy try (FileSystem fsUri = provider.newFileSystem(
c869993e79c1eafbec61a56bf6cea848fe754c71xy new URI("jar", tmpfsPath.toUri().toString(), null),
c869993e79c1eafbec61a56bf6cea848fe754c71xy new HashMap<String, Object>()))
c869993e79c1eafbec61a56bf6cea848fe754c71xy {
c869993e79c1eafbec61a56bf6cea848fe754c71xy throw new RuntimeException("newFileSystem(uri...) does not throw exception");
c869993e79c1eafbec61a56bf6cea848fe754c71xy } catch (FileSystemAlreadyExistsException fsaee) {}
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy // prepare a src
c869993e79c1eafbec61a56bf6cea848fe754c71xy Path src = getTempPath();
c869993e79c1eafbec61a56bf6cea848fe754c71xy String tmpName = src.toString();
c869993e79c1eafbec61a56bf6cea848fe754c71xy OutputStream os = Files.newOutputStream(src);
c869993e79c1eafbec61a56bf6cea848fe754c71xy byte[] bits = new byte[12345];
c869993e79c1eafbec61a56bf6cea848fe754c71xy rdm.nextBytes(bits);
c869993e79c1eafbec61a56bf6cea848fe754c71xy os.write(bits);
c869993e79c1eafbec61a56bf6cea848fe754c71xy os.close();
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl try {
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl provider.newFileSystem(new File(System.getProperty("test.src", ".")).toPath(),
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl new HashMap<String, Object>());
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl throw new RuntimeException("newFileSystem() opens a directory as zipfs");
c869993e79c1eafbec61a56bf6cea848fe754c71xy } catch (UnsupportedOperationException uoe) {}
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy try {
c869993e79c1eafbec61a56bf6cea848fe754c71xy provider.newFileSystem(src, new HashMap<String, Object>());
c869993e79c1eafbec61a56bf6cea848fe754c71xy throw new RuntimeException("newFileSystem() opens a non-zip file as zipfs");
c869993e79c1eafbec61a56bf6cea848fe754c71xy } catch (UnsupportedOperationException uoe) {}
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy // copyin
c869993e79c1eafbec61a56bf6cea848fe754c71xy Path dst = getPathWithParents(fs, tmpName);
c869993e79c1eafbec61a56bf6cea848fe754c71xy Files.copy(src, dst);
c869993e79c1eafbec61a56bf6cea848fe754c71xy checkEqual(src, dst);
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy // copy
c869993e79c1eafbec61a56bf6cea848fe754c71xy Path dst2 = getPathWithParents(fs, "/xyz" + rdm.nextInt(100) +
c869993e79c1eafbec61a56bf6cea848fe754c71xy "/efg" + rdm.nextInt(100) + "/foo.class");
c869993e79c1eafbec61a56bf6cea848fe754c71xy Files.copy(dst, dst2);
c869993e79c1eafbec61a56bf6cea848fe754c71xy //dst.moveTo(dst2);
c869993e79c1eafbec61a56bf6cea848fe754c71xy checkEqual(src, dst2);
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy // delete
c869993e79c1eafbec61a56bf6cea848fe754c71xy Files.delete(dst);
c869993e79c1eafbec61a56bf6cea848fe754c71xy if (Files.exists(dst))
c869993e79c1eafbec61a56bf6cea848fe754c71xy throw new RuntimeException("Failed!");
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy // moveout
c869993e79c1eafbec61a56bf6cea848fe754c71xy Path dst3 = Paths.get(tmpName + "_Tmp");
c869993e79c1eafbec61a56bf6cea848fe754c71xy Files.move(dst2, dst3);
c869993e79c1eafbec61a56bf6cea848fe754c71xy checkEqual(src, dst3);
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy // delete
c869993e79c1eafbec61a56bf6cea848fe754c71xy if (Files.exists(dst2))
c869993e79c1eafbec61a56bf6cea848fe754c71xy throw new RuntimeException("Failed!");
c869993e79c1eafbec61a56bf6cea848fe754c71xy Files.delete(dst3);
c869993e79c1eafbec61a56bf6cea848fe754c71xy if (Files.exists(dst3))
c869993e79c1eafbec61a56bf6cea848fe754c71xy throw new RuntimeException("Failed!");
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy // newInputStream on dir
c869993e79c1eafbec61a56bf6cea848fe754c71xy Path parent = dst2.getParent();
c869993e79c1eafbec61a56bf6cea848fe754c71xy try {
c869993e79c1eafbec61a56bf6cea848fe754c71xy Files.newInputStream(parent);
c869993e79c1eafbec61a56bf6cea848fe754c71xy throw new RuntimeException("Failed");
c869993e79c1eafbec61a56bf6cea848fe754c71xy } catch (FileSystemException e) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy e.printStackTrace(); // expected fse
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy // rmdirs
c869993e79c1eafbec61a56bf6cea848fe754c71xy try {
c869993e79c1eafbec61a56bf6cea848fe754c71xy rmdirs(parent);
c869993e79c1eafbec61a56bf6cea848fe754c71xy } catch (IOException x) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy x.printStackTrace();
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl }
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy // newFileChannel() copy in, out and verify via fch
c869993e79c1eafbec61a56bf6cea848fe754c71xy fchCopy(src, dst); // in
c869993e79c1eafbec61a56bf6cea848fe754c71xy checkEqual(src, dst);
c869993e79c1eafbec61a56bf6cea848fe754c71xy Path tmp = Paths.get(tmpName + "_Tmp");
c869993e79c1eafbec61a56bf6cea848fe754c71xy fchCopy(dst, tmp); // out
c869993e79c1eafbec61a56bf6cea848fe754c71xy checkEqual(src, tmp);
c869993e79c1eafbec61a56bf6cea848fe754c71xy Files.delete(tmp);
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy // test channels
c869993e79c1eafbec61a56bf6cea848fe754c71xy channel(fs, dst);
c869993e79c1eafbec61a56bf6cea848fe754c71xy Files.delete(dst);
c869993e79c1eafbec61a56bf6cea848fe754c71xy Files.delete(src);
c869993e79c1eafbec61a56bf6cea848fe754c71xy } finally {
c869993e79c1eafbec61a56bf6cea848fe754c71xy if (Files.exists(tmpfsPath))
c869993e79c1eafbec61a56bf6cea848fe754c71xy Files.delete(tmpfsPath);
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy static void test2(FileSystem fs) throws Exception {
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy Path fs1Path = getTempPath();
c869993e79c1eafbec61a56bf6cea848fe754c71xy Path fs2Path = getTempPath();
c869993e79c1eafbec61a56bf6cea848fe754c71xy Path fs3Path = getTempPath();
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy // create a new filesystem, copy everything from fs
c869993e79c1eafbec61a56bf6cea848fe754c71xy Map<String, Object> env = new HashMap<String, Object>();
c869993e79c1eafbec61a56bf6cea848fe754c71xy env.put("create", "true");
c869993e79c1eafbec61a56bf6cea848fe754c71xy FileSystem fs0 = newZipFileSystem(fs1Path, env);
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy final FileSystem fs2 = newZipFileSystem(fs2Path, env);
c869993e79c1eafbec61a56bf6cea848fe754c71xy final FileSystem fs3 = newZipFileSystem(fs3Path, env);
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy System.out.println("copy src: fs -> fs0...");
c869993e79c1eafbec61a56bf6cea848fe754c71xy z2zcopy(fs, fs0, "/", 0); // copy fs -> fs1
c869993e79c1eafbec61a56bf6cea848fe754c71xy fs0.close(); // dump to file
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy System.out.println("open fs0 as fs1");
c869993e79c1eafbec61a56bf6cea848fe754c71xy env = new HashMap<String, Object>();
c869993e79c1eafbec61a56bf6cea848fe754c71xy final FileSystem fs1 = newZipFileSystem(fs1Path, env);
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy System.out.println("listing...");
c869993e79c1eafbec61a56bf6cea848fe754c71xy final ArrayList<String> files = new ArrayList<>();
c869993e79c1eafbec61a56bf6cea848fe754c71xy final ArrayList<String> dirs = new ArrayList<>();
c869993e79c1eafbec61a56bf6cea848fe754c71xy list(fs1.getPath("/"), files, dirs);
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy Thread t0 = new Thread(new Runnable() {
c869993e79c1eafbec61a56bf6cea848fe754c71xy public void run() {
c869993e79c1eafbec61a56bf6cea848fe754c71xy List<String> list = new ArrayList<>(dirs);
c869993e79c1eafbec61a56bf6cea848fe754c71xy Collections.shuffle(list);
c869993e79c1eafbec61a56bf6cea848fe754c71xy for (String path : list) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy try {
c869993e79c1eafbec61a56bf6cea848fe754c71xy z2zcopy(fs1, fs2, path, 0);
c869993e79c1eafbec61a56bf6cea848fe754c71xy } catch (Exception x) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy x.printStackTrace();
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy });
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy Thread t1 = new Thread(new Runnable() {
c869993e79c1eafbec61a56bf6cea848fe754c71xy public void run() {
c869993e79c1eafbec61a56bf6cea848fe754c71xy List<String> list = new ArrayList<>(dirs);
c869993e79c1eafbec61a56bf6cea848fe754c71xy Collections.shuffle(list);
c869993e79c1eafbec61a56bf6cea848fe754c71xy for (String path : list) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy try {
c869993e79c1eafbec61a56bf6cea848fe754c71xy z2zcopy(fs1, fs2, path, 1);
c869993e79c1eafbec61a56bf6cea848fe754c71xy } catch (Exception x) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy x.printStackTrace();
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy });
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy Thread t2 = new Thread(new Runnable() {
c869993e79c1eafbec61a56bf6cea848fe754c71xy public void run() {
c869993e79c1eafbec61a56bf6cea848fe754c71xy List<String> list = new ArrayList<>(dirs);
c869993e79c1eafbec61a56bf6cea848fe754c71xy Collections.shuffle(list);
c869993e79c1eafbec61a56bf6cea848fe754c71xy for (String path : list) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy try {
c869993e79c1eafbec61a56bf6cea848fe754c71xy z2zcopy(fs1, fs2, path, 2);
c869993e79c1eafbec61a56bf6cea848fe754c71xy } catch (Exception x) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy x.printStackTrace();
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy });
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy Thread t3 = new Thread(new Runnable() {
c869993e79c1eafbec61a56bf6cea848fe754c71xy public void run() {
c869993e79c1eafbec61a56bf6cea848fe754c71xy List<String> list = new ArrayList<>(files);
c869993e79c1eafbec61a56bf6cea848fe754c71xy Collections.shuffle(list);
c869993e79c1eafbec61a56bf6cea848fe754c71xy while (!list.isEmpty()) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy Iterator<String> itr = list.iterator();
c869993e79c1eafbec61a56bf6cea848fe754c71xy while (itr.hasNext()) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy String path = itr.next();
c869993e79c1eafbec61a56bf6cea848fe754c71xy try {
c869993e79c1eafbec61a56bf6cea848fe754c71xy if (Files.exists(fs2.getPath(path))) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy z2zmove(fs2, fs3, path);
c869993e79c1eafbec61a56bf6cea848fe754c71xy itr.remove();
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy } catch (FileAlreadyExistsException x){
c869993e79c1eafbec61a56bf6cea848fe754c71xy itr.remove();
c869993e79c1eafbec61a56bf6cea848fe754c71xy } catch (Exception x) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy x.printStackTrace();
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy });
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy System.out.println("copying/removing...");
c869993e79c1eafbec61a56bf6cea848fe754c71xy t0.start(); t1.start(); t2.start(); t3.start();
c869993e79c1eafbec61a56bf6cea848fe754c71xy t0.join(); t1.join(); t2.join(); t3.join();
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl System.out.println("closing: fs1, fs2");
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl fs1.close();
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl fs2.close();
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl int failed = 0;
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl System.out.println("checkEqual: fs vs fs3");
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl for (String path : files) {
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl try {
c869993e79c1eafbec61a56bf6cea848fe754c71xy checkEqual(fs.getPath(path), fs3.getPath(path));
c869993e79c1eafbec61a56bf6cea848fe754c71xy } catch (IOException x) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy //x.printStackTrace();
c869993e79c1eafbec61a56bf6cea848fe754c71xy failed++;
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy System.out.println("closing: fs3");
c869993e79c1eafbec61a56bf6cea848fe754c71xy fs3.close();
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy System.out.println("opening: fs3 as fs4");
c869993e79c1eafbec61a56bf6cea848fe754c71xy FileSystem fs4 = newZipFileSystem(fs3Path, env);
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy ArrayList<String> files2 = new ArrayList<>();
c869993e79c1eafbec61a56bf6cea848fe754c71xy ArrayList<String> dirs2 = new ArrayList<>();
c869993e79c1eafbec61a56bf6cea848fe754c71xy list(fs4.getPath("/"), files2, dirs2);
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy System.out.println("checkEqual: fs vs fs4");
c869993e79c1eafbec61a56bf6cea848fe754c71xy for (String path : files2) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy checkEqual(fs.getPath(path), fs4.getPath(path));
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy System.out.println("walking: fs4");
c869993e79c1eafbec61a56bf6cea848fe754c71xy walk(fs4.getPath("/"));
c869993e79c1eafbec61a56bf6cea848fe754c71xy System.out.println("closing: fs4");
c869993e79c1eafbec61a56bf6cea848fe754c71xy fs4.close();
c869993e79c1eafbec61a56bf6cea848fe754c71xy System.out.printf("failed=%d%n", failed);
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy Files.delete(fs1Path);
c869993e79c1eafbec61a56bf6cea848fe754c71xy Files.delete(fs2Path);
c869993e79c1eafbec61a56bf6cea848fe754c71xy Files.delete(fs3Path);
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy private static FileSystem newZipFileSystem(Path path, Map<String, ?> env)
c869993e79c1eafbec61a56bf6cea848fe754c71xy throws Exception
c869993e79c1eafbec61a56bf6cea848fe754c71xy {
c869993e79c1eafbec61a56bf6cea848fe754c71xy return FileSystems.newFileSystem(
c869993e79c1eafbec61a56bf6cea848fe754c71xy new URI("jar", path.toUri().toString(), null), env, null);
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy private static Path getTempPath() throws IOException
c869993e79c1eafbec61a56bf6cea848fe754c71xy {
c869993e79c1eafbec61a56bf6cea848fe754c71xy File tmp = File.createTempFile("testzipfs_", "zip");
c869993e79c1eafbec61a56bf6cea848fe754c71xy tmp.delete(); // we need a clean path, no file
c869993e79c1eafbec61a56bf6cea848fe754c71xy return tmp.toPath();
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy private static void list(Path path, List<String> files, List<String> dirs )
c869993e79c1eafbec61a56bf6cea848fe754c71xy throws IOException
c869993e79c1eafbec61a56bf6cea848fe754c71xy {
c869993e79c1eafbec61a56bf6cea848fe754c71xy if (Files.isDirectory(path)) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy try (DirectoryStream<Path> ds = Files.newDirectoryStream(path)) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy for (Path child : ds)
c869993e79c1eafbec61a56bf6cea848fe754c71xy list(child, files, dirs);
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy dirs.add(path.toString());
c869993e79c1eafbec61a56bf6cea848fe754c71xy } else {
c869993e79c1eafbec61a56bf6cea848fe754c71xy files.add(path.toString());
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy private static void z2zcopy(FileSystem src, FileSystem dst, String path,
c869993e79c1eafbec61a56bf6cea848fe754c71xy int method)
c869993e79c1eafbec61a56bf6cea848fe754c71xy throws IOException
c869993e79c1eafbec61a56bf6cea848fe754c71xy {
c869993e79c1eafbec61a56bf6cea848fe754c71xy Path srcPath = src.getPath(path);
c869993e79c1eafbec61a56bf6cea848fe754c71xy Path dstPath = dst.getPath(path);
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy if (Files.isDirectory(srcPath)) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy if (!Files.exists(dstPath)) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy try {
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl mkdirs(dstPath);
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl } catch (FileAlreadyExistsException x) {}
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl }
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl try (DirectoryStream<Path> ds = Files.newDirectoryStream(srcPath)) {
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl for (Path child : ds) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy z2zcopy(src, dst,
c869993e79c1eafbec61a56bf6cea848fe754c71xy path + (path.endsWith("/")?"":"/") + child.getFileName(),
c869993e79c1eafbec61a56bf6cea848fe754c71xy method);
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy } else {
c869993e79c1eafbec61a56bf6cea848fe754c71xy try {
c869993e79c1eafbec61a56bf6cea848fe754c71xy if (Files.exists(dstPath))
c869993e79c1eafbec61a56bf6cea848fe754c71xy return;
c869993e79c1eafbec61a56bf6cea848fe754c71xy switch (method) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy case 0:
c869993e79c1eafbec61a56bf6cea848fe754c71xy Files.copy(srcPath, dstPath);
c869993e79c1eafbec61a56bf6cea848fe754c71xy break;
c869993e79c1eafbec61a56bf6cea848fe754c71xy case 1:
c869993e79c1eafbec61a56bf6cea848fe754c71xy chCopy(srcPath, dstPath);
c869993e79c1eafbec61a56bf6cea848fe754c71xy break;
c869993e79c1eafbec61a56bf6cea848fe754c71xy case 2:
c869993e79c1eafbec61a56bf6cea848fe754c71xy //fchCopy(srcPath, dstPath);
c869993e79c1eafbec61a56bf6cea848fe754c71xy streamCopy(srcPath, dstPath);
c869993e79c1eafbec61a56bf6cea848fe754c71xy break;
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy } catch (FileAlreadyExistsException x) {}
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy private static void z2zmove(FileSystem src, FileSystem dst, String path)
c869993e79c1eafbec61a56bf6cea848fe754c71xy throws IOException
c869993e79c1eafbec61a56bf6cea848fe754c71xy {
c869993e79c1eafbec61a56bf6cea848fe754c71xy Path srcPath = src.getPath(path);
c869993e79c1eafbec61a56bf6cea848fe754c71xy Path dstPath = dst.getPath(path);
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy if (Files.isDirectory(srcPath)) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy if (!Files.exists(dstPath))
c869993e79c1eafbec61a56bf6cea848fe754c71xy mkdirs(dstPath);
c869993e79c1eafbec61a56bf6cea848fe754c71xy try (DirectoryStream<Path> ds = Files.newDirectoryStream(srcPath)) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy for (Path child : ds) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy z2zmove(src, dst,
c869993e79c1eafbec61a56bf6cea848fe754c71xy path + (path.endsWith("/")?"":"/") + child.getFileName());
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy } else {
c869993e79c1eafbec61a56bf6cea848fe754c71xy //System.out.println("moving..." + path);
c869993e79c1eafbec61a56bf6cea848fe754c71xy Path parent = dstPath.getParent();
c869993e79c1eafbec61a56bf6cea848fe754c71xy if (parent != null && Files.notExists(parent))
c869993e79c1eafbec61a56bf6cea848fe754c71xy mkdirs(parent);
c869993e79c1eafbec61a56bf6cea848fe754c71xy Files.move(srcPath, dstPath);
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy private static void walk(Path path) throws IOException
c869993e79c1eafbec61a56bf6cea848fe754c71xy {
c869993e79c1eafbec61a56bf6cea848fe754c71xy Files.walkFileTree(
c869993e79c1eafbec61a56bf6cea848fe754c71xy path,
c869993e79c1eafbec61a56bf6cea848fe754c71xy new SimpleFileVisitor<Path>() {
c869993e79c1eafbec61a56bf6cea848fe754c71xy private int indent = 0;
c869993e79c1eafbec61a56bf6cea848fe754c71xy private void indent() {
c869993e79c1eafbec61a56bf6cea848fe754c71xy int n = 0;
c869993e79c1eafbec61a56bf6cea848fe754c71xy while (n++ < indent)
c869993e79c1eafbec61a56bf6cea848fe754c71xy System.out.printf(" ");
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy @Override
c869993e79c1eafbec61a56bf6cea848fe754c71xy public FileVisitResult visitFile(Path file,
c869993e79c1eafbec61a56bf6cea848fe754c71xy BasicFileAttributes attrs)
c869993e79c1eafbec61a56bf6cea848fe754c71xy {
c869993e79c1eafbec61a56bf6cea848fe754c71xy indent();
c869993e79c1eafbec61a56bf6cea848fe754c71xy System.out.printf("%s%n", file.getFileName().toString());
c869993e79c1eafbec61a56bf6cea848fe754c71xy return FileVisitResult.CONTINUE;
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy @Override
c869993e79c1eafbec61a56bf6cea848fe754c71xy public FileVisitResult preVisitDirectory(Path dir,
c869993e79c1eafbec61a56bf6cea848fe754c71xy BasicFileAttributes attrs)
c869993e79c1eafbec61a56bf6cea848fe754c71xy {
c869993e79c1eafbec61a56bf6cea848fe754c71xy indent();
c869993e79c1eafbec61a56bf6cea848fe754c71xy System.out.printf("[%s]%n", dir.toString());
c869993e79c1eafbec61a56bf6cea848fe754c71xy indent += 2;
c869993e79c1eafbec61a56bf6cea848fe754c71xy return FileVisitResult.CONTINUE;
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy @Override
c869993e79c1eafbec61a56bf6cea848fe754c71xy public FileVisitResult postVisitDirectory(Path dir,
c869993e79c1eafbec61a56bf6cea848fe754c71xy IOException ioe)
c869993e79c1eafbec61a56bf6cea848fe754c71xy throws IOException
c869993e79c1eafbec61a56bf6cea848fe754c71xy {
c869993e79c1eafbec61a56bf6cea848fe754c71xy indent -= 2;
c869993e79c1eafbec61a56bf6cea848fe754c71xy return FileVisitResult.CONTINUE;
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy });
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy private static void mkdirs(Path path) throws IOException {
c869993e79c1eafbec61a56bf6cea848fe754c71xy if (Files.exists(path))
c869993e79c1eafbec61a56bf6cea848fe754c71xy return;
c869993e79c1eafbec61a56bf6cea848fe754c71xy path = path.toAbsolutePath();
c869993e79c1eafbec61a56bf6cea848fe754c71xy Path parent = path.getParent();
c869993e79c1eafbec61a56bf6cea848fe754c71xy if (parent != null) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy if (Files.notExists(parent))
c869993e79c1eafbec61a56bf6cea848fe754c71xy mkdirs(parent);
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy Files.createDirectory(path);
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy private static void rmdirs(Path path) throws IOException {
c869993e79c1eafbec61a56bf6cea848fe754c71xy while (path != null && path.getNameCount() != 0) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy Files.delete(path);
c869993e79c1eafbec61a56bf6cea848fe754c71xy path = path.getParent();
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy // check the content of two paths are equal
c869993e79c1eafbec61a56bf6cea848fe754c71xy private static void checkEqual(Path src, Path dst) throws IOException
c869993e79c1eafbec61a56bf6cea848fe754c71xy {
c869993e79c1eafbec61a56bf6cea848fe754c71xy //System.out.printf("checking <%s> vs <%s>...%n",
c869993e79c1eafbec61a56bf6cea848fe754c71xy // src.toString(), dst.toString());
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy //streams
c869993e79c1eafbec61a56bf6cea848fe754c71xy byte[] bufSrc = new byte[8192];
c869993e79c1eafbec61a56bf6cea848fe754c71xy byte[] bufDst = new byte[8192];
c869993e79c1eafbec61a56bf6cea848fe754c71xy try (InputStream isSrc = Files.newInputStream(src);
c869993e79c1eafbec61a56bf6cea848fe754c71xy InputStream isDst = Files.newInputStream(dst))
c869993e79c1eafbec61a56bf6cea848fe754c71xy {
c869993e79c1eafbec61a56bf6cea848fe754c71xy int nSrc = 0;
c869993e79c1eafbec61a56bf6cea848fe754c71xy while ((nSrc = isSrc.read(bufSrc)) != -1) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy int nDst = 0;
c869993e79c1eafbec61a56bf6cea848fe754c71xy while (nDst < nSrc) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy int n = isDst.read(bufDst, nDst, nSrc - nDst);
c869993e79c1eafbec61a56bf6cea848fe754c71xy if (n == -1) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy System.out.printf("checking <%s> vs <%s>...%n",
c869993e79c1eafbec61a56bf6cea848fe754c71xy src.toString(), dst.toString());
c869993e79c1eafbec61a56bf6cea848fe754c71xy throw new RuntimeException("CHECK FAILED!");
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy nDst += n;
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy while (--nSrc >= 0) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy if (bufSrc[nSrc] != bufDst[nSrc]) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy System.out.printf("checking <%s> vs <%s>...%n",
c869993e79c1eafbec61a56bf6cea848fe754c71xy src.toString(), dst.toString());
c869993e79c1eafbec61a56bf6cea848fe754c71xy throw new RuntimeException("CHECK FAILED!");
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy nSrc--;
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy // channels
c869993e79c1eafbec61a56bf6cea848fe754c71xy try (SeekableByteChannel chSrc = Files.newByteChannel(src);
c869993e79c1eafbec61a56bf6cea848fe754c71xy SeekableByteChannel chDst = Files.newByteChannel(dst))
c869993e79c1eafbec61a56bf6cea848fe754c71xy {
c869993e79c1eafbec61a56bf6cea848fe754c71xy if (chSrc.size() != chDst.size()) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy System.out.printf("src[%s].size=%d, dst[%s].size=%d%n",
c869993e79c1eafbec61a56bf6cea848fe754c71xy chSrc.toString(), chSrc.size(),
c869993e79c1eafbec61a56bf6cea848fe754c71xy chDst.toString(), chDst.size());
c869993e79c1eafbec61a56bf6cea848fe754c71xy throw new RuntimeException("CHECK FAILED!");
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy ByteBuffer bbSrc = ByteBuffer.allocate(8192);
c869993e79c1eafbec61a56bf6cea848fe754c71xy ByteBuffer bbDst = ByteBuffer.allocate(8192);
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy int nSrc = 0;
c869993e79c1eafbec61a56bf6cea848fe754c71xy while ((nSrc = chSrc.read(bbSrc)) != -1) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy int nDst = chDst.read(bbDst);
c869993e79c1eafbec61a56bf6cea848fe754c71xy if (nSrc != nDst) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy System.out.printf("checking <%s> vs <%s>...%n",
c869993e79c1eafbec61a56bf6cea848fe754c71xy src.toString(), dst.toString());
c869993e79c1eafbec61a56bf6cea848fe754c71xy throw new RuntimeException("CHECK FAILED!");
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy while (--nSrc >= 0) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy if (bbSrc.get(nSrc) != bbDst.get(nSrc)) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy System.out.printf("checking <%s> vs <%s>...%n",
c869993e79c1eafbec61a56bf6cea848fe754c71xy src.toString(), dst.toString());
c869993e79c1eafbec61a56bf6cea848fe754c71xy throw new RuntimeException("CHECK FAILED!");
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy nSrc--;
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy bbSrc.flip();
c869993e79c1eafbec61a56bf6cea848fe754c71xy bbDst.flip();
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy } catch (IOException x) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy x.printStackTrace();
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy private static void fchCopy(Path src, Path dst) throws IOException
c869993e79c1eafbec61a56bf6cea848fe754c71xy {
c869993e79c1eafbec61a56bf6cea848fe754c71xy Set<OpenOption> read = new HashSet<>();
c869993e79c1eafbec61a56bf6cea848fe754c71xy read.add(READ);
c869993e79c1eafbec61a56bf6cea848fe754c71xy Set<OpenOption> openwrite = new HashSet<>();
c869993e79c1eafbec61a56bf6cea848fe754c71xy openwrite.add(CREATE_NEW);
c869993e79c1eafbec61a56bf6cea848fe754c71xy openwrite.add(WRITE);
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy try (FileChannel srcFc = src.getFileSystem()
c869993e79c1eafbec61a56bf6cea848fe754c71xy .provider()
c869993e79c1eafbec61a56bf6cea848fe754c71xy .newFileChannel(src, read);
c869993e79c1eafbec61a56bf6cea848fe754c71xy FileChannel dstFc = dst.getFileSystem()
c869993e79c1eafbec61a56bf6cea848fe754c71xy .provider()
c869993e79c1eafbec61a56bf6cea848fe754c71xy .newFileChannel(dst, openwrite))
c869993e79c1eafbec61a56bf6cea848fe754c71xy {
c869993e79c1eafbec61a56bf6cea848fe754c71xy ByteBuffer bb = ByteBuffer.allocate(8192);
c869993e79c1eafbec61a56bf6cea848fe754c71xy while (srcFc.read(bb) >= 0) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy bb.flip();
c869993e79c1eafbec61a56bf6cea848fe754c71xy dstFc.write(bb);
c869993e79c1eafbec61a56bf6cea848fe754c71xy bb.clear();
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy private static void chCopy(Path src, Path dst) throws IOException
c869993e79c1eafbec61a56bf6cea848fe754c71xy {
c869993e79c1eafbec61a56bf6cea848fe754c71xy Set<OpenOption> read = new HashSet<>();
c869993e79c1eafbec61a56bf6cea848fe754c71xy read.add(READ);
c869993e79c1eafbec61a56bf6cea848fe754c71xy Set<OpenOption> openwrite = new HashSet<>();
c869993e79c1eafbec61a56bf6cea848fe754c71xy openwrite.add(CREATE_NEW);
c869993e79c1eafbec61a56bf6cea848fe754c71xy openwrite.add(WRITE);
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy try (SeekableByteChannel srcCh = Files.newByteChannel(src, read);
c869993e79c1eafbec61a56bf6cea848fe754c71xy SeekableByteChannel dstCh = Files.newByteChannel(dst, openwrite))
c869993e79c1eafbec61a56bf6cea848fe754c71xy {
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy ByteBuffer bb = ByteBuffer.allocate(8192);
c869993e79c1eafbec61a56bf6cea848fe754c71xy while (srcCh.read(bb) >= 0) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy bb.flip();
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl dstCh.write(bb);
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl bb.clear();
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy private static void streamCopy(Path src, Path dst) throws IOException
c869993e79c1eafbec61a56bf6cea848fe754c71xy {
c869993e79c1eafbec61a56bf6cea848fe754c71xy byte[] buf = new byte[8192];
c869993e79c1eafbec61a56bf6cea848fe754c71xy try (InputStream isSrc = Files.newInputStream(src);
c869993e79c1eafbec61a56bf6cea848fe754c71xy OutputStream osDst = Files.newOutputStream(dst))
c869993e79c1eafbec61a56bf6cea848fe754c71xy {
c869993e79c1eafbec61a56bf6cea848fe754c71xy int n = 0;
c869993e79c1eafbec61a56bf6cea848fe754c71xy while ((n = isSrc.read(buf)) != -1) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy osDst.write(buf, 0, n);
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy static void channel(FileSystem fs, Path path)
c869993e79c1eafbec61a56bf6cea848fe754c71xy throws Exception
c869993e79c1eafbec61a56bf6cea848fe754c71xy {
c869993e79c1eafbec61a56bf6cea848fe754c71xy System.out.println("test ByteChannel...");
c869993e79c1eafbec61a56bf6cea848fe754c71xy Set<OpenOption> read = new HashSet<>();
c869993e79c1eafbec61a56bf6cea848fe754c71xy read.add(READ);
c869993e79c1eafbec61a56bf6cea848fe754c71xy int n = 0;
c869993e79c1eafbec61a56bf6cea848fe754c71xy ByteBuffer bb = null;
c869993e79c1eafbec61a56bf6cea848fe754c71xy ByteBuffer bb2 = null;
c869993e79c1eafbec61a56bf6cea848fe754c71xy int N = 120;
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy try (SeekableByteChannel sbc = Files.newByteChannel(path)) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy System.out.printf(" sbc[0]: pos=%d, size=%d%n", sbc.position(), sbc.size());
c869993e79c1eafbec61a56bf6cea848fe754c71xy bb = ByteBuffer.allocate((int)sbc.size());
c869993e79c1eafbec61a56bf6cea848fe754c71xy n = sbc.read(bb);
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl System.out.printf(" sbc[1]: read=%d, pos=%d, size=%d%n",
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl n, sbc.position(), sbc.size());
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl bb2 = ByteBuffer.allocate((int)sbc.size());
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl }
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl // sbc.position(pos) is not supported in current version
8bb4b220fdb894543e41a5f9037898cf3c3f312bgl // try the FileChannel
c869993e79c1eafbec61a56bf6cea848fe754c71xy try (SeekableByteChannel sbc = fs.provider().newFileChannel(path, read)) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy sbc.position(N);
c869993e79c1eafbec61a56bf6cea848fe754c71xy System.out.printf(" sbc[2]: pos=%d, size=%d%n",
c869993e79c1eafbec61a56bf6cea848fe754c71xy sbc.position(), sbc.size());
c869993e79c1eafbec61a56bf6cea848fe754c71xy bb2.limit(100);
c869993e79c1eafbec61a56bf6cea848fe754c71xy n = sbc.read(bb2);
c869993e79c1eafbec61a56bf6cea848fe754c71xy System.out.printf(" sbc[3]: read=%d, pos=%d, size=%d%n",
c869993e79c1eafbec61a56bf6cea848fe754c71xy n, sbc.position(), sbc.size());
c869993e79c1eafbec61a56bf6cea848fe754c71xy System.out.printf(" sbc[4]: bb[%d]=%d, bb1[0]=%d%n",
c869993e79c1eafbec61a56bf6cea848fe754c71xy N, bb.get(N) & 0xff, bb2.get(0) & 0xff);
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy
c869993e79c1eafbec61a56bf6cea848fe754c71xy // create parents if does not exist
c869993e79c1eafbec61a56bf6cea848fe754c71xy static Path getPathWithParents(FileSystem fs, String name)
c869993e79c1eafbec61a56bf6cea848fe754c71xy throws Exception
c869993e79c1eafbec61a56bf6cea848fe754c71xy {
c869993e79c1eafbec61a56bf6cea848fe754c71xy Path path = fs.getPath(name);
c869993e79c1eafbec61a56bf6cea848fe754c71xy Path parent = path.getParent();
c869993e79c1eafbec61a56bf6cea848fe754c71xy if (parent != null && Files.notExists(parent))
c869993e79c1eafbec61a56bf6cea848fe754c71xy mkdirs(parent);
c869993e79c1eafbec61a56bf6cea848fe754c71xy return path;
c869993e79c1eafbec61a56bf6cea848fe754c71xy }
c869993e79c1eafbec61a56bf6cea848fe754c71xy}
c869993e79c1eafbec61a56bf6cea848fe754c71xy