Skip to content

Commit 45b9b13

Browse files
author
gitlab
committed
Merge branch 'refactor-pathutil' into 'master'
refactor against PathUtil See merge request zstackio/zstack!5986
2 parents 14d120d + d1f723c commit 45b9b13

1 file changed

Lines changed: 36 additions & 38 deletions

File tree

utils/src/main/java/org/zstack/utils/path/PathUtil.java

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package org.zstack.utils.path;
22

33
import org.apache.commons.codec.digest.DigestUtils;
4+
import org.apache.commons.io.FileUtils;
45
import org.apache.commons.io.IOUtils;
56
import org.zstack.utils.Utils;
67
import org.zstack.utils.logging.CLogger;
78

8-
import java.io.File;
9-
import java.io.FileInputStream;
10-
import java.io.FileOutputStream;
11-
import java.io.IOException;
9+
import java.io.*;
1210
import java.net.URISyntaxException;
1311
import java.net.URL;
1412
import java.nio.charset.Charset;
@@ -26,7 +24,7 @@ public static String join(String... paths) {
2624
assert paths != null && paths.length > 0;
2725

2826
File parent = new File(paths[0]);
29-
for (int i=1; i<paths.length; i++) {
27+
for (int i = 1; i < paths.length; i++) {
3028
parent = new File(parent, paths[i]);
3129
}
3230
return parent.getPath();
@@ -64,7 +62,7 @@ public static String getFilePathUnderZStackHomeFolder(String path) {
6462
}
6563

6664
public static String parentFolder(String fullPath) {
67-
if (! fullPath.contains(File.separator)) {
65+
if (!fullPath.contains(File.separator)) {
6866
return fullPath;
6967
}
7068

@@ -89,7 +87,7 @@ public static boolean exists(String path) {
8987
return f.exists();
9088
}
9189

92-
public static File findFolderOnClassPath(String folderName, boolean exceptionOnNotFound) {
90+
public static File findFolderOnClassPath(String folderName, boolean exceptionOnNotFound) {
9391
File folder = findFolderOnClassPath(folderName);
9492
if (folder == null && exceptionOnNotFound) {
9593
throw new RuntimeException(String.format("The folder %s is not found on classpath or there is another resource has the same name.", folderName));
@@ -132,38 +130,20 @@ public static File findFileOnClassPath(String fileName) {
132130
}
133131

134132
public static boolean compareFileByMd5(File src, File dst) {
135-
FileInputStream srcIn = null;
136-
FileInputStream dstIn = null;
137-
try {
138-
srcIn = new FileInputStream(src);
133+
try (FileInputStream srcIn = new FileInputStream(src);
134+
FileInputStream dstIn = new FileInputStream(dst)) {
139135
String srcMd5 = DigestUtils.md5Hex(srcIn);
140-
dstIn = new FileInputStream(dst);
141136
String dstMd5 = DigestUtils.md5Hex(dstIn);
142137
return srcMd5.equals(dstMd5);
143-
} catch (Exception e) {
138+
} catch (IOException e) {
144139
throw new RuntimeException(e);
145-
} finally {
146-
if (srcIn != null) {
147-
try {
148-
srcIn.close();
149-
} catch (IOException e) {
150-
logger.warn(String.format("FileInputStream close IOException:%s", e.getMessage()));
151-
}
152-
}
153-
if (dstIn != null) {
154-
try {
155-
dstIn.close();
156-
} catch (IOException e) {
157-
logger.warn(String.format("FileInputStream close IOException:%s", e.getMessage()));
158-
}
159-
}
160140
}
161141
}
162142

163143
public static List<String> scanFolderOnClassPath(String folderName) {
164144
URL folderUrl = PathUtil.class.getClassLoader().getResource(folderName);
165145
if (folderUrl == null || !folderUrl.getProtocol().equals("file")) {
166-
String info = String.format("The folder %s is not found in classpath or there is another resource has the same name.",folderName);
146+
String info = String.format("The folder %s is not found in classpath or there is another resource has the same name.", folderName);
167147
logger.warn(info);
168148
return new ArrayList<String>();
169149
}
@@ -190,7 +170,7 @@ public static void scanFolder(List<String> ret, String folderName) {
190170
return;
191171
}
192172

193-
for(File f: fileArray) {
173+
for (File f : fileArray) {
194174
if (f.isDirectory()) {
195175
scanFolder(ret, f.getAbsolutePath());
196176
} else {
@@ -208,33 +188,51 @@ public static void forceRemoveFile(String path) {
208188
boolean success = f.delete();
209189
logger.warn(String.format("Delete %s status: %s", path, success));
210190
} catch (Exception e) {
211-
logger.warn(String.format("Failed to delete file[path:%s]", path));
191+
logger.warn(String.format("Failed in deleting file: %s", path));
192+
}
193+
}
194+
195+
public static void forceRemoveDirectory(String path) {
196+
try {
197+
FileUtils.deleteDirectory(new File(path));
198+
logger.warn(String.format("Deleted directory: %s", path));
199+
} catch (IOException ignored) {
200+
logger.warn(String.format("Failed in deleting directory: %s", path));
212201
}
213202
}
214203

215-
public static String createTempDirectory(){
204+
public static String createTempDirectory() {
216205
try {
217206
return Files.createTempDirectory("tmp").toAbsolutePath().normalize().toString();
218207
} catch (IOException e) {
219208
throw new RuntimeException(e.getMessage());
220209
}
221210
}
222211

223-
public static String createTempFile(String prefix, String suffix){
212+
public static String createTempFile(String prefix, String suffix) {
224213
try {
225214
return Files.createTempFile(prefix, suffix).toAbsolutePath().normalize().toString();
226215
} catch (IOException e) {
227216
throw new RuntimeException(e.getMessage());
228217
}
229218
}
230219

231-
public static String createTempFileWithContent(String content){
220+
public static void writeFile(String fpath, String content) throws IOException {
221+
writeFile(fpath, content.getBytes(StandardCharsets.UTF_8));
222+
}
223+
224+
public static void writeFile(String fpath, byte[] data) throws IOException {
225+
try (FileOutputStream outputStream = new FileOutputStream(new File(fpath))) {
226+
outputStream.write(data);
227+
outputStream.flush();
228+
}
229+
}
230+
231+
public static String createTempFileWithContent(String content) {
232232
String tmpFile = null;
233233
try {
234234
tmpFile = Files.createTempFile("zs-", ".tmp").toAbsolutePath().normalize().toString();
235-
FileOutputStream outputStream = new FileOutputStream(new File(tmpFile));
236-
outputStream.write(content.getBytes(StandardCharsets.UTF_8));
237-
outputStream.close();
235+
writeFile(tmpFile, content);
238236
return tmpFile;
239237
} catch (IOException e) {
240238
Optional.ofNullable(tmpFile).ifPresent(PathUtil::forceRemoveFile);
@@ -247,7 +245,7 @@ public static boolean moveFile(String source, String target) {
247245
}
248246

249247
public static String readFileToString(String path, Charset charset) {
250-
try(UnicodeReader reader = new UnicodeReader(new FileInputStream(new File(path)), charset.toString())) {
248+
try (UnicodeReader reader = new UnicodeReader(new FileInputStream(new File(path)), charset.toString())) {
251249
return IOUtils.toString(reader);
252250
} catch (IOException e) {
253251
throw new RuntimeException(e.getMessage());

0 commit comments

Comments
 (0)