<commit_before>package org.safehaus.subutai.common.util;


import java.io.*;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;


public class FileUtil
{

    private static final Logger log = Logger.getLogger( FileUtil.class.getName() );


    public static File getFile( String fileName, Object object )
    {
        String currentPath = System.getProperty( "user.dir" ) + "/res/" + fileName;
        File file = new File( currentPath );
        if ( !file.exists() )
        {
            checkFolder( System.getProperty( "user.dir" ) + "/res/" );
            writeFile( fileName, object );
            file = new File( currentPath );
        }
        return file;
    }


    private static void checkFolder( String path )
    {
        File file = new File( path );
        if ( !file.exists() )
        {
            file.mkdir();
        }
    }


    private static void writeFile( String fileName, Object object )
    {

        try
        {
            String currentPath = System.getProperty( "user.dir" ) + "/res/" + fileName;
            InputStream inputStream = getClassLoader( object.getClass() ).getResourceAsStream( "img/" + fileName );
            OutputStream outputStream = new FileOutputStream( new File( currentPath ) );
            int read = 0;
            byte[] bytes = new byte[1024];

            while ( ( read = inputStream.read( bytes ) ) != -1 )
            {
                outputStream.write( bytes, 0, read );
            }

            inputStream.close();
            outputStream.close();
        }
        catch ( Exception ex )
        {
            ex.printStackTrace();
        }
    }


    private static URLClassLoader getClassLoader( Class clazz )
    {
        // Needed an instance to get URL, i.e. the static way doesn't work: FileUtil.class.getClass().
        URL url = clazz.getProtectionDomain().getCodeSource().getLocation();
        URLClassLoader classLoader =
            new URLClassLoader( new URL[] { url }, Thread.currentThread().getContextClassLoader() );

        return classLoader;
    }


    public static String getContent( String filePath, Object object )
    {

        if ( object != null )
        {
            return getContent( filePath, object.getClass() );
        }
        return null;
    }


    public static String getContent( String filePath, Class clazz )
    {
        String content = "";

        try
        {
            content = readFile( filePath, clazz );
        }
        catch ( Exception e )
        {
            log.log( Level.SEVERE, "Error while reading file: " + e );
        }

        return content;
    }


    private static String readFile( String filePath, Class clazz ) throws IOException
    {

        InputStream is = getClassLoader( clazz ).getResourceAsStream( filePath );
        String s = streamToString( is );
        is.close();

        return s;
    }


    private static String streamToString( InputStream is )
    {
        Scanner scanner = new Scanner( is ).useDelimiter( "\\A" );
        return scanner.hasNext() ? scanner.next() : "";
    }


    public static String readFile( String path, Charset encoding ) throws IOException
    {
        byte[] encoded = Files.readAllBytes( Paths.get( path ) );
        return new String( encoded, encoding );
    }
}<commit_msg>Core refactor<commit_after>package org.safehaus.subutai.common.util;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;


public class FileUtil {

    private static final Logger log = Logger.getLogger( FileUtil.class.getName() );


    public static File getFile( String fileName, Object object ) {
        String currentPath = System.getProperty( "user.dir" ) + "/res/" + fileName;
        File file = new File( currentPath );
        if ( !file.exists() ) {
            checkFolder( System.getProperty( "user.dir" ) + "/res/" );
            writeFile( fileName, object );
            file = new File( currentPath );
        }
        return file;
    }


    private static void checkFolder( String path ) {
        File file = new File( path );
        if ( !file.exists() ) {
            file.mkdir();
        }
    }


    private static void writeFile( String fileName, Object object ) {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            String currentPath = System.getProperty( "user.dir" ) + "/res/" + fileName;
            inputStream = getClassLoader( object.getClass() ).getResourceAsStream( "img/" + fileName );
            outputStream = new FileOutputStream( new File( currentPath ) );
            int read = 0;
            byte[] bytes = new byte[1024];

            while ( ( read = inputStream.read( bytes ) ) != -1 ) {
                outputStream.write( bytes, 0, read );
            }
        }
        catch ( Exception ex ) {
            log.log( Level.SEVERE, "Error while writing to file: " + ex );
        }
        finally {
            if ( inputStream != null ) {
                try {
                    inputStream.close();
                }
                catch ( IOException ignore ) {
                }
            }
            if ( outputStream != null ) {
                try {
                    outputStream.close();
                }
                catch ( IOException ignore ) {
                }
            }
        }
    }


    private static URLClassLoader getClassLoader( Class clazz ) {
        // Needed an instance to get URL, i.e. the static way doesn't work: FileUtil.class.getClass().
        URL url = clazz.getProtectionDomain().getCodeSource().getLocation();
        URLClassLoader classLoader =
                new URLClassLoader( new URL[] { url }, Thread.currentThread().getContextClassLoader() );

        return classLoader;
    }


    public static String getContent( String filePath, Object object ) {

        if ( object != null ) {
            return getContent( filePath, object.getClass() );
        }
        return null;
    }


    public static String getContent( String filePath, Class clazz ) {
        String content = "";

        try {
            content = readFile( filePath, clazz );
        }
        catch ( Exception e ) {
            log.log( Level.SEVERE, "Error while reading file: " + e );
        }

        return content;
    }


    private static String readFile( String filePath, Class clazz ) throws IOException {

        InputStream is = getClassLoader( clazz ).getResourceAsStream( filePath );
        String s = streamToString( is );
        is.close();

        return s;
    }


    private static String streamToString( InputStream is ) {
        Scanner scanner = new Scanner( is ).useDelimiter( "\\A" );
        return scanner.hasNext() ? scanner.next() : "";
    }


    public static String readFile( String path, Charset encoding ) throws IOException {
        byte[] encoded = Files.readAllBytes( Paths.get( path ) );
        return new String( encoded, encoding );
    }
}
{'fraction_non_alphanumeric': 0.10534084809447128, 'fraction_numerical': 0.001878690284487386, 'mean_word_length': 1.943522906793049, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4945206', 'n_tokens_mistral': 2018, 'n_tokens_neox': 1946, 'n_words': 563}