GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
MiscUtils.java
Go to the documentation of this file.
1/**
2 * Copyright 2014 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28package com.jogamp.junit.util;
29
30import java.io.BufferedInputStream;
31import java.io.File;
32import java.io.FileInputStream;
33import java.io.IOException;
34import java.io.InputStream;
35import java.util.ArrayList;
36import java.util.List;
37
38import com.jogamp.common.util.IOUtil;
39
40public class MiscUtils {
41 public static class CopyStats {
42 public int totalBytes = 0;
43 public int totalFiles = 0;
44 public int totalFolders = 0;
45 public int currentDepth = 0;
46 public int maxDepth = 0;
47 public boolean trackFiles;
48 public final List<File> srcFiles = new ArrayList<File>();
49 public final List<File> dstFiles = new ArrayList<File>();
50
51 public void dump(final String prefix, final boolean folderOnly) {
52 System.err.println(prefix+"Total bytes: "+totalBytes);
53 System.err.println(prefix+"Total files: "+totalFiles);
54 System.err.println(prefix+"Total folder: "+totalFolders);
55 System.err.println(prefix+"Depth: "+currentDepth/maxDepth);
56 System.err.println(prefix+"Tracking: "+trackFiles);
57 if( trackFiles ) {
58 for(int i=0; i<srcFiles.size(); i++) {
59 final File src = srcFiles.get(i);
60 final File dst = dstFiles.get(i);
61 if( !folderOnly || src.isDirectory() ) {
62 System.err.printf("%s\t src %4d: %s%n", prefix, i, src.toString());
63 System.err.printf("%s\t dst %4d: %s%n%n", prefix, i, dst.toString());
64 }
65 }
66 }
67 }
68 }
69 public static CopyStats copy(final File src, final File dest, final int maxDepth, final boolean trackFiles) throws IOException {
70 final CopyStats cs = new CopyStats();
71 cs.maxDepth = maxDepth;
72 cs.trackFiles = trackFiles;
73 copy(src, dest, cs);
74 return cs;
75 }
76 private static void copy(final File src, final File dest, final CopyStats stats) throws IOException {
77 if(src.isDirectory()){
78 if( stats.maxDepth >= 0 && stats.currentDepth >= stats.maxDepth ) {
79 return;
80 }
81 stats.totalFolders++;
82 if( stats.trackFiles ) {
83 stats.srcFiles.add(src);
84 stats.dstFiles.add(dest);
85 }
86 stats.currentDepth++;
87 if(!dest.exists()){
88 dest.mkdirs();
89 }
90 final String fileNames[] = src.list();
91 for (int i=0; i<fileNames.length; i++) {
92 final String fileName = fileNames[i];
93 final File srcFile = new File(src, fileName);
94 final File destFile = new File(dest, fileName);
95 copy(srcFile, destFile, stats);
96 }
97 stats.currentDepth--;
98 } else {
99 stats.totalFiles++;
100 if( stats.trackFiles ) {
101 stats.srcFiles.add(src);
102 stats.dstFiles.add(dest);
103 }
104 final InputStream in = new BufferedInputStream(new FileInputStream(src));
105 try {
106 stats.totalBytes += IOUtil.copyStream2File(in, dest);
107 } finally {
108 in.close();
109 }
110 }
111 }
112}
void dump(final String prefix, final boolean folderOnly)
Definition: MiscUtils.java:51
static CopyStats copy(final File src, final File dest, final int maxDepth, final boolean trackFiles)
Definition: MiscUtils.java:69