<commit_before>package org.jboss.forge.furnace.se;

import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

public class BootstrapClassLoader extends URLClassLoader
{
   public BootstrapClassLoader(String bootstrapPath)
   {
      super(new JarLocator(bootstrapPath).find(), null);
   }

   private static class JarLocator
   {
      private String path;

      public JarLocator(String path)
      {
         this.path = path;
      }

      private URL[] find()
      {
         List<URL> result = new ArrayList<URL>();
         try
         {
            for (URL url : Collections.list(JarLocator.class.getClassLoader().getResources(path)))
            {
               String urlPath = url.getFile();
               urlPath = URLDecoder.decode(urlPath, "UTF-8");
               if (urlPath.startsWith("file:"))
               {
                  urlPath = urlPath.substring(5);
               }
               if (urlPath.indexOf('!') > 0)
               {
                  urlPath = urlPath.substring(0, urlPath.indexOf('!'));
               }
               result.add(new URL(url, "."));
               result.addAll(handle(urlPath, url));
            }
         }
         catch (Exception e)
         {
            throw new RuntimeException("Could not load jars from " + path, e);
         }

         return result.toArray(new URL[0]);
      }

      private List<URL> handle(String urlPath, URL original) throws IOException
      {
         File file = new File(urlPath);
         if (file.exists())
            if (file.isDirectory())
            {
               return handle(file);
            }
            else
            {
               return handleArchiveByFile(file, original);
            }
         else
            throw new FileNotFoundException(urlPath);
      }

      private List<URL> handle(File file)
      {
         List<URL> result = new ArrayList<URL>();
         for (File child : file.listFiles())
         {
            if (!child.isDirectory() && child.getName().endsWith(".jar"))
            {
               try
               {
                  result.add(child.toURI().toURL());
               }
               catch (MalformedURLException e)
               {
                  throw new RuntimeException("Could not convert to URL " + child, e);
               }
            }
         }
         return result;
      }

      @SuppressWarnings("deprecation")
      private List<URL> handleArchiveByFile(File file, URL url) throws IOException
      {
         List<URL> result = new ArrayList<URL>();
         try
         {
            ZipFile zip = new ZipFile(file);
            Enumeration<? extends ZipEntry> entries = zip.entries();
            while (entries.hasMoreElements())
            {
               ZipEntry entry = entries.nextElement();
               String name = entry.getName();
               if (name.matches(path + "/.*\\.jar"))
               {
                  result.add(
                           copy(entry.getName(),
                                    JarLocator.class.getClassLoader().getResource(name).openStream()
                           ).toURL()
                           );<commit_msg>Support VFS environments in Furnace SE<commit_after>package org.jboss.forge.furnace.se;

import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

import org.jboss.forge.furnace.util.OperatingSystemUtils;

public class BootstrapClassLoader extends URLClassLoader
{
   private static final Logger log = Logger.getLogger(BootstrapClassLoader.class.getName());

   public BootstrapClassLoader(String bootstrapPath)
   {
      super(new JarLocator(bootstrapPath).find(), null);
   }

   private static class JarLocator
   {
      private String path;

      public JarLocator(String path)
      {
         this.path = path;
      }

      private URL[] find()
      {
         List<URL> result = new ArrayList<URL>();
         try
         {
            for (URL url : Collections.list(JarLocator.class.getClassLoader().getResources(path)))
            {
               String urlPath = url.getFile();
               urlPath = URLDecoder.decode(urlPath, "UTF-8");
               if (urlPath.startsWith("file:"))
               {
                  urlPath = urlPath.substring(5);
               }
               if (urlPath.indexOf('!') > 0)
               {
                  urlPath = urlPath.substring(0, urlPath.indexOf('!'));
               }
               result.add(new URL(url, "."));
               result.addAll(handle(urlPath, url));
            }
         }
         catch (Exception e)
         {
            throw new RuntimeException("Could not load jars from " + path, e);
         }

         return result.toArray(new URL[0]);
      }

      private List<URL> handle(String urlPath, URL original) throws IOException
      {
         List<URL> result = new ArrayList<URL>();
         File file = new File(urlPath);
         if (file.isDirectory())
            result = handle(file);
         else if (file.isFile())
            result = handleZipFile(file);
         else
            result = handleZipStream(original);
         return result;
      }

      private List<URL> handleZipStream(URL original) throws IOException, FileNotFoundException
      {
         List<URL> result = new ArrayList<URL>();
         InputStream stream = original.openStream();
         if (stream instanceof ZipInputStream)
         {
            File tempDir = OperatingSystemUtils.createTempDir();
            ZipEntry entry;
            while ((entry = ((ZipInputStream) stream).getNextEntry()) != null)
            {
               if (entry.getName().matches(".*\\.jar$"))
               {
                  log.log(Level.FINE, String.format("ZipEntry detected: %s len %d added %TD",
                           original.toExternalForm() + entry.getName(), entry.getSize(),
                           new Date(entry.getTime())));

                  FileOutputStream output = null;
                  try
                  {
                     byte[] buffer = new byte[2048];
                     output = new FileOutputStream(new File(tempDir, entry.getName()));
                     int len = 0;
                     while ((len = stream.read(buffer)) > 0)
                     {
                        output.write(buffer, 0, len);
                     }
                  }
                  finally
                  {
                     if (output != null)
                        output.close();
                  }
               }
            }
            result = handle(tempDir);
         }
         return result;
      }

      private List<URL> handle(File file)
      {
         List<URL> result = new ArrayList<URL>();
         for (File child : file.listFiles())
         {
            if (!child.isDirectory() && child.getName().endsWith(".jar"))
            {
               try
               {
                  log.log(Level.FINE, "File entry detected: " + child.getAbsolutePath());
                  result.add(child.toURI().toURL());
               }
               catch (MalformedURLException e)
               {
                  throw new RuntimeException("Could not convert to URL " + child, e);
               }
            }
         }
         return result;
      }

      @SuppressWarnings("deprecation")
      private List<URL> handleZipFile(File file) throws IOException
      {
         List<URL> result = new ArrayList<URL>();
         try
         {
            ZipFile zip = new ZipFile(file);
            Enumeration<? extends ZipEntry> entries = zip.entries();
            while (entries.hasMoreElements())
            {
               ZipEntry entry = entries.nextElement();
               String name = entry.getName();
               if (name.matches(path + "/.*\\.jar"))
               {
                  log.log(Level.FINE, String.format("ZipEntry detected: %s len %d added %TD",
                           file.getAbsolutePath() + "/" + entry.getName(), entry.getSize(),
                           new Date(entry.getTime())));

                  result.add(copy(entry.getName(),
                           JarLocator.class.getClassLoader().getResource(name).openStream()
                           ).toURL());
{'fraction_non_alphanumeric': 0.10946617678841582, 'fraction_numerical': 0.001837043440674303, 'mean_word_length': 1.5859178541492036, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5108057', 'n_tokens_mistral': 2416, 'n_tokens_neox': 2326, 'n_words': 517}