private static File extractLibraryFile()

in src/main/java/org/apache/commons/crypto/NativeCodeLoader.java [142:199]


    private static File extractLibraryFile(final String libFolderForCurrentOS, final String libraryFileName,
            final String targetFolder) {
        final String nativeLibraryFilePath = libFolderForCurrentOS + File.separator + libraryFileName;

        // Attach UUID to the native library file to ensure multiple class loaders
        // can read the libcommons-crypto multiple times.
        final UUID uuid = UUID.randomUUID();
        final String extractedLibFileName = String.format("commons-crypto-%s-%s", uuid, libraryFileName);
        final File extractedLibFile = new File(targetFolder, extractedLibFileName);
        debug("Extracting '%s' to '%s'...", nativeLibraryFilePath, extractedLibFile);
        try (InputStream inputStream = NativeCodeLoader.class.getResourceAsStream(nativeLibraryFilePath)) {
            if (inputStream == null) {
                debug("Resource not found: %s", nativeLibraryFilePath);
                return null;
            }
            // Extract a native library file into the target directory
            final Path path;
            try {
                path = extractedLibFile.toPath();
                final long byteCount = Files.copy(inputStream, path, StandardCopyOption.REPLACE_EXISTING);
                if (isDebug()) {
                    debug("Extracted '%s' to '%s': %,d bytes [%s]", nativeLibraryFilePath, extractedLibFile, byteCount,
                            Files.isExecutable(path) ? "X+" : "X-");
                    final PosixFileAttributes attributes = Files.readAttributes(path, PosixFileAttributes.class);
                    if (attributes != null) {
                        debug("Attributes '%s': %s %s %s", extractedLibFile, attributes.permissions(),
                                attributes.owner(), attributes.group());
                    }
                }
            } finally {
                // Delete the extracted lib file on JVM exit.
                debug("Delete on exit: %s", extractedLibFile);
                extractedLibFile.deleteOnExit();
            }

            // Set executable (x) flag to enable Java to load the native library
            if (!extractedLibFile.setReadable(true) || !extractedLibFile.setExecutable(true)
                    || !extractedLibFile.setWritable(true, true)) {
                throw new IllegalStateException("Invalid path for library path " + extractedLibFile);
            }

            // Check whether the contents are properly copied from the resource
            // folder
            try (InputStream nativeInputStream = NativeCodeLoader.class.getResourceAsStream(nativeLibraryFilePath)) {
                try (InputStream extractedLibIn = Files.newInputStream(path)) {
                    debug("Validating '%s'...", extractedLibFile);
                    if (!contentsEquals(nativeInputStream, extractedLibIn)) {
                        throw new IllegalStateException(String.format("Failed to write a native library file %s to %s",
                                nativeLibraryFilePath, extractedLibFile));
                    }
                }
            }
            return extractedLibFile;
        } catch (final IOException e) {
            debug("Ignoring %s", e);
            return null;
        }
    }