FileUtils.java

  1. /**
  2.  *
  3.  * Copyright the original author or authors
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smack.util;

  18. import java.io.BufferedReader;
  19. import java.io.File;
  20. import java.io.FileNotFoundException;
  21. import java.io.FileReader;
  22. import java.io.FileWriter;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.io.InputStreamReader;
  26. import java.io.Reader;
  27. import java.net.MalformedURLException;
  28. import java.net.URI;
  29. import java.util.ArrayList;
  30. import java.util.List;
  31. import java.util.Set;
  32. import java.util.logging.Level;
  33. import java.util.logging.Logger;

  34. public final class FileUtils {

  35.     private static final Logger LOGGER = Logger.getLogger(FileUtils.class.getName());

  36.     public static InputStream getStreamForUrl(String url, ClassLoader loader) throws MalformedURLException, IOException {
  37.         URI fileUri = URI.create(url);

  38.         if (fileUri.getScheme() == null) {
  39.             throw new MalformedURLException("No protocol found in file URL: " + url);
  40.         }

  41.         if (fileUri.getScheme().equals("classpath")) {
  42.             // Get an array of class loaders to try loading the providers files from.
  43.             List<ClassLoader> classLoaders = getClassLoaders();
  44.             if (loader != null) {
  45.                 classLoaders.add(0, loader);
  46.             }
  47.             for (ClassLoader classLoader : classLoaders) {
  48.                 InputStream is = classLoader.getResourceAsStream(fileUri.getSchemeSpecificPart());

  49.                 if (is != null) {
  50.                     return is;
  51.                 }
  52.             }
  53.         }
  54.         else {
  55.             return fileUri.toURL().openStream();
  56.         }
  57.         return null;
  58.     }

  59.     /**
  60.      * Returns default classloaders.
  61.      *
  62.      * @return a List of ClassLoader instances.
  63.      */
  64.     public static List<ClassLoader> getClassLoaders() {
  65.         ClassLoader[] classLoaders = new ClassLoader[2];
  66.         classLoaders[0] = FileUtils.class.getClassLoader();
  67.         classLoaders[1] = Thread.currentThread().getContextClassLoader();

  68.         // Clean up possible null values. Note that #getClassLoader may return a null value.
  69.         List<ClassLoader> loaders = new ArrayList<ClassLoader>(classLoaders.length);
  70.         for (ClassLoader classLoader : classLoaders) {
  71.             if (classLoader != null) {
  72.                 loaders.add(classLoader);
  73.             }
  74.         }
  75.         return loaders;
  76.     }

  77.     public static boolean addLines(String url, Set<String> set) throws MalformedURLException, IOException {
  78.         InputStream is = getStreamForUrl(url, null);
  79.         if (is == null) return false;
  80.         BufferedReader br = new BufferedReader(new InputStreamReader(is));
  81.         String line;
  82.         while ((line = br.readLine()) != null) {
  83.             set.add(line);
  84.         }
  85.         return true;
  86.     }

  87.     /**
  88.      * Reads the contents of a File
  89.      *
  90.      * @param file
  91.      * @return the content of file or null in case of an error
  92.      * @throws IOException
  93.      */
  94.     public static String readFileOrThrow(File file) throws FileNotFoundException, IOException {
  95.         Reader reader = null;
  96.         try {
  97.             reader = new FileReader(file);
  98.             char[] buf = new char[8192];
  99.             int len;
  100.             StringBuilder s = new StringBuilder();
  101.             while ((len = reader.read(buf)) >= 0) {
  102.                 s.append(buf, 0, len);
  103.             }
  104.             return s.toString();
  105.         }
  106.         finally {
  107.             if (reader != null) {
  108.                 reader.close();
  109.             }
  110.         }
  111.     }

  112.     public static String readFile(File file) {
  113.         try {
  114.             return readFileOrThrow(file);
  115.         } catch (FileNotFoundException e) {
  116.             LOGGER.log(Level.FINE, "readFile", e);
  117.         } catch (IOException e) {
  118.             LOGGER.log(Level.WARNING, "readFile", e);
  119.         }
  120.         return null;
  121.     }

  122.     public static void writeFileOrThrow(File file, String content) throws IOException {
  123.         FileWriter writer = new FileWriter(file, false);
  124.         writer.write(content);
  125.         writer.close();
  126.     }

  127.     public static boolean writeFile(File file, String content) {
  128.         try {
  129.             writeFileOrThrow(file, content);
  130.             return true;
  131.         }
  132.         catch (IOException e) {
  133.             LOGGER.log(Level.WARNING, "writeFile", e);
  134.             return false;
  135.         }
  136.     }
  137. }