/** Close all the {@link Resource} objects in this {@link ResourceList}. */ @Override public void close() { for (final Resource resource : this) { try { resource.close(); } catch (final Exception e) { // Ignore } } } }
@Override public boolean accept(final Resource resource) { final String path = resource.getPath(); if (!path.endsWith(".class") || path.length() < 7) { return false; } // Check filename is not simply ".class" final char c = path.charAt(path.length() - 7); return c != '/' && c != '.'; } };
/** Add a resource discovered during the scan. */ protected void addWhitelistedResource(final Resource resource, final ScanSpecPathMatch parentMatchStatus, final LogNode log) { final String path = resource.getPath(); final boolean isClassFile = FileUtils.isClassfile(path); if (isClassFile) { if (scanSpec.enableClassInfo && !scanSpec.classfilePathWhiteBlackList.isBlacklisted(path)) { // ClassInfo is enabled, and found a whitelisted classfile whitelistedClassfileResources.add(resource); if (log != null) { log.log(path, "Found whitelisted classfile: " + path + (path.equals(resource.getPathRelativeToClasspathElement()) ? "" : " ; full path: " + resource.getPathRelativeToClasspathElement())); } } } else { if (log != null) { log.log(path, "Found whitelisted resource: " + path + (path.equals(resource.getPathRelativeToClasspathElement()) ? "" : " ; full path: " + resource.getPathRelativeToClasspathElement())); } } whitelistedResources.add(resource); }
/** * Read each {@link Resource} in this {@link ResourceList} as a {@link ByteBuffer}, pass the {@link ByteBuffer} * to the given {@link InputStreamConsumer}, then release the {@link ByteBuffer} after the * {@link ByteBufferConsumer} returns, by calling {@link Resource#close()}. * * @param byteBufferConsumer * The {@link ByteBufferConsumer}. * @param ignoreIOExceptions * if true, any {@link IOException} thrown while trying to load any of the resources will be silently * ignored. * @throws IllegalArgumentException * if ignoreExceptions is false, and an {@link IOException} is thrown while trying to load any of * the resources. */ public void forEachByteBuffer(final ByteBufferConsumer byteBufferConsumer, final boolean ignoreIOExceptions) { for (final Resource resource : this) { try { final ByteBuffer byteBuffer = resource.read(); byteBufferConsumer.accept(resource, byteBuffer); } catch (final IOException e) { if (!ignoreIOExceptions) { throw new IllegalArgumentException("Could not load resource " + resource, e); } } finally { resource.close(); } } }
/** * Fetch an {@link InputStream} for each {@link Resource} in this {@link ResourceList}, pass the * {@link InputStream} to the given {@link InputStreamConsumer}, then close the {@link InputStream} after the * {@link InputStreamConsumer} returns, by calling {@link Resource#close()}. * * @param inputStreamConsumer * The {@link InputStreamConsumer}. * @param ignoreIOExceptions * if true, any {@link IOException} thrown while trying to load any of the resources will be silently * ignored. * @throws IllegalArgumentException * if ignoreExceptions is false, and an {@link IOException} is thrown while trying to open any of * the resources. */ public void forEachInputStream(final InputStreamConsumer inputStreamConsumer, final boolean ignoreIOExceptions) { for (final Resource resource : this) { try { inputStreamConsumer.accept(resource, resource.open()); } catch (final IOException e) { if (!ignoreIOExceptions) { throw new IllegalArgumentException("Could not load resource " + resource, e); } } finally { resource.close(); } } }
/** * Fetch the content of each {@link Resource} in this {@link ResourceList} as a byte array, pass the byte array * to the given {@link ByteArrayConsumer}, then close the underlying InputStream or release the underlying * ByteBuffer by calling {@link Resource#close()}. * * @param byteArrayConsumer * The {@link ByteArrayConsumer}. * @param ignoreIOExceptions * if true, any {@link IOException} thrown while trying to load any of the resources will be silently * ignored. * @throws IllegalArgumentException * if ignoreExceptions is false, and an {@link IOException} is thrown while trying to load any of * the resources. */ public void forEachByteArray(final ByteArrayConsumer byteArrayConsumer, final boolean ignoreIOExceptions) { for (final Resource resource : this) { try { final byte[] resourceContent = resource.load(); byteArrayConsumer.accept(resource, resourceContent); } catch (final IOException e) { if (!ignoreIOExceptions) { throw new IllegalArgumentException("Could not load resource " + resource, e); } } finally { resource.close(); } } }
try { inputStreamOrByteBuffer = classfileResource.openOrRead(); classfileResource.close(); inputStreamOrByteBuffer = null;
/** Return a string representation of the resource's location (as a URL string). */ @Override public String toString() { if (toString != null) { return toString; } else { return toString = getURL().toString(); } }
protected ByteBuffer inputStreamToByteBuffer() throws IOException { return byteBuffer == null ? byteBuffer = ByteBuffer.wrap(inputStreamToByteArray()) : byteBuffer; }
@Override public InputStream getResourceAsStream(final String path) { final ResourceList resourceList = scanResult.getResourcesWithPath(path); if (resourceList == null || resourceList.isEmpty()) { return super.getResourceAsStream(path); } else { try { return resourceList.get(0).open(); } catch (final IOException e) { return null; } } } }
if (moduleInfo == null) { moduleNameToModuleInfo.put(classpathElement.moduleName, moduleInfo = new ModuleInfo(classfileResource.getModuleRef(), classpathElement));
@Override public URL nextElement() { return resourceList.get(idx++).getURL(); } };
/** Close parent resource, which will call {@link #closeInputStream()}. */ @Override public void close() throws IOException { if (parentResource != null) { try { parentResource.close(); } catch (final Exception e) { // Ignore } parentResource = null; } } }
/** * @return The paths of all resources in this list relative to the package root of the classpath element, by * calling {@link Resource#getPath()} for each item in the list. For example, given a resource path of * "BOOT-INF/classes/com/xyz/resource.xml" and a package root of "BOOT-INF/classes/", returns * "com/xyz/resource.xml". */ public List<String> getPaths() { final List<String> resourcePaths = new ArrayList<>(this.size()); for (final Resource resource : this) { resourcePaths.add(resource.getPath()); } return resourcePaths; }
/** * @return The URLs of all resources in this list, by calling {@link Resource#getURL()} for each item in the * list. */ public List<URL> getURLs() { final List<URL> resourceURLs = new ArrayList<>(this.size()); for (final Resource resource : this) { resourceURLs.add(resource.getURL()); } return resourceURLs; }
@Override public synchronized void close() { super.close(); // Close inputStream if (byteBuffer != null) { byteBuffer = null; } markAsClosed(); } };
/** * @return The paths of all resources in this list relative to the root of the classpath element, by calling * {@link Resource#getPathRelativeToClasspathElement()} for each item in the list. For example, given a * resource path of "BOOT-INF/classes/com/xyz/resource.xml", returns * "BOOT-INF/classes/com/xyz/resource.xml" (even if the package root is "BOOT-INF/classes/"). */ public List<String> getPathsRelativeToClasspathElement() { final List<String> resourcePaths = new ArrayList<>(this.size()); for (final Resource resource : this) { resourcePaths.add(resource.getPath()); } return resourcePaths; }
@Override public URL getResource(final String path) { final ResourceList resourceList = scanResult.getResourcesWithPath(path); if (resourceList == null || resourceList.isEmpty()) { return super.getResource(path); } else { return resourceList.get(0).getURL(); } }
@Override public synchronized void close() { super.close(); // Close inputStream if (byteBuffer != null) { FileUtils.closeDirectByteBuffer(byteBuffer, /* log = */ null); byteBuffer = null; } if (fileChannel != null) { try { fileChannel.close(); } catch (final IOException e) { // Ignore } fileChannel = null; } if (randomAccessFile != null) { try { randomAccessFile.close(); } catch (final IOException e) { // Ignore } randomAccessFile = null; } markAsClosed(); } };