use of com.ibm.dtfj.utils.file.J9FileImageInputStream in project openj9 by eclipse.
the class ImageFactory method getImagesFromArchive.
@Override
public Image[] getImagesFromArchive(File archive, boolean extract) throws IOException {
Objects.requireNonNull(archive);
if (!FileManager.fileExists(archive)) {
// $NON-NLS-1$ //$NON-NLS-2$
throw new FileNotFoundException("Archive '" + archive.getAbsolutePath() + "' not found.");
}
if (!FileManager.isArchive(archive)) {
// $NON-NLS-1$ //$NON-NLS-2$
throw new IOException("The specified archive " + archive.getAbsolutePath() + " was not recognised");
}
// images created from this zip file
ArrayList<Image> images = new ArrayList<>();
exceptions.clear();
// the cast to an ArchiveFileManager is safe as we've already checked earlier with FileManager.isArchive
CompressedFileManager manager = (CompressedFileManager) FileManager.getManager(archive);
List<ManagedImageSource> sources = manager.getImageSources();
if (extract) {
// create the temporary directory in preparation for the extraction
File parent = getTempDirParent();
tmpdir = FileManager.createTempDir(parent);
}
for (ManagedImageSource source : sources) {
ImageInputStream corestream = null;
ImageInputStream metastream = null;
if (extract) {
// extract the files to disk
manager.extract(source, tmpdir);
File coreFile = new File(source.getPathToExtractedFile());
corestream = new J9FileImageInputStream(coreFile, source);
if (source.hasMetaData()) {
manager.extract(source.getMetadata(), tmpdir);
File metaFile = new File(source.getMetadata().getPathToExtractedFile());
metastream = new J9FileImageInputStream(metaFile, source.getMetadata());
}
} else {
// run from in memory
corestream = manager.getStream(source);
if (source.hasMetaData()) {
metastream = manager.getStream(source.getMetadata());
}
}
try {
// Try each factory listed for this source file type, checking to see if we find a JRE
ImageReference imageReference = new ImageReference();
for (String factory : source.getType().getFactoryNames()) {
if (foundRuntimeInImage(factory, imageReference, source.toURI(), corestream, metastream)) {
if (imageReference.image instanceof ManagedImage) {
((ManagedImage) imageReference.image).setImageSource(source);
}
images.add(imageReference.image);
// quit looking for an image
break;
}
imageReference.image = null;
}
// Final fallback is to get a native-only DDR Image (no JRE), if the source is a core dump
if (imageReference.image == null && source.getType().equals(ImageSourceType.CORE)) {
com.ibm.dtfj.image.ImageFactory factory = createImageFactory(FACTORY_DDR);
if (factory != null) {
imageReference.image = factory.getImage(corestream, source.toURI());
if (imageReference.image != null) {
images.add(imageReference.image);
}
}
}
} catch (Exception e) {
// log exception and attempt to carry on creating images
exceptions.add(e);
}
}
printExceptions();
return images.toArray(new Image[images.size()]);
}
Aggregations