private static File unpackBinaries()

in src/main/java/com/hadoop/compression/lzo/GPLNativeCodeLoader.java [85:139]


  private static File unpackBinaries() {
    // locate the binaries inside the jar
    String fileName = System.mapLibraryName(LIBRARY_NAME);
    String directory = getDirectoryLocation();
    // use the current defining classloader to load the resource
    InputStream is =
        GPLNativeCodeLoader.class.getResourceAsStream(directory + "/" + fileName);
    if (is == null) {
      // specific to mac
      // on mac the filename can be either .dylib or .jnilib: try again with the
      // alternate name
      if (getOsName().contains("Mac")) {
        if (fileName.endsWith(".dylib")) {
          fileName = fileName.replace(".dylib", ".jnilib");
        } else if (fileName.endsWith(".jnilib")) {
          fileName = fileName.replace(".jnilib", ".dylib");
        }
        is = GPLNativeCodeLoader.class.getResourceAsStream(directory + "/" + fileName);
      }
      // the OS-specific library was not found: fall back on the library path
      if (is == null) {
        return null;
      }
    }

    // write the file
    byte[] buffer = new byte[8192];
    OutputStream os = null;
    try {
      // prepare the unpacked file location
      File unpackedFile = File.createTempFile("unpacked-", "-" + fileName);
      // ensure the file gets cleaned up
      unpackedFile.deleteOnExit();

      os = new FileOutputStream(unpackedFile);
      int read = 0;
      while ((read = is.read(buffer)) != -1) {
        os.write(buffer, 0, read);
      }

      // set the execution permission
      unpackedFile.setExecutable(true, false);
      LOG.debug("temporary unpacked path: " + unpackedFile);
      // return the file
      return unpackedFile;
    } catch (IOException e) {
      LOG.error("could not unpack the binaries", e);
      return null;
    } finally {
      try { is.close(); } catch (IOException ignore) {}
      if (os != null) {
        try { os.close(); } catch (IOException ignore) {}
      }
    }
  }