package org.jivesoftware.smack.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

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

   public static InputStream getStreamForUrl(String url, ClassLoader loader) throws MalformedURLException, IOException {
      URI fileUri = URI.create(url);
      if (fileUri.getScheme() == null) {
         throw new MalformedURLException("No protocol found in file URL: " + url);
      } else if (fileUri.getScheme().equals("classpath")) {
         List<ClassLoader> classLoaders = getClassLoaders();
         if (loader != null) {
            classLoaders.add(0, loader);
         }

         Iterator i$ = classLoaders.iterator();

         InputStream is;
         do {
            if (!i$.hasNext()) {
               return null;
            }

            ClassLoader classLoader = (ClassLoader)i$.next();
            is = classLoader.getResourceAsStream(fileUri.getSchemeSpecificPart());
         } while(is == null);

         return is;
      } else {
         return fileUri.toURL().openStream();
      }
   }

   public static List<ClassLoader> getClassLoaders() {
      ClassLoader[] classLoaders = new ClassLoader[]{FileUtils.class.getClassLoader(), Thread.currentThread().getContextClassLoader()};
      List<ClassLoader> loaders = new ArrayList(classLoaders.length);
      ClassLoader[] arr$ = classLoaders;
      int len$ = classLoaders.length;

      for(int i$ = 0; i$ < len$; ++i$) {
         ClassLoader classLoader = arr$[i$];
         if (classLoader != null) {
            loaders.add(classLoader);
         }
      }

      return loaders;
   }

   public static boolean addLines(String url, Set<String> set) throws MalformedURLException, IOException {
      InputStream is = getStreamForUrl(url, (ClassLoader)null);
      if (is == null) {
         return false;
      } else {
         BufferedReader br = new BufferedReader(new InputStreamReader(is));

         String line;
         while((line = br.readLine()) != null) {
            set.add(line);
         }

         return true;
      }
   }

   public static String readFileOrThrow(File file) throws FileNotFoundException, IOException {
      FileReader reader = null;

      try {
         reader = new FileReader(file);
         char[] buf = new char[8192];
         StringBuilder s = new StringBuilder();

         int len;
         while((len = reader.read(buf)) >= 0) {
            s.append(buf, 0, len);
         }

         String var5 = s.toString();
         return var5;
      } finally {
         if (reader != null) {
            reader.close();
         }

      }
   }

   public static String readFile(File file) {
      try {
         return readFileOrThrow(file);
      } catch (FileNotFoundException var2) {
         LOGGER.log(Level.FINE, "readFile", var2);
      } catch (IOException var3) {
         LOGGER.log(Level.WARNING, "readFile", var3);
      }

      return null;
   }

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

   public static boolean writeFile(File file, String content) {
      try {
         writeFileOrThrow(file, content);
         return true;
      } catch (IOException var3) {
         LOGGER.log(Level.WARNING, "writeFile", var3);
         return false;
      }
   }
}
