Ejemplo n.º 1
0
  /**
   * Creates a new instance of GeoTiffReader
   *
   * @param input the GeoTiff file
   * @param uHints user-supplied hints TODO currently are unused
   * @throws DataSourceException
   */
  public GeoTiffReader(Object input, Hints uHints) throws DataSourceException {
    super(input, uHints);

    // /////////////////////////////////////////////////////////////////////
    //
    // Set the source being careful in case it is an URL pointing to a file
    //
    // /////////////////////////////////////////////////////////////////////
    try {

      // setting source
      if (input instanceof URL) {
        final URL sourceURL = (URL) input;
        source = DataUtilities.urlToFile(sourceURL);
      }

      closeMe = true;

      // /////////////////////////////////////////////////////////////////////
      //
      // Get a stream in order to read from it for getting the basic
      // information for this coverage
      //
      // /////////////////////////////////////////////////////////////////////
      if ((source instanceof InputStream) || (source instanceof ImageInputStream)) closeMe = false;
      if (source instanceof ImageInputStream) inStream = (ImageInputStream) source;
      else {

        inStreamSPI = ImageIOExt.getImageInputStreamSPI(source);
        if (inStreamSPI == null)
          throw new IllegalArgumentException("No input stream for the provided source");
        inStream =
            inStreamSPI.createInputStreamInstance(
                source, ImageIO.getUseCache(), ImageIO.getCacheDirectory());
      }
      if (inStream == null) {
        // Try to figure out what went wrong and provide some info to the user.
        if (source instanceof File) {
          File f = (File) source;
          if (!f.exists()) {
            throw new FileNotFoundException("File " + f.getAbsolutePath() + " does not exist.");
          } else if (f.isDirectory()) {
            throw new IOException("File " + f.getAbsolutePath() + " is a directory.");
          } else if (!f.canRead()) {
            throw new IOException("File " + f.getAbsolutePath() + " can not be read.");
          }
        }

        // If we can't give anything more specific, throw the generic error.
        throw new IllegalArgumentException("No input stream for the provided source");
      }

      checkForExternalOverviews();
      // /////////////////////////////////////////////////////////////////////
      //
      // Informations about multiple levels and such
      //
      // /////////////////////////////////////////////////////////////////////
      getHRInfo(this.hints);

      // /////////////////////////////////////////////////////////////////////
      //
      // Coverage name
      //
      // /////////////////////////////////////////////////////////////////////
      coverageName = source instanceof File ? ((File) source).getName() : "geotiff_coverage";
      final int dotIndex = coverageName.lastIndexOf('.');
      if (dotIndex != -1 && dotIndex != coverageName.length())
        coverageName = coverageName.substring(0, dotIndex);

    } catch (IOException e) {
      throw new DataSourceException(e);
    } finally {
      // /////////////////////////////////////////////////////////////////////
      //
      // Freeing streams
      //
      // /////////////////////////////////////////////////////////////////////
      if (closeMe && inStream != null) //
      try {
          inStream.close();
        } catch (Throwable t) {
        }
    }
  }