1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.internal.test.util;
20
21 import java.io.BufferedOutputStream;
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.OutputStream;
27 import java.io.RandomAccessFile;
28 import java.nio.charset.StandardCharsets;
29 import java.nio.file.Files;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.Properties;
33 import java.util.UUID;
34
35
36
37
38 public class TestFileUtils {
39
40 private static final File TMP = new File(
41 System.getProperty("java.io.tmpdir"),
42 "aether-" + UUID.randomUUID().toString().substring(0, 8));
43
44 static {
45 Runtime.getRuntime().addShutdownHook(new Thread(() -> {
46 try {
47 deleteFile(TMP);
48 } catch (IOException e) {
49 e.printStackTrace();
50 }
51 }));
52 }
53
54 private TestFileUtils() {
55
56 }
57
58 @Deprecated
59 public static void deleteTempFiles() throws IOException {
60 deleteFile(TMP);
61 }
62
63 public static void deleteFile(File file) throws IOException {
64 if (file == null) {
65 return;
66 }
67
68 Collection<File> undeletables = new ArrayList<>();
69
70 delete(file, undeletables);
71
72 if (!undeletables.isEmpty()) {
73 throw new IOException("Failed to delete " + undeletables);
74 }
75 }
76
77 private static void delete(File file, Collection<File> undeletables) {
78 String[] children = file.list();
79 if (children != null) {
80 for (String child : children) {
81 delete(new File(file, child), undeletables);
82 }
83 }
84
85 if (!del(file)) {
86 undeletables.add(file.getAbsoluteFile());
87 }
88 }
89
90 private static boolean del(File file) {
91 for (int i = 0; i < 10; i++) {
92 if (file.delete() || !file.exists()) {
93 return true;
94 }
95 }
96 return false;
97 }
98
99 @Deprecated
100 public static boolean mkdirs(File directory) {
101 if (directory == null) {
102 return false;
103 }
104
105 if (directory.exists()) {
106 return false;
107 }
108 if (directory.mkdir()) {
109 return true;
110 }
111
112 File canonDir = null;
113 try {
114 canonDir = directory.getCanonicalFile();
115 } catch (IOException e) {
116 return false;
117 }
118
119 File parentDir = canonDir.getParentFile();
120 return (parentDir != null && (mkdirs(parentDir) || parentDir.exists()) && canonDir.mkdir());
121 }
122
123
124
125
126
127 @Deprecated
128 public static File createTempFile(String contents) throws IOException {
129 return createTempFile(contents.getBytes(StandardCharsets.UTF_8), 1);
130 }
131
132 @Deprecated
133
134
135
136
137 public static File createTempFile(byte[] pattern, int repeat) throws IOException {
138 mkdirs(TMP);
139 File tmpFile = File.createTempFile("tmpfile-", ".data", TMP);
140 writeBytes(tmpFile, pattern, repeat);
141 return tmpFile;
142 }
143
144
145
146
147
148
149
150
151 @Deprecated
152 public static File createTempDir() throws IOException {
153 return createTempDir("");
154 }
155
156
157
158
159
160
161
162
163 @Deprecated
164 public static File createTempDir(String suffix) throws IOException {
165 mkdirs(TMP);
166 File tmpFile = File.createTempFile("tmpdir-", suffix, TMP);
167 deleteFile(tmpFile);
168 mkdirs(tmpFile);
169 return tmpFile;
170 }
171
172 public static long copyFile(File source, File target) throws IOException {
173 long total = 0;
174
175 mkdirs(target.getParentFile());
176
177 try (FileInputStream fis = new FileInputStream(source);
178 OutputStream fos = new BufferedOutputStream(new FileOutputStream(target))) {
179 for (byte[] buffer = new byte[1024 * 32]; ; ) {
180 int bytes = fis.read(buffer);
181 if (bytes < 0) {
182 break;
183 }
184
185 fos.write(buffer, 0, bytes);
186
187 total += bytes;
188 }
189 }
190
191 return total;
192 }
193
194
195
196
197
198
199
200
201
202 @Deprecated
203 public static byte[] readBytes(File file) throws IOException {
204 try (RandomAccessFile in = new RandomAccessFile(file, "r")) {
205 byte[] actual = new byte[(int) in.length()];
206 in.readFully(actual);
207 return actual;
208 }
209 }
210
211 @Deprecated
212 public static void writeBytes(File file, byte[] pattern, int repeat) throws IOException {
213 file.deleteOnExit();
214 file.getParentFile().mkdirs();
215 try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file))) {
216 for (int i = 0; i < repeat; i++) {
217 out.write(pattern);
218 }
219 }
220 }
221
222 public static String readString(File file) throws IOException {
223 byte[] content = Files.readAllBytes(file.toPath());
224 return new String(content, StandardCharsets.UTF_8);
225 }
226
227 @Deprecated
228 public static void writeString(File file, String content) throws IOException {
229 writeBytes(file, content.getBytes(StandardCharsets.UTF_8), 1);
230 }
231
232 @Deprecated
233 public static void writeString(File file, String content, long timestamp) throws IOException {
234 writeBytes(file, content.getBytes(StandardCharsets.UTF_8), 1);
235 file.setLastModified(timestamp);
236 }
237
238 public static void readProps(File file, Properties props) throws IOException {
239 try (FileInputStream fis = new FileInputStream(file)) {
240 props.load(fis);
241 }
242 }
243
244 public static void writeProps(File file, Properties props) throws IOException {
245 file.getParentFile().mkdirs();
246
247 try (FileOutputStream fos = new FileOutputStream(file)) {
248 props.store(fos, "aether-test");
249 }
250 }
251 }