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 specified
41       * in file's path, then nothing is done and this returns gracefully.
42       *
43       * @param file file whose parent directories (if any) should be created
44       * @return {@code true} if either no parents were specified, or if all parent
45       *         directories were created successfully; {@code false} otherwise
46       */
47      static public boolean createMissingParentDirectories(File file) {
48          File parent = file.getParentFile();
49          if (parent == null) {
50              // Parent directory not specified, therefore it's a request to
51              // create nothing. Done! ;)
52              return true;
53          }
54  
55          // File.mkdirs() creates the parent directories only if they don't
56          // already exist; and it's okay if they do.
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                      // ignore
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                     // ignore
123                 }
124             }
125             if (bos != null) {
126                 try {
127                     bos.close();
128                 } catch (IOException e) {
129                     // ignore
130                 }
131             }
132         }
133     }
134 }