Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
MiscUtils.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2021 Gothel Software e.K.
4 * Copyright (c) 2014 Gothel Software e.K.
5 * Copyright (c) 2014 JogAmp Community.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26package jau.test.junit.util;
27
28import java.io.BufferedInputStream;
29import java.io.File;
30import java.io.FileInputStream;
31import java.io.IOException;
32import java.io.InputStream;
33import java.util.ArrayList;
34import java.util.List;
35
36import org.jau.io.IOUtil;
37
38public class MiscUtils {
39 public static class CopyStats {
40 public int totalBytes = 0;
41 public int totalFiles = 0;
42 public int totalFolders = 0;
43 public int currentDepth = 0;
44 public int maxDepth = 0;
45 public boolean trackFiles;
46 public final List<File> srcFiles = new ArrayList<File>();
47 public final List<File> dstFiles = new ArrayList<File>();
48
49 public void dump(final String prefix, final boolean folderOnly) {
50 System.err.println(prefix+"Total bytes: "+totalBytes);
51 System.err.println(prefix+"Total files: "+totalFiles);
52 System.err.println(prefix+"Total folder: "+totalFolders);
53 System.err.println(prefix+"Depth: "+currentDepth/maxDepth);
54 System.err.println(prefix+"Tracking: "+trackFiles);
55 if( trackFiles ) {
56 for(int i=0; i<srcFiles.size(); i++) {
57 final File src = srcFiles.get(i);
58 final File dst = dstFiles.get(i);
59 if( !folderOnly || src.isDirectory() ) {
60 System.err.printf("%s\t src %4d: %s%n", prefix, i, src.toString());
61 System.err.printf("%s\t dst %4d: %s%n%n", prefix, i, dst.toString());
62 }
63 }
64 }
65 }
66 }
67 public static CopyStats copy(final File src, final File dest, final int maxDepth, final boolean trackFiles) throws IOException {
68 final CopyStats cs = new CopyStats();
69 cs.maxDepth = maxDepth;
70 cs.trackFiles = trackFiles;
71 copy(src, dest, cs);
72 return cs;
73 }
74 private static void copy(final File src, final File dest, final CopyStats stats) throws IOException {
75 if(src.isDirectory()){
76 if( stats.maxDepth >= 0 && stats.currentDepth >= stats.maxDepth ) {
77 return;
78 }
79 stats.totalFolders++;
80 if( stats.trackFiles ) {
81 stats.srcFiles.add(src);
82 stats.dstFiles.add(dest);
83 }
84 stats.currentDepth++;
85 if(!dest.exists()){
86 dest.mkdirs();
87 }
88 final String fileNames[] = src.list();
89 for (int i=0; i<fileNames.length; i++) {
90 final String fileName = fileNames[i];
91 final File srcFile = new File(src, fileName);
92 final File destFile = new File(dest, fileName);
93 copy(srcFile, destFile, stats);
94 }
95 stats.currentDepth--;
96 } else {
97 stats.totalFiles++;
98 if( stats.trackFiles ) {
99 stats.srcFiles.add(src);
100 stats.dstFiles.add(dest);
101 }
102 final InputStream in = new BufferedInputStream(new FileInputStream(src));
103 try {
104 stats.totalBytes += IOUtil.copyStream2File(in, dest, 0);
105 } finally {
106 in.close();
107 }
108 }
109 }
110}
void dump(final String prefix, final boolean folderOnly)
Definition: MiscUtils.java:49
static CopyStats copy(final File src, final File dest, final int maxDepth, final boolean trackFiles)
Definition: MiscUtils.java:67