Top Description Inners Fields Constructors Methods
nonapi.io.github.classgraph.fastzipfilereader

public Class NestedJarHandler

extends Object
Class Inheritance
Imports
java.io.BufferedOutputStream, .File, .FileOutputStream, .IOException, .InputStream, .OutputStream, .RandomAccessFile, java.lang.reflect.Method, java.net.HttpURLConnection, .MalformedURLException, .URI, .URISyntaxException, .URL, .URLConnection, java.nio.ByteBuffer, java.nio.channels.FileChannel, java.nio.file.FileSystem, .FileSystemNotFoundException, .Files, .Path, .Paths, java.util.AbstractMap.SimpleEntry, .ArrayList, .Arrays, .Collections, .Map.Entry, .Set, java.util.concurrent.ConcurrentHashMap, java.util.concurrent.atomic.AtomicBoolean, java.util.zip.DataFormatException, .Inflater, .InflaterInputStream, .ZipException, io.github.classgraph.ModuleReaderProxy, .ModuleRef, .ScanResult, nonapi.io.github.classgraph.concurrency.InterruptionChecker, .SingletonMap, nonapi.io.github.classgraph.fileslice.ArraySlice, .FileSlice, .Slice, nonapi.io.github.classgraph.recycler.Recycler, .Resettable, nonapi.io.github.classgraph.reflection.ReflectionUtils, nonapi.io.github.classgraph.scanspec.ScanSpec, nonapi.io.github.classgraph.utils.FastPathResolver, .FileUtils, .JarUtils, .LogNode

Open and read jarfiles, which may be nested within other jarfiles.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class
NestedJarHandler.RecyclableInflater

Wrapper class that allows an Inflater instance to be reset for reuse and then recycled by a Recycler.

Field Summary

Modifier and TypeField and Description
private SingletonMap<File, PhysicalZipFile, IOException>
canonicalFileToPhysicalZipFileMap

A singleton map from a zipfile's File to the PhysicalZipFile for that file, used to ensure that the RandomAccessFile and FileChannel for any given zipfile is opened only once.

private final AtomicBoolean
closed

True if close(LogNode) has been called.

private static final int
DEFAULT_BUFFER_SIZE

The default size of a file buffer.

private SingletonMap<FastZipEntry, ZipFileSlice, IOException>
fastZipEntryToZipFileSliceMap

A singleton map from a FastZipEntry to the ZipFileSlice wrapping either the zip entry data, if the entry is stored, or a ByteBuffer, if the zip entry was inflated to memory, or a physical file on disk if the zip entry was inflated to a temporary file.

private static final int
HTTP_TIMEOUT

HTTP(S) timeout, ms.

private Recycler<NestedJarHandler.RecyclableInflater, RuntimeException>
inflaterRecycler

A recycler for Inflater instances.

public InterruptionChecker
interruptionChecker

The interruption checker.

private static final int
MAX_INITIAL_BUFFER_SIZE

The maximum initial buffer size.

public SingletonMap<ModuleRef, Recycler<ModuleReaderProxy, IOException>, IOException>
moduleRefToModuleReaderProxyRecyclerMap

A singleton map from a ModuleRef to a ModuleReaderProxy recycler for the module.

public SingletonMap<String, Map.Entry<LogicalZipFile, String>, IOException>
nestedPathToLogicalZipFileAndPackageRootMap

A singleton map from nested jarfile path to a tuple of the logical zipfile for the path, and the package root within the logical zipfile.

private Set<Slice>
openSlices

FileSlice instances that are currently open.

public ReflectionUtils
private static Method
runFinalizationMethod

System.runFinalization() -- deprecated in JDK 18, so accessed by reflection.

public final ScanSpec
public static final String
TEMP_FILENAME_LEAF_SEPARATOR

The separator between random temp filename part and leafname.

private Set<File>
tempFiles

Any temporary files created while scanning.

private SingletonMap<ZipFileSlice, LogicalZipFile, IOException>
zipFileSliceToLogicalZipFileMap

A singleton map from a ZipFileSlice to the LogicalZipFile for that slice.

Constructor Summary

AccessConstructor and Description
public
NestedJarHandler(final ScanSpec scanSpec, final InterruptionChecker
the interruption checker
interruptionChecker
,
final ReflectionUtils reflectionUtils)

A handler for nested jars.

Method Summary

Modifier and TypeMethod and Description
public void
close(final LogNode
The log.
log
)

Close zipfiles, modules, and recyclers, and delete temporary files.

public void
closeDirectByteBuffer(final ByteBuffer backingByteBuffer)

private PhysicalZipFile

Returns:

the temporary file or ByteBuffer the jar was downloaded to, wrapped in a PhysicalZipFile instance.
downloadJarFromURL
(final String
the jar URL
jarURL
,
final LogNode
the log
log
)

Download a jar from a URL to a temporary file, or to a ByteBuffer if the temporary directory is not writeable or full.

private static String

Returns:

the string
leafname
(final String
the path
path
)

Get the leafname of a path.

public File

Returns:

The temporary File.
makeTempFile
(final String
The path to derive the temporary filename from.
filePathBase
,
final boolean
If true, only use the leafname of filePath to derive the temporary filename.
onlyUseLeafname
)

Create a temporary file, and mark it for deletion on exit.

public void
markSliceAsClosed(final Slice
the Slice to close.
slice
)

Mark a Slice as closed.

public void
markSliceAsOpen(final Slice
the Slice that was just opened.
slice
)

Mark a Slice as open, so it can be closed when the ScanResult is closed.

public InputStream

Returns:

the inflater input stream
openInflaterInputStream
(final InputStream
the raw input stream
rawInputStream
)

Wrap an InputStream with an InflaterInputStream, recycling the Inflater instance.

public static byte[]

Returns:

The contents of the InputStream as a byte array.
readAllBytesAsArray
(final InputStream inputStream, final long
The length of the data once inflated from the InputStream, if known, otherwise -1L.
uncompressedLengthHint
)

Read all the bytes in an InputStream.

public Slice

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.
readAllBytesWithSpilloverToDisk
(final InputStream
the InputStream to read from.
inputStream
,
final String
the source URL or zip entry that inputStream was opened from (used to name temporary file, if needed).
tempFileBaseName
,
final long
the length of inputStream if known, else -1L.
inputStreamLengthHint
,
final LogNode
the log.
log
)

Read all the bytes in an InputStream, with spillover to a temporary file on disk if a maximum buffer size is exceeded.

pack-priv void
removeTempFile(final File
the temp file
tempFile
)

Attempt to remove a temporary file.

public void
private String

Returns:

the sanitized filename
sanitizeFilename
(final String
the filename
filename
)

Sanitize filename.

private FileSlice

Returns:

the file slice
spillToDisk
(final InputStream inputStream, final String
The stem to base the temporary filename on.
tempFileBaseName
,
final byte[]
The first buffer to write to the beginning of the file, or null if none.
buf
,
final byte[]
The second buffer to write to the beginning of the file, or null if none. (Should have same nullity as buf.)
overflowBuf
,
final LogNode
The log.
log
)

Spill an InputStream to disk if the stream is too large to fit in RAM.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

canonicalFileToPhysicalZipFileMapback to summary
private SingletonMap<File, PhysicalZipFile, IOException> canonicalFileToPhysicalZipFileMap

A singleton map from a zipfile's File to the PhysicalZipFile for that file, used to ensure that the RandomAccessFile and FileChannel for any given zipfile is opened only once.

closedback to summary
private final AtomicBoolean closed

True if close(LogNode) has been called.

DEFAULT_BUFFER_SIZEback to summary
private static final int DEFAULT_BUFFER_SIZE

The default size of a file buffer.

fastZipEntryToZipFileSliceMapback to summary
private SingletonMap<FastZipEntry, ZipFileSlice, IOException> fastZipEntryToZipFileSliceMap

A singleton map from a FastZipEntry to the ZipFileSlice wrapping either the zip entry data, if the entry is stored, or a ByteBuffer, if the zip entry was inflated to memory, or a physical file on disk if the zip entry was inflated to a temporary file.

HTTP_TIMEOUTback to summary
private static final int HTTP_TIMEOUT

HTTP(S) timeout, ms.

inflaterRecyclerback to summary
private Recycler<NestedJarHandler.RecyclableInflater, RuntimeException> inflaterRecycler

A recycler for Inflater instances.

interruptionCheckerback to summary
public InterruptionChecker interruptionChecker

The interruption checker.

MAX_INITIAL_BUFFER_SIZEback to summary
private static final int MAX_INITIAL_BUFFER_SIZE

The maximum initial buffer size.

moduleRefToModuleReaderProxyRecyclerMapback to summary
public SingletonMap<ModuleRef, Recycler<ModuleReaderProxy, IOException>, IOException> moduleRefToModuleReaderProxyRecyclerMap

A singleton map from a ModuleRef to a ModuleReaderProxy recycler for the module.

nestedPathToLogicalZipFileAndPackageRootMapback to summary
public SingletonMap<String, Map.Entry<LogicalZipFile, String>, IOException> nestedPathToLogicalZipFileAndPackageRootMap

A singleton map from nested jarfile path to a tuple of the logical zipfile for the path, and the package root within the logical zipfile.

openSlicesback to summary
private Set<Slice> openSlices

FileSlice instances that are currently open.

reflectionUtilsback to summary
public ReflectionUtils reflectionUtils
runFinalizationMethodback to summary
private static Method runFinalizationMethod

System.runFinalization() -- deprecated in JDK 18, so accessed by reflection.

scanSpecback to summary
public final ScanSpec scanSpec

The ScanSpec.

TEMP_FILENAME_LEAF_SEPARATORback to summary
public static final String TEMP_FILENAME_LEAF_SEPARATOR

The separator between random temp filename part and leafname.

tempFilesback to summary
private Set<File> tempFiles

Any temporary files created while scanning.

zipFileSliceToLogicalZipFileMapback to summary
private SingletonMap<ZipFileSlice, LogicalZipFile, IOException> zipFileSliceToLogicalZipFileMap

A singleton map from a ZipFileSlice to the LogicalZipFile for that slice.

Constructor Detail

NestedJarHandlerback to summary
public NestedJarHandler(final ScanSpec scanSpec, final InterruptionChecker interruptionChecker, final ReflectionUtils reflectionUtils)

A handler for nested jars.

Parameters
scanSpec:ScanSpec

The ScanSpec.

interruptionChecker:InterruptionChecker

the interruption checker

Method Detail

closeback to summary
public void close(final LogNode log)

Close zipfiles, modules, and recyclers, and delete temporary files. Called by ScanResult#close().

Parameters
log:LogNode

The log.

closeDirectByteBufferback to summary
public void closeDirectByteBuffer(final ByteBuffer backingByteBuffer)
downloadJarFromURLback to summary
private PhysicalZipFile downloadJarFromURL(final String jarURL, final 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:String

the jar URL

log:LogNode

the log

Returns:PhysicalZipFile

the temporary file or ByteBuffer the jar was downloaded to, wrapped in a PhysicalZipFile instance.

Exceptions
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.)
leafnameback to summary
private static String leafname(final String path)

Get the leafname of a path.

Parameters
path:String

the path

Returns:String

the string

makeTempFileback to summary
public File makeTempFile(final String filePathBase, final boolean onlyUseLeafname) throws IOException

Create a temporary file, and mark it for deletion on exit.

Parameters
filePathBase:String

The path to derive the temporary filename from.

onlyUseLeafname:boolean

If true, only use the leafname of filePath to derive the temporary filename.

Returns:File

The temporary File.

Exceptions
IOException:
If the temporary file could not be created.
markSliceAsClosedback to summary
public void markSliceAsClosed(final Slice slice)

Mark a Slice as closed.

Parameters
slice:Slice

the Slice to close.

markSliceAsOpenback to summary
public void markSliceAsOpen(final Slice slice) throws IOException

Mark a Slice as open, so it can be closed when the ScanResult is closed.

Parameters
slice:Slice

the Slice that was just opened.

Exceptions
IOException:
Signals that an I/O exception has occurred.
openInflaterInputStreamback to summary
public InputStream openInflaterInputStream(final InputStream rawInputStream) throws IOException

Wrap an InputStream with an InflaterInputStream, recycling the Inflater instance.

Parameters
rawInputStream:InputStream

the raw input stream

Returns:InputStream

the inflater input stream

Exceptions
IOException:
Signals that an I/O exception has occurred.
readAllBytesAsArrayback to summary
public static byte[] readAllBytesAsArray(final InputStream inputStream, final long uncompressedLengthHint) throws IOException

Read all the bytes in an InputStream.

Parameters
inputStream:InputStream

The InputStream.

uncompressedLengthHint:long

The length of the data once inflated from the InputStream, if known, otherwise -1L.

Returns:byte[]

The contents of the InputStream as a byte array.

Exceptions
IOException:
If the contents could not be read.
readAllBytesWithSpilloverToDiskback to summary
public Slice readAllBytesWithSpilloverToDisk(final InputStream inputStream, final String tempFileBaseName, final long inputStreamLengthHint, final 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:InputStream

the InputStream to read from.

tempFileBaseName:String

the source URL or zip entry that inputStream was opened from (used to name temporary file, if needed).

inputStreamLengthHint:long

the length of inputStream if known, else -1L.

log:LogNode

the log.

Returns:Slice

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.

Exceptions
IOException:
If the contents could not be read.
removeTempFileback to summary
pack-priv void removeTempFile(final File tempFile) throws IOException, SecurityException

Attempt to remove a temporary file.

Parameters
tempFile:File

the temp file

Exceptions
IOException:
If the temporary file could not be removed.
SecurityException:
If the temporary file is inaccessible.
runFinalizationMethodback to summary
public void runFinalizationMethod()
sanitizeFilenameback to summary
private String sanitizeFilename(final String filename)

Sanitize filename.

Parameters
filename:String

the filename

Returns:String

the sanitized filename

spillToDiskback to summary
private FileSlice spillToDisk(final InputStream inputStream, final String tempFileBaseName, final byte[] buf, final byte[] overflowBuf, final LogNode log) throws IOException

Spill an InputStream to disk if the stream is too large to fit in RAM.

Parameters
inputStream:InputStream

The InputStream.

tempFileBaseName:String

The stem to base the temporary filename on.

buf:byte[]

The first buffer to write to the beginning of the file, or null if none.

overflowBuf:byte[]

The second buffer to write to the beginning of the file, or null if none. (Should have same nullity as buf.)

log:LogNode

The log.

Returns:FileSlice

the file slice

Exceptions
IOException:
If anything went wrong creating or writing to the temp file.
nonapi.io.github.classgraph.fastzipfilereader back to summary

private Class NestedJarHandler.CloseableUrlConnection

extends Object
implements AutoCloseable
Class Inheritance
  • java.lang.Object
  • nonapi.io.github.classgraph.fastzipfilereader.NestedJarHandler.CloseableUrlConnection
All Implemented Interfaces
java.lang.AutoCloseable

Field Summary

Modifier and TypeField and Description
public final URLConnection
public final HttpURLConnection

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
close()

Implements java.lang.AutoCloseable.close.

Closes this resource, relinquishing any underlying resources.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

connback to summary
public final URLConnection conn
httpConnback to summary
public final HttpURLConnection httpConn

Constructor Detail

CloseableUrlConnectionback to summary
public CloseableUrlConnection(final URL url) throws IOException

Method Detail

closeback to summary
public void close()

Implements java.lang.AutoCloseable.close.

Doc from java.lang.AutoCloseable.close.

Closes this resource, relinquishing any underlying resources. This method is invoked automatically on objects managed by the try-with-resources statement.

Annotations
@Override
nonapi.io.github.classgraph.fastzipfilereader back to summary

private Class NestedJarHandler.RecyclableInflater

extends Object
implements Resettable, AutoCloseable
Class Inheritance
  • java.lang.Object
  • nonapi.io.github.classgraph.fastzipfilereader.NestedJarHandler.RecyclableInflater
All Implemented Interfaces
java.lang.AutoCloseable, nonapi.io.github.classgraph.recycler.Resettable

Wrapper class that allows an Inflater instance to be reset for reuse and then recycled by a Recycler.

Field Summary

Modifier and TypeField and Description
private final Inflater
inflater

Create a new Inflater instance with the "nowrap" option (which is needed for zipfile entries).

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
close()

Implements java.lang.AutoCloseable.close.

Called when the Recycler instance is closed, to destroy the Inflater instance.

public Inflater

Returns:

the Inflater instance.
getInflater
()

Get the Inflater instance.

public void
reset()

Implements nonapi.io.github.classgraph.recycler.Resettable.reset.

Called when an Inflater instance is recycled, to reset the inflater so it can accept new input.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

inflaterback to summary
private final Inflater inflater

Create a new Inflater instance with the "nowrap" option (which is needed for zipfile entries).

Constructor Detail

RecyclableInflaterback to summary
private RecyclableInflater()

Method Detail

closeback to summary
public void close()

Implements java.lang.AutoCloseable.close.

Called when the Recycler instance is closed, to destroy the Inflater instance.

Annotations
@Override
getInflaterback to summary
public Inflater getInflater()

Get the Inflater instance.

Returns:Inflater

the Inflater instance.

resetback to summary
public void reset()

Implements nonapi.io.github.classgraph.recycler.Resettable.reset.

Called when an Inflater instance is recycled, to reset the inflater so it can accept new input.

Annotations
@Override