<commit_before>//--------------------------------------
//  <NAME>
//  Auto-Updater
//  github.com/denkers/auto-updater
//--------------------------------------

package com.denker.updater.io;

import org.apache.commons.net.ftp.FTPClient;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import net.lingala.zip4j.core.ZipFile;

public class UpdateLoader
{
    private AppVersion clientVersion, serverVersion;
    private FTPClient client;
    
    public UpdateLoader()
    {
        initClient();
        initVersions();
    }
    
    private void initClient()
    {
        try
        {
            ClientConnector connector = ClientConnector.getInstance();
            connector.connect();
            client    =   connector.getClient();
        }
        
        catch(IOException e)
        {
            System.out.println(e.getMessage());
        }
    }

    private void initServerVersion()
    {
        try
        {
            InputStream is      =   client.retrieveFileStream("version.xml");
            serverVersion       =   AppVersion.getVersionFromFile(is);
        }

        catch(IOException e)
        {
            System.out.println(e.getMessage());
        }
    }
    
    public boolean isConnected()
    {
        return client != null && client.isConnected();
    }

    protected void initClientVersion()
    {
        try
        {
            String path     =   FTPConfig.getInstance().getVersionPath();
            File file       =   new File(path);
            clientVersion   =   AppVersion.getVersionFromFile(new FileInputStream(file));
        }

        catch(FileNotFoundException e)
        {
            System.out.println("[Error] Version file not found");
        }
    }

    private void initVersions()
    {
        if(isConnected())
        {
            initServerVersion();
            initClientVersion();
        }
    }
    
    private boolean downloadPatchZip(String patchFile)
    {
        try
        {
            client.setFileType(FTPClient.BINARY_FILE_TYPE);
            final String PATCH_DIR          =   FTPConfig.getInstance().getPatchDirectory();
            File output                     =   new File(PATCH_DIR + "/" + patchFile);
            try (FileOutputStream outputStream = new FileOutputStream(output.getAbsolutePath())) 
            {
                client.retrieveFile(patchFile, outputStream);
            }

            return output.exists();
        }
        
        catch(IOException e)
        {
            e.printStackTrace();
            return false;
        }
    }
    
   private boolean unpackPatch(String patchFile)
   {
       try
       {
           final String PATCH_DIR   =   FTPConfig.getInstance().getPatchDirectory();
           final String PATCH_PATH  =   PATCH_DIR + patchFile;
           final String OUT_DIR     =   FTPConfig.getInstance().getOutputDirectory();
           String absPatchPath      =   new File(PATCH_PATH).getAbsolutePath();
           
           ZipFile zFile            =   new ZipFile(absPatchPath);
           zFile.extractAll(OUT_DIR);
           
           return true;
       }
       
       catch(Exception e)
       {
           e.printStackTrace();
           return false;
       }
   }
   
   private boolean removePatchFile(String patchFile)
   {
        final String PATCH_DIR   =   FTPConfig.getInstance().getPatchDirectory();
        
        try
        {
            Path path                =   Paths.get(PATCH_DIR, patchFile);
            Files.delete(path);
            return true;
        }
        
        catch(IOException e)
        {
            e.printStackTrace();
            return false;
        }
   }

    public boolean hasUpdateAvailable()
    {
        if(clientVersion == null || serverVersion == null)
            return false;

        else
            return clientVersion.getBuildID() < serverVersion.getBuildID();
    }

    public void update()
    {
        if(hasUpdateAvailable())
        {
//            downloadPatchZip("testzip.zip");
            unpackPatch("testzip.zip");
            //removePatchFile("testzip.zip");
        }
    }

    public static void main(String[] args)
    {
        UpdateLoader loader =   new UpdateLoader();
        loader.update();
    }
}

<commit_msg>Updated io.UpdateLoader to throw io.UpdateException on version checking and downloading/unpacking/removing patches<commit_after>//--------------------------------------
//  <NAME>
//  Auto-Updater
//  github.com/denkers/auto-updater
//--------------------------------------

package com.denker.updater.io;

import com.denker.updater.io.UpdateException.ErrorCode;
import org.apache.commons.net.ftp.FTPClient;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import net.lingala.zip4j.core.ZipFile;

public class UpdateLoader
{
    private AppVersion clientVersion, serverVersion;
    private FTPClient client;
    
    public UpdateLoader() throws UpdateException
    {
        initClient();
        initVersions();
    }
    
    private void initClient() throws UpdateException
    {
        ClientConnector connector = ClientConnector.getInstance();
        connector.connect();
        client    =   connector.getClient();
    }

    private void initServerVersion() throws UpdateException
    {
        try
        {
            InputStream is      =   client.retrieveFileStream("version.xml");
            serverVersion       =   AppVersion.getVersionFromFile(is);
        }

        catch(IOException e)
        {
            throw new UpdateException(ErrorCode.SVERSION_CHECK_FAIL);
        }
    }
    
    public boolean isConnected()
    {
        return client != null && client.isConnected();
    }

    protected void initClientVersion() throws UpdateException
    {
        try
        {
            String path     =   FTPConfig.getInstance().getVersionPath();
            File file       =   new File(path);
            clientVersion   =   AppVersion.getVersionFromFile(new FileInputStream(file));
        }

        catch(IOException e)
        {
            throw new UpdateException(ErrorCode.CVERSION_CHECK_FAIL);
        }
    }

    private void initVersions() throws UpdateException
    {
        if(isConnected())
        {
            initServerVersion();
            initClientVersion();
        }
    }
    
    private void downloadPatchZip(String patchFile) throws UpdateException
    {
        try
        {
            client.setFileType(FTPClient.BINARY_FILE_TYPE);
            final String PATCH_DIR          =   FTPConfig.getInstance().getPatchDirectory();
            File output                     =   new File(PATCH_DIR + "/" + patchFile);
            try (FileOutputStream outputStream = new FileOutputStream(output.getAbsolutePath())) 
            {
                client.retrieveFile(patchFile, outputStream);
            }

            if(!output.exists())
                throw new UpdateException(ErrorCode.PATCH_DL_ERR);
        }
        
        catch(IOException e)
        {
            throw new UpdateException(ErrorCode.PATCH_DL_ERR);
        }
    }
    
   private void unpackPatch(String patchFile) throws UpdateException
   {
       try
       {
           final String PATCH_DIR   =   FTPConfig.getInstance().getPatchDirectory();
           final String PATCH_PATH  =   PATCH_DIR + patchFile;
           final String OUT_DIR     =   FTPConfig.getInstance().getOutputDirectory();
           String absPatchPath      =   new File(PATCH_PATH).getAbsolutePath();
           
           ZipFile zFile            =   new ZipFile(absPatchPath);
           zFile.extractAll(OUT_DIR);
       }
       
       catch(Exception e)
       {
           throw new UpdateException(ErrorCode.UNPACK_PATCH_ERR);
       }
   }
   
   private void removePatchFile(String patchFile) throws UpdateException
   {
        final String PATCH_DIR   =   FTPConfig.getInstance().getPatchDirectory();
        
        try
        {
            Path path                =   Paths.get(PATCH_DIR, patchFile);
            Files.delete(path);
        }
        
        catch(IOException e)
        {
            throw new UpdateException(ErrorCode.REMOVE_PATCH_ERR);
        }
   }

    public boolean hasUpdateAvailable()
    {
        if(clientVersion == null || serverVersion == null)
            return false;

        else
            return clientVersion.getBuildID() < serverVersion.getBuildID();
    }

    public void update()
    {
        if(hasUpdateAvailable())
        {
//            downloadPatchZip("testzip.zip");
            unpackPatch("testzip.zip");
            //removePatchFile("testzip.zip");
        }
    }

    public static void main(String[] args)
    {
        UpdateLoader loader =   new UpdateLoader();
        loader.update();
    }
}


{'fraction_non_alphanumeric': 0.10640216411181244, 'fraction_numerical': 0.0002254283137962128, 'mean_word_length': 1.6376337693222354, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6198079', 'n_tokens_mistral': 2324, 'n_tokens_neox': 2287, 'n_words': 432}