Class NestedJarHandler

java.lang.Object
nonapi.io.github.classgraph.fastzipfilereader.NestedJarHandler

public class NestedJarHandler extends Object
Open and read jarfiles, which may be nested within other jarfiles.
  • Field Details

  • Constructor Details

    • NestedJarHandler

      public NestedJarHandler(ScanSpec scanSpec, InterruptionChecker interruptionChecker, ReflectionUtils reflectionUtils)
      A handler for nested jars.
      Parameters:
      scanSpec - The ScanSpec.
      interruptionChecker - the interruption checker
  • Method Details

    • leafname

      private static String leafname(String path)
      Get the leafname of a path.
      Parameters:
      path - the path
      Returns:
      the string
    • sanitizeFilename

      private String sanitizeFilename(String filename)
      Sanitize filename.
      Parameters:
      filename - the filename
      Returns:
      the sanitized filename
    • makeTempFile

      public File makeTempFile(String filePathBase, boolean onlyUseLeafname) throws IOException
      Create a temporary file, and mark it for deletion on exit.
      Parameters:
      filePathBase - The path to derive the temporary filename from.
      onlyUseLeafname - If true, only use the leafname of filePath to derive the temporary filename.
      Returns:
      The temporary File.
      Throws:
      IOException - If the temporary file could not be created.
    • removeTempFile

      void removeTempFile(File tempFile) throws IOException, SecurityException
      Attempt to remove a temporary file.
      Parameters:
      tempFile - the temp file
      Throws:
      IOException - If the temporary file could not be removed.
      SecurityException - If the temporary file is inaccessible.
    • markSliceAsOpen

      public void markSliceAsOpen(Slice slice) throws IOException
      Mark a Slice as open, so it can be closed when the ScanResult is closed.
      Parameters:
      slice - the Slice that was just opened.
      Throws:
      IOException - Signals that an I/O exception has occurred.
    • markSliceAsClosed

      public void markSliceAsClosed(Slice slice)
      Mark a Slice as closed.
      Parameters:
      slice - the Slice to close.
    • downloadJarFromURL

      private PhysicalZipFile downloadJarFromURL(String jarURL, LogNode log) throws IOException, InterruptedException
      Download a jar from a URL to a temporary file, or to a ByteBuffer if the temporary directory is not writeable or full. The downloaded jar is returned wrapped in a PhysicalZipFile instance.
      Parameters:
      jarURL - the jar URL
      log - the log
      Returns:
      the temporary file or ByteBuffer the jar was downloaded to, wrapped in a PhysicalZipFile instance.
      Throws:
      IOException - If the jar could not be downloaded, or the jar URL is malformed.
      InterruptedException - if the thread was interrupted
      IllegalArgumentException - If the temp dir is not writeable, or has insufficient space to download the jar. (This is thrown as a separate exception from IOException, so that the case of an unwriteable temp dir can be handled separately, by downloading the jar to a ByteBuffer in RAM.)
    • openInflaterInputStream

      public InputStream openInflaterInputStream(InputStream rawInputStream) throws IOException
      Wrap an InputStream with an InflaterInputStream, recycling the Inflater instance.
      Parameters:
      rawInputStream - the raw input stream
      Returns:
      the inflater input stream
      Throws:
      IOException - Signals that an I/O exception has occurred.
    • readAllBytesWithSpilloverToDisk

      public Slice readAllBytesWithSpilloverToDisk(InputStream inputStream, String tempFileBaseName, long inputStreamLengthHint, LogNode log) throws IOException
      Read all the bytes in an InputStream, with spillover to a temporary file on disk if a maximum buffer size is exceeded.
      Parameters:
      inputStream - the InputStream to read from.
      tempFileBaseName - the source URL or zip entry that inputStream was opened from (used to name temporary file, if needed).
      inputStreamLengthHint - the length of inputStream if known, else -1L.
      log - the log.
      Returns:
      if the InputStream could be read into a byte array, an ArraySlice will be returned. If this fails and the InputStream is spilled over to disk, a FileSlice will be returned.
      Throws:
      IOException - If the contents could not be read.
    • spillToDisk

      private FileSlice spillToDisk(InputStream inputStream, String tempFileBaseName, byte[] buf, byte[] overflowBuf, LogNode log) throws IOException
      Spill an InputStream to disk if the stream is too large to fit in RAM.
      Parameters:
      inputStream - The InputStream.
      tempFileBaseName - The stem to base the temporary filename on.
      buf - The first buffer to write to the beginning of the file, or null if none.
      overflowBuf - The second buffer to write to the beginning of the file, or null if none. (Should have same nullity as buf.)
      log - The log.
      Returns:
      the file slice
      Throws:
      IOException - If anything went wrong creating or writing to the temp file.
    • readAllBytesAsArray

      public static byte[] readAllBytesAsArray(InputStream inputStream, long uncompressedLengthHint) throws IOException
      Read all the bytes in an InputStream.
      Parameters:
      inputStream - The InputStream.
      uncompressedLengthHint - The length of the data once inflated from the InputStream, if known, otherwise -1L.
      Returns:
      The contents of the InputStream as a byte array.
      Throws:
      IOException - If the contents could not be read.
    • close

      public void close(LogNode log)
      Close zipfiles, modules, and recyclers, and delete temporary files. Called by ScanResult.close().
      Parameters:
      log - The log.
    • runFinalizationMethod

      public void runFinalizationMethod()
    • closeDirectByteBuffer

      public void closeDirectByteBuffer(ByteBuffer backingByteBuffer)