<commit_before>package net.rubygrapefruit.platform;

import net.rubygrapefruit.platform.internal.*;
import net.rubygrapefruit.platform.internal.jni.NativeLibraryFunctions;

import java.io.*;
import java.net.URL;

/**
 * Provides access to the native integrations. Use {@link #get(Class)} to load a particular integration.
 */
public class Native {
    private static final Object lock = new Object();
    private static boolean loaded;

    private Native() {
    }

    /**
     * Initialises the native integration, if not already initialized.
     *
     * @param extractDir The directory to extract native resources into. May be null, in which case a default is
     * selected.
     */
    static public void init(File extractDir) {
        synchronized (lock) {
            if (!loaded) {
                Platform platform = Platform.current();
                try {
                    File libFile;
                    URL resource = Native.class.getClassLoader().getResource(platform.getLibraryName());
                    if (resource != null) {
                        File libDir = extractDir;
                        if (libDir == null) {
                            libDir = File.createTempFile("native-platform", "dir");
                            libDir.delete();
                            libDir.mkdirs();
                        }
                        libFile = new File(libDir, platform.getLibraryName());
                        libFile.deleteOnExit();
                        copy(resource, libFile);
                    } else {
                        libFile = new File("build/binaries/" + platform.getLibraryName());
                    }
                    System.load(libFile.getCanonicalPath());
                    int nativeVersion = NativeLibraryFunctions.getVersion();
                    if (nativeVersion != NativeLibraryFunctions.VERSION) {
                        throw new NativeException(String.format(
                                "Unexpected native library version loaded. Expected %s, was %s.", nativeVersion,
                                NativeLibraryFunctions.VERSION));
                    }
                    loaded = true;
                } catch (NativeException e) {
                    throw e;
                } catch (Exception e) {
                    throw new NativeException("Failed to initialise native integration.", e);
                }
            }
        }
    }

    /**
     * Locates a native integration of the given type.
     *
     * @return The native integration.
     * @throws NativeIntegrationUnavailableException When the given native integration is not available on the current
     * machine.
     * @throws NativeException On failure to load the native integration.
     */
    public static <T extends NativeIntegration> T get(Class<T> type)
            throws NativeIntegrationUnavailableException, NativeException {
        init(null);
        try {
            Platform platform = Platform.current();
            return platform.get(type);
        } catch (NativeException e) {
            throw e;
        } catch (Exception e) {
            throw new NativeException(String.format("Failed to load native integration %s.", type.getSimpleName()), e);
        }
    }
<commit_msg>Improved error handling for case where native library for current architecture is not available.<commit_after>package net.rubygrapefruit.platform;

import net.rubygrapefruit.platform.internal.*;
import net.rubygrapefruit.platform.internal.jni.NativeLibraryFunctions;

import java.io.*;
import java.net.URL;

/**
 * Provides access to the native integrations. Use {@link #get(Class)} to load a particular integration.
 */
public class Native {
    private static final Object lock = new Object();
    private static boolean loaded;

    private Native() {
    }

    /**
     * Initialises the native integration, if not already initialized.
     *
     * @param extractDir The directory to extract native resources into. May be null, in which case a default is
     * selected.
     */
    static public void init(File extractDir) {
        synchronized (lock) {
            if (!loaded) {
                Platform platform = Platform.current();
                try {
                    File libFile;
                    URL resource = Native.class.getClassLoader().getResource(platform.getLibraryName());
                    if (resource != null) {
                        File libDir = extractDir;
                        if (libDir == null) {
                            libDir = File.createTempFile("native-platform", "dir");
                            libDir.delete();
                            libDir.mkdirs();
                        }
                        libFile = new File(libDir, platform.getLibraryName());
                        libFile.deleteOnExit();
                        copy(resource, libFile);
                    } else {
                        libFile = new File("build/binaries/" + platform.getLibraryName());
                    }
                    if (!libFile.isFile()) {
                        throw new NativeIntegrationUnavailableException(String.format("Native library is not available for this operating system and architecture."));
                    }
                    System.load(libFile.getCanonicalPath());
                    int nativeVersion = NativeLibraryFunctions.getVersion();
                    if (nativeVersion != NativeLibraryFunctions.VERSION) {
                        throw new NativeException(String.format(
                                "Unexpected native library version loaded. Expected %s, was %s.", nativeVersion,
                                NativeLibraryFunctions.VERSION));
                    }
                    loaded = true;
                } catch (NativeException e) {
                    throw e;
                } catch (Throwable t) {
                    throw new NativeException("Failed to initialise native integration.", t);
                }
            }
        }
    }

    /**
     * Locates a native integration of the given type.
     *
     * @return The native integration.
     * @throws NativeIntegrationUnavailableException When the given native integration is not available on the current
     * machine.
     * @throws NativeException On failure to load the native integration.
     */
    public static <T extends NativeIntegration> T get(Class<T> type)
            throws NativeIntegrationUnavailableException, NativeException {
        init(null);
        try {
            Platform platform = Platform.current();
            return platform.get(type);
        } catch (NativeException e) {
            throw e;
        } catch (Throwable t) {
            throw new NativeException(String.format("Failed to load native integration %s.", type.getSimpleName()), t);
        }
    }

{'fraction_non_alphanumeric': 0.08353951389899578, 'fraction_numerical': 0.0, 'mean_word_length': 1.6461301501732768, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '3430675', 'n_tokens_mistral': 1565, 'n_tokens_neox': 1492, 'n_words': 470}