Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/sourcetools/com.ibm.jpp.preprocessor/com/ibm/jpp/om/SimpleCopy.java
6004 views
1
/*******************************************************************************
2
* Copyright (c) 1999, 2021 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
package com.ibm.jpp.om;
23
24
import java.io.File;
25
import java.io.FileInputStream;
26
import java.io.FileOutputStream;
27
import java.io.IOException;
28
import java.io.InputStream;
29
import java.io.OutputStream;
30
import java.util.ArrayList;
31
import java.util.HashMap;
32
import java.util.List;
33
import java.util.Map;
34
import java.util.StringTokenizer;
35
36
/**
37
* Class representing a preprocess copy simple copy (as opposed to a source that must be preprocessed). The SimpleCopy class is
38
* responsible for copying entire folder or single files to the appropriate destination.
39
*/
40
public class SimpleCopy {
41
private File sourcePath = null; /* The source directory or file to the copy. */
42
private File outputDir = null; /* The output directory for the copy. */
43
private int copyFileCount = 0; /* The number of copy files for each source directory. */
44
private final Map<File, String[]> copyFilesBySourceDir = new HashMap<>(); /* the relative paths of all of the files for a given sourceDir. */
45
private String baseDir = "";
46
private String simpleOutput = "";
47
48
/**
49
* Returns the copy task's output directory.
50
*
51
* @return the output directory
52
*/
53
public File getOutputDir() {
54
return this.outputDir;
55
}
56
57
/**
58
* Sets the copy task's output directory.
59
*
60
* @param outputDir the task's output directory
61
*/
62
public void setOutputDir(File outputDir) {
63
if (outputDir == null) {
64
throw new NullPointerException();
65
}
66
this.outputDir = outputDir;
67
}
68
69
/**
70
* Returns the copy task's source path.
71
*
72
* @return the source path
73
*/
74
public File getSourcePath() {
75
return this.sourcePath;
76
}
77
78
/**
79
* Sets the copy task's source path.
80
*
81
* @param sourcePath the source path
82
*/
83
public void setSourcePath(File sourcePath) {
84
if (sourcePath == null) {
85
throw new NullPointerException();
86
}
87
88
this.sourcePath = sourcePath;
89
}
90
91
/**
92
* Sets the simplecopy base directory.
93
*
94
* @param baseDir the base directory
95
*/
96
public void setBaseDir(String baseDir) {
97
this.baseDir = baseDir;
98
}
99
100
/**
101
* Sets the output path of this simplecopy task. This can either be
102
* an absolute path or one relative to the configuration output path.
103
*
104
* @param simpleOutput the simplecopy output path
105
*/
106
public void setSimpleOutput(String simpleOutput) {
107
this.simpleOutput = simpleOutput;
108
}
109
110
/**
111
* Performs the build.
112
*/
113
public boolean copy() {
114
System.out.println("\nCopying from " + sourcePath.getAbsolutePath());
115
116
if (sourcePath.isDirectory()) {
117
computeBuildFiles();
118
copyFileCount = 0;
119
String[] copyFiles = copyFilesBySourceDir.get(sourcePath);
120
121
for (String copyFile : copyFiles) {
122
File sourceFile = new File(sourcePath, copyFile);
123
File outputFile = new File(outputDir, copyFile);
124
125
// Exclude CVS files so the generated project does not display as shared in a repository
126
if (outputFile.getParentFile().getName().indexOf("CVS") > -1) {
127
outputFile.delete();
128
continue;
129
}
130
131
try {
132
copyFile(sourceFile, outputFile);
133
copyFileCount++;
134
} catch (IOException e) {
135
System.out.println("IOException occured in file " + sourceFile.getAbsolutePath() + ", copy failed.");
136
e.printStackTrace();
137
return false;
138
} catch (Exception e) {
139
System.out.println("Exception occured in file " + sourceFile.getAbsolutePath() + ", copy failed.");
140
e.printStackTrace();
141
return false;
142
}
143
}
144
} else {
145
File sourceFile = sourcePath;
146
File outputFile = null;
147
148
if (simpleOutput.startsWith(File.separator) || simpleOutput.startsWith(":", 1)) {
149
outputFile = new File(simpleOutput, sourcePath.getName());
150
} else if (!simpleOutput.equals("")) {
151
outputFile = new File(new File(outputDir, simpleOutput), sourcePath.getName());
152
} else {
153
String parent = sourcePath.getParent();
154
parent = parent.substring(baseDir.length());
155
156
StringBuffer strBuffer = new StringBuffer(outputDir.getAbsolutePath());
157
StringTokenizer st = new StringTokenizer(parent, File.separator);
158
159
if (st.countTokens() > 2) {
160
st.nextToken();
161
String token = st.nextToken();
162
163
if (!token.equals("src")) {
164
strBuffer.append(File.separator);
165
strBuffer.append(token);
166
}
167
168
while (st.hasMoreTokens()) {
169
strBuffer.append(File.separator);
170
strBuffer.append(st.nextToken());
171
}
172
}
173
174
strBuffer.append(File.separator);
175
strBuffer.append(sourcePath.getName());
176
177
outputFile = new File(strBuffer.toString());
178
}
179
180
try {
181
copyFile(sourceFile, outputFile);
182
copyFileCount++;
183
} catch (IOException e) {
184
System.out.println("IOException occured in file " + sourceFile.getAbsolutePath() + ", copy failed.");
185
e.printStackTrace();
186
return false;
187
}
188
}
189
190
System.out.println(copyFileCount + " file(s) copied.\n");
191
return true;
192
}
193
194
public static void copyFile(File inputFile, File outputFile) throws IOException {
195
outputFile.getParentFile().mkdirs();
196
outputFile.createNewFile();
197
198
try (InputStream input = new FileInputStream(inputFile)) {
199
try (OutputStream output = new FileOutputStream(outputFile)) {
200
byte[] buffer = new byte[4096];
201
202
for (int numBytes; (numBytes = input.read(buffer)) != -1;) {
203
output.write(buffer, 0, numBytes);
204
}
205
}
206
}
207
}
208
209
/**
210
* Recursively searches the given root directory to find all files. The file
211
* paths are returned, relative to the root directory.
212
*/
213
private static List<String> getFiles(File root) {
214
List<String> fileList = new ArrayList<>();
215
File[] files = root.listFiles();
216
217
if (files != null) {
218
getFiles(files, "", fileList);
219
} else {
220
System.out.print("Error reading the source directory \"");
221
System.out.print(root.getAbsolutePath());
222
System.out.println("\" - No Files copied");
223
}
224
225
return fileList;
226
}
227
228
/**
229
* This is a helper function to getFiles(File);
230
*/
231
private static void getFiles(File[] files, String relativePath, List<String> fileList) {
232
for (File file : files) {
233
String filePath = relativePath + file.getName();
234
235
if (file.isFile()) {
236
fileList.add(filePath);
237
} else {
238
getFiles(file.listFiles(), filePath + File.separator, fileList);
239
}
240
}
241
}
242
243
private void computeBuildFiles() {
244
List<String> copyFiles = getFiles(sourcePath);
245
String[] buildFilesArray = copyFiles.toArray(new String[copyFiles.size()]);
246
247
copyFileCount += buildFilesArray.length;
248
copyFilesBySourceDir.put(sourcePath, buildFilesArray);
249
}
250
}
251
252