View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Provides utility methods to read and write (temporary) files.
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          // hide constructor
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      * @throws IOException if an I/O error occurs
125      * @deprecated use @TempDir (JUnit 5) Or TemporaryFolder (JUnit 4) instead
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      * @throws IOException if an I/O error occurs
135      * @deprecated use @TempDir (JUnit 5) Or TemporaryFolder (JUnit 4) instead
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      * Creates a temporary directory.
146      *
147      * @return the temporary directory
148      * @throws IOException if an I/O error occurs
149      * @deprecated use @TempDir (JUnit 5) Or TemporaryFolder (JUnit 4) instead
150      */
151     @Deprecated
152     public static File createTempDir() throws IOException {
153         return createTempDir("");
154     }
155 
156     /**
157      * Creates a temporary directory.
158      *
159      * @return the temporary directory
160      * @throws IOException if an I/O error occurs
161      * @deprecated use {@code @TempDir} (JUnit 5) or {@code TemporaryFolder} (JUnit 4) instead
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         FileInputStream fis = null;
176         OutputStream fos = null;
177         try {
178             fis = new FileInputStream(source);
179 
180             mkdirs(target.getParentFile());
181 
182             fos = new BufferedOutputStream(new FileOutputStream(target));
183 
184             for (byte[] buffer = new byte[1024 * 32]; ; ) {
185                 int bytes = fis.read(buffer);
186                 if (bytes < 0) {
187                     break;
188                 }
189 
190                 fos.write(buffer, 0, bytes);
191 
192                 total += bytes;
193             }
194 
195             fos.close();
196             fos = null;
197 
198             fis.close();
199             fis = null;
200         } finally {
201             try {
202                 if (fos != null) {
203                     fos.close();
204                 }
205             } catch (final IOException e) {
206                 // Suppressed due to an exception already thrown in the try block.
207             } finally {
208                 try {
209                     if (fis != null) {
210                         fis.close();
211                     }
212                 } catch (final IOException e) {
213                     // Suppressed due to an exception already thrown in the try block.
214                 }
215             }
216         }
217 
218         return total;
219     }
220 
221     /**
222      * Reads the contents of a file into a byte array.
223      *
224      * @param file the file to read
225      * @return the contents of the file as a byte array
226      * @throws IOException if an I/O error occurs
227      * @deprecated use {@code Files.readAllBytes(Path)} instead
228      */
229     @Deprecated
230     public static byte[] readBytes(File file) throws IOException {
231         RandomAccessFile in = null;
232         try {
233             in = new RandomAccessFile(file, "r");
234             byte[] actual = new byte[(int) in.length()];
235             in.readFully(actual);
236             in.close();
237             in = null;
238             return actual;
239         } finally {
240             try {
241                 if (in != null) {
242                     in.close();
243                 }
244             } catch (final IOException e) {
245                 // Suppressed due to an exception already thrown in the try block.
246             }
247         }
248     }
249 
250     @Deprecated
251     public static void writeBytes(File file, byte[] pattern, int repeat) throws IOException {
252         file.deleteOnExit();
253         file.getParentFile().mkdirs();
254         OutputStream out = null;
255         try {
256             out = new BufferedOutputStream(new FileOutputStream(file));
257             for (int i = 0; i < repeat; i++) {
258                 out.write(pattern);
259             }
260             out.close();
261             out = null;
262         } finally {
263             try {
264                 if (out != null) {
265                     out.close();
266                 }
267             } catch (final IOException e) {
268                 // Suppressed due to an exception already thrown in the try block.
269             }
270         }
271     }
272 
273     public static String readString(File file) throws IOException {
274         byte[] content = Files.readAllBytes(file.toPath());
275         return new String(content, StandardCharsets.UTF_8);
276     }
277 
278     @Deprecated
279     public static void writeString(File file, String content) throws IOException {
280         writeBytes(file, content.getBytes(StandardCharsets.UTF_8), 1);
281     }
282 
283     @Deprecated
284     public static void writeString(File file, String content, long timestamp) throws IOException {
285         writeBytes(file, content.getBytes(StandardCharsets.UTF_8), 1);
286         file.setLastModified(timestamp);
287     }
288 
289     public static void readProps(File file, Properties props) throws IOException {
290         FileInputStream fis = null;
291         try {
292             fis = new FileInputStream(file);
293             props.load(fis);
294             fis.close();
295             fis = null;
296         } finally {
297             try {
298                 if (fis != null) {
299                     fis.close();
300                 }
301             } catch (final IOException e) {
302                 // Suppressed due to an exception already thrown in the try block.
303             }
304         }
305     }
306 
307     public static void writeProps(File file, Properties props) throws IOException {
308         file.getParentFile().mkdirs();
309 
310         FileOutputStream fos = null;
311         try {
312             fos = new FileOutputStream(file);
313             props.store(fos, "aether-test");
314             fos.close();
315             fos = null;
316         } finally {
317             try {
318                 if (fos != null) {
319                     fos.close();
320                 }
321             } catch (final IOException e) {
322                 // Suppressed due to an exception already thrown in the try block.
323             }
324         }
325     }
326 }