1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  package ch.qos.logback.core.util;
15  
16  import ch.qos.logback.core.Context;
17  import ch.qos.logback.core.rolling.RolloverFailure;
18  import ch.qos.logback.core.spi.ContextAwareBase;
19  
20  import java.io.*;
21  import java.net.MalformedURLException;
22  import java.net.URL;
23  import java.net.URLConnection;
24  
25  public class FileUtil extends ContextAwareBase {
26  
27      public FileUtil(Context context) {
28          setContext(context);
29      }
30  
31      public static URL fileToURL(File file) {
32          try {
33              return file.toURI().toURL();
34          } catch (MalformedURLException e) {
35              throw new RuntimeException("Unexpected exception on file [" + file + "]", e);
36          }
37      }
38  
39      
40  
41  
42  
43  
44  
45  
46  
47      static public boolean createMissingParentDirectories(File file) {
48          File parent = file.getParentFile();
49          if (parent == null) {
50              
51              
52              return true;
53          }
54  
55          
56          
57          parent.mkdirs();
58          return parent.exists();
59      }
60  
61      public String resourceAsString(ClassLoader classLoader, String resourceName) {
62          URL url = classLoader.getResource(resourceName);
63          if (url == null) {
64              addError("Failed to find resource [" + resourceName + "]");
65              return null;
66          }
67  
68          InputStreamReader isr = null;
69          try {
70              URLConnection urlConnection = url.openConnection();
71              urlConnection.setUseCaches(false);
72              isr = new InputStreamReader(urlConnection.getInputStream());
73              char[] buf = new char[128];
74              StringBuilder builder = new StringBuilder();
75              int count = -1;
76              while ((count = isr.read(buf, 0, buf.length)) != -1) {
77                  builder.append(buf, 0, count);
78              }
79              return builder.toString();
80          } catch (IOException e) {
81              addError("Failed to open " + resourceName, e);
82          } finally {
83              if (isr != null) {
84                  try {
85                      isr.close();
86                  } catch (IOException e) {
87                      
88                  }
89              }
90          }
91          return null;
92      }
93  
94      static final int BUF_SIZE = 32 * 1024;
95  
96      public void copy(String src, String destination) throws RolloverFailure {
97          BufferedInputStream bis = null;
98          BufferedOutputStream bos = null;
99          try {
100             bis = new BufferedInputStream(new FileInputStream(src));
101             bos = new BufferedOutputStream(new FileOutputStream(destination));
102             byte[] inbuf = new byte[BUF_SIZE];
103             int n;
104 
105             while ((n = bis.read(inbuf)) != -1) {
106                 bos.write(inbuf, 0, n);
107             }
108 
109             bis.close();
110             bis = null;
111             bos.close();
112             bos = null;
113         } catch (IOException ioe) {
114             String msg = "Failed to copy [" + src + "] to [" + destination + "]";
115             addError(msg, ioe);
116             throw new RolloverFailure(msg);
117         } finally {
118             if (bis != null) {
119                 try {
120                     bis.close();
121                 } catch (IOException e) {
122                     
123                 }
124             }
125             if (bos != null) {
126                 try {
127                     bos.close();
128                 } catch (IOException e) {
129                     
130                 }
131             }
132         }
133     }
134 }