use of it.geosolutions.imageio.stream.input.URIImageInputStream in project imageio-ext by geosolutions-it.
the class GDALImageReader method setInput.
/**
* Sets the input for the specialized reader.
*
* @throws IllegalArgumentException
* if the provided input is <code>null</code>
*/
public void setInput(Object input, boolean seekForwardOnly, boolean ignoreMetadata) {
if (LOGGER.isLoggable(Level.FINE))
LOGGER.fine("Setting Input");
// check input
if (input == null)
throw new IllegalArgumentException("The provided input is null!");
// is gdal available
if (!GDALUtilities.isGDALAvailable())
throw new IllegalStateException("GDAL native libraries are not available.");
// to clear any value-object which was related to the previous input.
if (this.imageInputStream != null) {
reset();
imageInputStream = null;
}
// //
if (input instanceof File) {
datasetSource = (File) input;
try {
imageInputStream = ImageIO.createImageInputStream(input);
} catch (IOException e) {
throw new RuntimeException("Failed to create a valid input stream ", e);
}
} else // //
if (input instanceof FileImageInputStreamExt) {
datasetSource = ((FileImageInputStreamExt) input).getFile();
imageInputStream = (ImageInputStream) input;
} else // //
if (input instanceof URL) {
final URL tempURL = (URL) input;
if (tempURL.getProtocol().equalsIgnoreCase("file")) {
try {
datasetSource = ImageIOUtilities.urlToFile(tempURL);
imageInputStream = ImageIO.createImageInputStream(input);
} catch (IOException e) {
throw new RuntimeException("Failed to create a valid input stream ", e);
}
}
} else if (input instanceof URIImageInputStream) {
imageInputStream = (URIImageInputStream) input;
datasetSource = null;
uriSource = ((URIImageInputStream) input).getUri();
}
//
// Checking if this input is of a supported format.
// Now, I have an ImageInputStream and I can try to see if the input's
// format is supported by the specialized reader
//
boolean isInputDecodable = false;
String mainDatasetName = null;
Dataset mainDataSet = null;
if (imageInputStream != null) {
if (datasetSource != null) {
mainDatasetName = datasetSource.getAbsolutePath();
mainDataSet = GDALUtilities.acquireDataSet(datasetSource.getAbsolutePath(), gdalconstConstants.GA_ReadOnly);
} else if (uriSource != null) {
final String urisource = uriSource.toString();
mainDatasetName = urisource;
mainDataSet = GDALUtilities.acquireDataSet(urisource, gdalconstConstants.GA_ReadOnly);
}
if (mainDataSet != null) {
isInputDecodable = ((GDALImageReaderSpi) this.getOriginatingProvider()).isDecodable(mainDataSet);
} else
isInputDecodable = false;
}
if (isInputDecodable) {
// cache dataset
datasetsMap.put(mainDatasetName, mainDataSet);
// input is decodable
super.setInput(imageInputStream, seekForwardOnly, ignoreMetadata);
// Listing available subdatasets
final List<String> subdatasets = mainDataSet.GetMetadata_List(GDALUtilities.GDALMetadataDomain.SUBDATASETS);
// setting the number of subdatasets
// It is worth to remind that the subdatasets vector
// contains both Subdataset's Name and Subdataset's Description
// Thus we need to divide its size by two.
nSubdatasets = subdatasets.size() / 2;
// Thus, theDataset is simply the main dataset.
if (nSubdatasets == 0) {
nSubdatasets = 1;
datasetNames = new String[1];
datasetNames[0] = mainDatasetName;
datasetMetadataMap.put(datasetNames[0], this.createDatasetMetadata(mainDatasetName));
} else {
datasetNames = new String[nSubdatasets + 1];
for (int i = 0; i < nSubdatasets; i++) {
final String subdatasetName = (subdatasets.get(i * 2)).toString();
final int nameStartAt = subdatasetName.lastIndexOf("_NAME=") + 6;
datasetNames[i] = subdatasetName.substring(nameStartAt);
}
datasetNames[nSubdatasets] = mainDatasetName;
datasetMetadataMap.put(datasetNames[nSubdatasets], createDatasetMetadata(mainDataSet, datasetNames[nSubdatasets]));
}
// clean list
subdatasets.clear();
} else {
StringBuilder sb = new StringBuilder();
if (imageInputStream == null) {
sb.append("Unable to create a valid ImageInputStream for the provided input:");
sb.append(GDALUtilities.NEWLINE);
sb.append(input.toString());
} else
sb.append("The Provided input is not supported by this reader");
throw new RuntimeException(sb.toString());
}
}
Aggregations