private void setPreferredTiling(Product product) throws IOException {
   final Dimension dimension;
   if (isBadTiling()) {
     dimension =
         JAIUtils.computePreferredTileSize(
             imageReader.getWidth(FIRST_IMAGE), imageReader.getHeight(FIRST_IMAGE), 1);
   } else {
     dimension =
         new Dimension(
             imageReader.getTileWidth(FIRST_IMAGE), imageReader.getTileHeight(FIRST_IMAGE));
   }
   if (isGlobalShifted180) {
     product.setPreferredTileSize(
         new Dimension(imageReader.getWidth(FIRST_IMAGE), imageReader.getHeight(FIRST_IMAGE)));
   } else {
     product.setPreferredTileSize(dimension);
   }
 }
 private boolean isBadTiling() throws IOException {
   final int imageHeight = imageReader.getHeight(FIRST_IMAGE);
   final int tileHeight = imageReader.getTileHeight(FIRST_IMAGE);
   final int imageWidth = imageReader.getWidth(FIRST_IMAGE);
   final int tileWidth = imageReader.getTileWidth(FIRST_IMAGE);
   return tileWidth <= 1
       || tileHeight <= 1
       || imageWidth == tileWidth
       || imageHeight == tileHeight;
 }
  Product readGeoTIFFProduct(final ImageInputStream stream, final File inputFile)
      throws IOException {
    Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(stream);
    while (imageReaders.hasNext()) {
      final ImageReader reader = imageReaders.next();
      if (reader instanceof TIFFImageReader) {
        imageReader = (TIFFImageReader) reader;
        break;
      }
    }
    if (imageReader == null) {
      throw new IOException("GeoTiff imageReader not found");
    }

    imageReader.setInput(stream);

    Product product = null;

    final TIFFImageMetadata imageMetadata =
        (TIFFImageMetadata) imageReader.getImageMetadata(FIRST_IMAGE);
    final TiffFileInfo tiffInfo = new TiffFileInfo(imageMetadata.getRootIFD());
    final TIFFField field = tiffInfo.getField(Utils.PRIVATE_BEAM_TIFF_TAG_NUMBER);
    if (field != null && field.getType() == TIFFTag.TIFF_ASCII) {
      final String s = field.getAsString(0).trim();
      if (s.contains("<Dimap_Document")) { // with DIMAP header
        InputStream is = null;
        try {
          final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
          final DocumentBuilder builder = factory.newDocumentBuilder();
          is = new ByteArrayInputStream(s.getBytes());
          final Document document = new DOMBuilder().build(builder.parse(is));
          product = DimapProductHelpers.createProduct(document);
          removeGeoCodingAndTiePointGrids(product);
          initBandsMap(product);
        } catch (ParserConfigurationException | SAXException ignore) {
          // ignore if it can not be read
        } finally {
          if (is != null) {
            is.close();
          }
        }
      }
    }

    if (product == null) { // without DIMAP header
      final String productName;
      if (tiffInfo.containsField(BaselineTIFFTagSet.TAG_IMAGE_DESCRIPTION)) {
        final TIFFField field1 = tiffInfo.getField(BaselineTIFFTagSet.TAG_IMAGE_DESCRIPTION);
        productName = field1.getAsString(0);
      } else if (inputFile != null) {
        productName = FileUtils.getFilenameWithoutExtension(inputFile);
      } else {
        productName = "geotiff";
      }
      final String productType = getReaderPlugIn().getFormatNames()[0];

      final int width = imageReader.getWidth(FIRST_IMAGE);
      final int height = imageReader.getHeight(FIRST_IMAGE);
      product = new Product(productName, productType, width, height, this);
      addBandsToProduct(tiffInfo, product);
    }

    if (tiffInfo.isGeotiff()) {
      applyGeoCoding(tiffInfo, imageMetadata, product);
    }

    TiffTagToMetadataConverter.addTiffTagsToMetadata(
        imageMetadata, tiffInfo, product.getMetadataRoot());

    if (inputFile != null) {
      product.setFileLocation(inputFile);
    }
    setPreferredTiling(product);

    return product;
  }