Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Logback: the reliable, generic, fast and flexible logging framework. Copyright (C) 1999-2011, QOS.ch. All rights reserved. This program and the accompanying materials are dual-licensed under either the terms of the Eclipse Public License v1.0 as published by the Eclipse Foundation or (per the licensee's choosing) under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation.
package ch.qos.logback.core.util;
public class FileUtil {
  public static URL fileToURL(File file) {
    try {
      return file.toURI().toURL();
    } catch (MalformedURLException e) {
      throw new RuntimeException("Unexpected exception on file ["+file+"]"e);
    }
  }
  static public boolean isParentDirectoryCreationRequired(File file) {
    File parent = file.getParentFile();
    if (parent != null && !parent.exists()) {
      return true;
    } else {
      return false;
    }
  }
  static public boolean createMissingParentDirectories(File file) {
    File parent = file.getParentFile();
    if (parent == null) {
      throw new IllegalStateException(file + " should not have a null parent");
    }
    if (parent.exists()) {
      throw new IllegalStateException(file + " should not have existing parent directory");
    }
    return parent.mkdirs();
  }
  static public String resourceAsString(ContextAware caClassLoader classLoaderString resourceName) {
    URL url = classLoader.getResource(resourceName);
    if (url == null) {
      ca.addError("Failed to find resource [" + resourceName + "]");
      return null;
    }
    InputStreamReader isr = null;
    try {
      URLConnection urlConnection = url.openConnection();
      urlConnection.setUseCaches(false);
      isr = new InputStreamReader(urlConnection.getInputStream());
      char[] buf = new char[128];
      StringBuilder builder = new StringBuilder();
      int count = -1;
      while ((count = isr.read(buf, 0, buf.length)) != -1) {
        builder.append(buf, 0, count);
      }
      return builder.toString();
    } catch (IOException e) {
      ca.addError("Failled to open " + resourceNamee);
    } finally {
      if(isr != null) {
        try {
          isr.close();
        } catch (IOException e) {
          // ignore
        }
      }
    }
    return null;
  }
New to GrepCode? Check out our FAQ X