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