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.util.ArrayList;
30  import java.util.Collection;
31  import java.util.Properties;
32  import java.util.UUID;
33  
34  /**
35   * Provides utility methods to read and write (temporary) files.
36   */
37  public class TestFileUtils {
38  
39      private static final File TMP = new File(
40              System.getProperty("java.io.tmpdir"),
41              "aether-" + UUID.randomUUID().toString().substring(0, 8));
42  
43      static {
44          Runtime.getRuntime().addShutdownHook(new Thread(() -> {
45              try {
46                  deleteFile(TMP);
47              } catch (IOException e) {
48                  e.printStackTrace();
49              }
50          }));
51      }
52  
53      private TestFileUtils() {
54          // hide constructor
55      }
56  
57      public static void deleteTempFiles() throws IOException {
58          deleteFile(TMP);
59      }
60  
61      public static void deleteFile(File file) throws IOException {
62          if (file == null) {
63              return;
64          }
65  
66          Collection<File> undeletables = new ArrayList<>();
67  
68          delete(file, undeletables);
69  
70          if (!undeletables.isEmpty()) {
71              throw new IOException("Failed to delete " + undeletables);
72          }
73      }
74  
75      private static void delete(File file, Collection<File> undeletables) {
76          String[] children = file.list();
77          if (children != null) {
78              for (String child : children) {
79                  delete(new File(file, child), undeletables);
80              }
81          }
82  
83          if (!del(file)) {
84              undeletables.add(file.getAbsoluteFile());
85          }
86      }
87  
88      private static boolean del(File file) {
89          for (int i = 0; i < 10; i++) {
90              if (file.delete() || !file.exists()) {
91                  return true;
92              }
93          }
94          return false;
95      }
96  
97      public static boolean mkdirs(File directory) {
98          if (directory == null) {
99              return false;
100         }
101 
102         if (directory.exists()) {
103             return false;
104         }
105         if (directory.mkdir()) {
106             return true;
107         }
108 
109         File canonDir = null;
110         try {
111             canonDir = directory.getCanonicalFile();
112         } catch (IOException e) {
113             return false;
114         }
115 
116         File parentDir = canonDir.getParentFile();
117         return (parentDir != null && (mkdirs(parentDir) || parentDir.exists()) && canonDir.mkdir());
118     }
119 
120     public static File createTempFile(String contents) throws IOException {
121         return createTempFile(contents.getBytes(StandardCharsets.UTF_8), 1);
122     }
123 
124     public static File createTempFile(byte[] pattern, int repeat) throws IOException {
125         mkdirs(TMP);
126         File tmpFile = File.createTempFile("tmpfile-", ".data", TMP);
127         writeBytes(tmpFile, pattern, repeat);
128         return tmpFile;
129     }
130 
131     public static File createTempDir() throws IOException {
132         return createTempDir("");
133     }
134 
135     public static File createTempDir(String suffix) throws IOException {
136         mkdirs(TMP);
137         File tmpFile = File.createTempFile("tmpdir-", suffix, TMP);
138         deleteFile(tmpFile);
139         mkdirs(tmpFile);
140         return tmpFile;
141     }
142 
143     public static long copyFile(File source, File target) throws IOException {
144         long total = 0;
145 
146         FileInputStream fis = null;
147         OutputStream fos = null;
148         try {
149             fis = new FileInputStream(source);
150 
151             mkdirs(target.getParentFile());
152 
153             fos = new BufferedOutputStream(new FileOutputStream(target));
154 
155             for (byte[] buffer = new byte[1024 * 32]; ; ) {
156                 int bytes = fis.read(buffer);
157                 if (bytes < 0) {
158                     break;
159                 }
160 
161                 fos.write(buffer, 0, bytes);
162 
163                 total += bytes;
164             }
165 
166             fos.close();
167             fos = null;
168 
169             fis.close();
170             fis = null;
171         } finally {
172             try {
173                 if (fos != null) {
174                     fos.close();
175                 }
176             } catch (final IOException e) {
177                 // Suppressed due to an exception already thrown in the try block.
178             } finally {
179                 try {
180                     if (fis != null) {
181                         fis.close();
182                     }
183                 } catch (final IOException e) {
184                     // Suppressed due to an exception already thrown in the try block.
185                 }
186             }
187         }
188 
189         return total;
190     }
191 
192     public static byte[] readBytes(File file) throws IOException {
193         RandomAccessFile in = null;
194         try {
195             in = new RandomAccessFile(file, "r");
196             byte[] actual = new byte[(int) in.length()];
197             in.readFully(actual);
198             in.close();
199             in = null;
200             return actual;
201         } finally {
202             try {
203                 if (in != null) {
204                     in.close();
205                 }
206             } catch (final IOException e) {
207                 // Suppressed due to an exception already thrown in the try block.
208             }
209         }
210     }
211 
212     public static void writeBytes(File file, byte[] pattern, int repeat) throws IOException {
213         file.deleteOnExit();
214         file.getParentFile().mkdirs();
215         OutputStream out = null;
216         try {
217             out = new BufferedOutputStream(new FileOutputStream(file));
218             for (int i = 0; i < repeat; i++) {
219                 out.write(pattern);
220             }
221             out.close();
222             out = null;
223         } finally {
224             try {
225                 if (out != null) {
226                     out.close();
227                 }
228             } catch (final IOException e) {
229                 // Suppressed due to an exception already thrown in the try block.
230             }
231         }
232     }
233 
234     public static String readString(File file) throws IOException {
235         byte[] content = readBytes(file);
236         return new String(content, StandardCharsets.UTF_8);
237     }
238 
239     public static void writeString(File file, String content) throws IOException {
240         writeBytes(file, content.getBytes(StandardCharsets.UTF_8), 1);
241     }
242 
243     public static void writeString(File file, String content, long timestamp) throws IOException {
244         writeBytes(file, content.getBytes(StandardCharsets.UTF_8), 1);
245         file.setLastModified(timestamp);
246     }
247 
248     public static void readProps(File file, Properties props) throws IOException {
249         FileInputStream fis = null;
250         try {
251             fis = new FileInputStream(file);
252             props.load(fis);
253             fis.close();
254             fis = null;
255         } finally {
256             try {
257                 if (fis != null) {
258                     fis.close();
259                 }
260             } catch (final IOException e) {
261                 // Suppressed due to an exception already thrown in the try block.
262             }
263         }
264     }
265 
266     public static void writeProps(File file, Properties props) throws IOException {
267         file.getParentFile().mkdirs();
268 
269         FileOutputStream fos = null;
270         try {
271             fos = new FileOutputStream(file);
272             props.store(fos, "aether-test");
273             fos.close();
274             fos = null;
275         } finally {
276             try {
277                 if (fos != null) {
278                     fos.close();
279                 }
280             } catch (final IOException e) {
281                 // Suppressed due to an exception already thrown in the try block.
282             }
283         }
284     }
285 }