View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
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       * Creates the parent directories of a file. If parent directories not
41       * specified in file's path, then nothing is done and this returns
42       * gracefully.
43       *
44       * @param file file whose parent directories (if any) should be created
45       * @return {@code true} if either no parents were specified, or if all
46       * parent directories were created successfully; {@code false} otherwise
47       */
48      static public boolean createMissingParentDirectories(File file) {
49          File parent = file.getParentFile();
50          if (parent == null) {
51              // Parent directory not specified, therefore it's a request to
52              // create nothing. Done! ;)
53              return true;
54          }
55  
56          // File.mkdirs() creates the parent directories only if they don't
57          // already exist; and it's okay if they do.
58          parent.mkdirs();
59          return parent.exists();
60      }
61  
62      public String resourceAsString(ClassLoader classLoader, String resourceName) {
63          URL url = classLoader.getResource(resourceName);
64          if (url == null) {
65              addError("Failed to find resource [" + resourceName + "]");
66              return null;
67          }
68  
69          InputStreamReader isr = null;
70          try {
71              URLConnection urlConnection = url.openConnection();
72              urlConnection.setUseCaches(false);
73              isr = new InputStreamReader(urlConnection.getInputStream());
74              char[] buf = new char[128];
75              StringBuilder builder = new StringBuilder();
76              int count = -1;
77              while ((count = isr.read(buf, 0, buf.length)) != -1) {
78                  builder.append(buf, 0, count);
79              }
80              return builder.toString();
81          } catch (IOException e) {
82              addError("Failed to open " + resourceName, e);
83          } finally {
84              if (isr != null) {
85                  try {
86                      isr.close();
87                  } catch (IOException e) {
88                      // ignore
89                  }
90              }
91          }
92          return null;
93      }
94  
95      static final int BUF_SIZE = 32 * 1024;
96  
97      public void copy(String src, String destination) throws RolloverFailure {
98          BufferedInputStream bis = null;
99          BufferedOutputStream bos = null;
100         try {
101             bis = new BufferedInputStream(new FileInputStream(src));
102             bos = new BufferedOutputStream(new FileOutputStream(destination));
103             byte[] inbuf = new byte[BUF_SIZE];
104             int n;
105 
106             while ((n = bis.read(inbuf)) != -1) {
107                 bos.write(inbuf, 0, n);
108             }
109 
110             bis.close();
111             bis = null;
112             bos.close();
113             bos = null;
114         } catch (IOException ioe) {
115             String msg = "Failed to copy [" + src + "] to [" + destination + "]";
116             addError(msg, ioe);
117             throw new RolloverFailure(msg);
118         } finally {
119             if (bis != null) {
120                 try {
121                     bis.close();
122                 } catch (IOException e) {
123                     // ignore
124                 }
125             }
126             if (bos != null) {
127                 try {
128                     bos.close();
129                 } catch (IOException e) {
130                     // ignore
131                 }
132             }
133         }
134     }
135 }