Program: 187104
Project: 187104
Buggy source:
/** 
 * Read data from the required region of the raster.
 * @param item a <code>GDALCommonIIOImageMetadata</code> related to the actual dataset
 * @param srcRegion the source Region to be read
 * @param dstRegion the destination Region of the image read
 * @param selectedBands an array specifying the requested bands
 * @return the read <code>Raster</code>
 */
private Raster readDatasetRaster(GDALCommonIIOImageMetadata item,Rectangle srcRegion,Rectangle dstRegion,int[] selectedBands,SampleModel destSampleModel) throws IOException {
  SampleModel destSm=destSampleModel != null ? destSampleModel : item.getSampleModel();
  SampleModel sampleModel=null;
  DataBuffer imgBuffer=null;
  final String datasetName=item.getDatasetName();
  final Dataset dataset=GDALUtilities.acquireDataSet(datasetName,gdalconst.GA_ReadOnly);
  Band pBand=null;
  if (dataset == null)   throw new IOException("String_Node_Str" + datasetName);
  try {
    int dstWidth=dstRegion.width;
    int dstHeight=dstRegion.height;
    int srcRegionXOffset=srcRegion.x;
    int srcRegionYOffset=srcRegion.y;
    int srcRegionWidth=srcRegion.width;
    int srcRegionHeight=srcRegion.height;
    if (LOGGER.isLoggable(Level.FINE))     LOGGER.fine("String_Node_Str" + srcRegion.toString());
    final int nBands=selectedBands != null ? selectedBands.length : destSm.getNumBands();
    int[] banks=new int[nBands];
    int[] offsets=new int[nBands];
    final int pixels=dstWidth * dstHeight;
    int bufferType=0, bufferSize=0;
    int typeSizeInBytes=0;
    pBand=dataset.GetRasterBand(1);
    bufferType=pBand.getDataType();
    typeSizeInBytes=gdal.GetDataTypeSize(bufferType) / 8;
    bufferSize=nBands * pixels * typeSizeInBytes;
    boolean splitBands=false;
    if (bufferSize < 0 || destSm instanceof BandedSampleModel) {
      bufferSize=pixels * typeSizeInBytes;
      splitBands=true;
    }
    int dataBufferType=-1;
    byte[][] byteBands=new byte[nBands][];
    for (int k=0; k < nBands; k++) {
      if (k > 0 && !splitBands)       break;
      final byte[] dataBuffer=new byte[bufferSize];
      final int returnVal;
      if (!splitBands) {
        final int bandsMap[]=new int[nBands];
        if (selectedBands != null) {
          for (int i=0; i < nBands; i++)           bandsMap[i]=selectedBands[i] + 1;
        }
 else {
          for (int i=0; i < nBands; i++)           bandsMap[i]=i + 1;
        }
        returnVal=dataset.ReadRaster(srcRegionXOffset,srcRegionYOffset,srcRegionWidth,srcRegionHeight,dstWidth,dstHeight,bufferType,dataBuffer,bandsMap,nBands * typeSizeInBytes,dstWidth * nBands * typeSizeInBytes,typeSizeInBytes);
        byteBands[k]=dataBuffer;
      }
 else {
        Band rBand=null;
        try {
          rBand=dataset.GetRasterBand(k + 1);
          returnVal=rBand.ReadRaster(srcRegionXOffset,srcRegionYOffset,srcRegionWidth,srcRegionHeight,dstWidth,dstHeight,bufferType,dataBuffer);
          byteBands[k]=dataBuffer;
        }
  finally {
          if (rBand != null) {
            try {
              rBand.delete();
            }
 catch (            Throwable e) {
              if (LOGGER.isLoggable(Level.FINEST))               LOGGER.log(Level.FINEST,e.getLocalizedMessage(),e);
            }
          }
        }
      }
      if (returnVal == gdalconstConstants.CE_None) {
        if (!splitBands)         for (int band=0; band < nBands; band++) {
          banks[band]=band;
          offsets[band]=band;
        }
 else {
          banks[k]=k;
          offsets[k]=0;
        }
      }
 else {
        LOGGER.info(new StringBuilder("String_Node_Str").append(gdal.GetLastErrorMsg()).toString());
        LOGGER.info(new StringBuilder("String_Node_Str").append(gdal.GetLastErrorNo()).toString());
        LOGGER.info(new StringBuilder("String_Node_Str").append(gdal.GetLastErrorType()).toString());
        GDALUtilities.closeDataSet(dataset);
        throw new RuntimeException(gdal.GetLastErrorMsg());
      }
    }
    if (bufferType == gdalconstConstants.GDT_Byte) {
      if (!splitBands) {
        imgBuffer=new DataBufferByte(byteBands[0],nBands * pixels);
      }
 else {
        imgBuffer=new DataBufferByte(byteBands,pixels);
      }
      dataBufferType=DataBuffer.TYPE_BYTE;
    }
 else {
      ByteBuffer bands[]=new ByteBuffer[nBands];
      for (int k=0; k < nBands; k++) {
        bands[k]=ByteBuffer.wrap(byteBands[k],0,byteBands[k].length);
      }
      if (bufferType == gdalconstConstants.GDT_Int16 || bufferType == gdalconstConstants.GDT_UInt16) {
        if (!splitBands) {
          short[] shorts=new short[nBands * pixels];
          bands[0].order(ByteOrder.nativeOrder());
          final ShortBuffer buff=bands[0].asShortBuffer();
          buff.get(shorts,0,nBands * pixels);
          if (bufferType == gdalconstConstants.GDT_Int16)           imgBuffer=new DataBufferShort(shorts,nBands * pixels);
 else           imgBuffer=new DataBufferUShort(shorts,nBands * pixels);
        }
 else {
          short[][] shorts=new short[nBands][];
          for (int i=0; i < nBands; i++) {
            shorts[i]=new short[pixels];
            bands[i].order(ByteOrder.nativeOrder());
            bands[i].asShortBuffer().get(shorts[i],0,pixels);
          }
          if (bufferType == gdalconstConstants.GDT_Int16)           imgBuffer=new DataBufferShort(shorts,pixels);
 else           imgBuffer=new DataBufferUShort(shorts,pixels);
        }
        if (bufferType == gdalconstConstants.GDT_UInt16)         dataBufferType=DataBuffer.TYPE_USHORT;
 else         dataBufferType=DataBuffer.TYPE_SHORT;
      }
 else       if (bufferType == gdalconstConstants.GDT_Int32 || bufferType == gdalconstConstants.GDT_UInt32) {
        if (!splitBands) {
          int[] ints=new int[nBands * pixels];
          bands[0].order(ByteOrder.nativeOrder());
          final IntBuffer buff=bands[0].asIntBuffer();
          buff.get(ints,0,nBands * pixels);
          imgBuffer=new DataBufferInt(ints,nBands * pixels);
        }
 else {
          int[][] ints=new int[nBands][];
          for (int i=0; i < nBands; i++) {
            ints[i]=new int[pixels];
            bands[i].order(ByteOrder.nativeOrder());
            bands[i].asIntBuffer().get(ints[i],0,pixels);
          }
          imgBuffer=new DataBufferInt(ints,pixels);
        }
        dataBufferType=DataBuffer.TYPE_INT;
      }
 else       if (bufferType == gdalconstConstants.GDT_Float32) {
        if (!splitBands) {
          float[] floats=new float[nBands * pixels];
          bands[0].order(ByteOrder.nativeOrder());
          final FloatBuffer buff=bands[0].asFloatBuffer();
          buff.get(floats,0,nBands * pixels);
          imgBuffer=new DataBufferFloat(floats,nBands * pixels);
        }
 else {
          float[][] floats=new float[nBands][];
          for (int i=0; i < nBands; i++) {
            floats[i]=new float[pixels];
            bands[i].order(ByteOrder.nativeOrder());
            bands[i].asFloatBuffer().get(floats[i],0,pixels);
          }
          imgBuffer=new DataBufferFloat(floats,pixels);
        }
        dataBufferType=DataBuffer.TYPE_FLOAT;
      }
 else       if (bufferType == gdalconstConstants.GDT_Float64) {
        if (!splitBands) {
          double[] doubles=new double[nBands * pixels];
          bands[0].order(ByteOrder.nativeOrder());
          final DoubleBuffer buff=bands[0].asDoubleBuffer();
          buff.get(doubles,0,nBands * pixels);
          imgBuffer=new DataBufferDouble(doubles,nBands * pixels);
        }
 else {
          double[][] doubles=new double[nBands][];
          for (int i=0; i < nBands; i++) {
            doubles[i]=new double[pixels];
            bands[i].order(ByteOrder.nativeOrder());
            bands[i].asDoubleBuffer().get(doubles[i],0,pixels);
          }
          imgBuffer=new DataBufferDouble(doubles,pixels);
        }
        dataBufferType=DataBuffer.TYPE_DOUBLE;
      }
 else {
        LOGGER.info("String_Node_Str" + bufferType);
      }
    }
    if (splitBands)     sampleModel=new BandedSampleModel(dataBufferType,dstWidth,dstHeight,dstWidth,banks,offsets);
 else     sampleModel=new PixelInterleavedSampleModel(dataBufferType,dstWidth,dstHeight,nBands,dstWidth * nBands,offsets);
  }
  finally {
    if (pBand != null) {
      try {
        pBand.delete();
      }
 catch (      Throwable e) {
        if (LOGGER.isLoggable(Level.FINEST))         LOGGER.log(Level.FINEST,e.getLocalizedMessage(),e);
      }
    }
    if (dataset != null) {
      try {
        GDALUtilities.closeDataSet(dataset);
      }
 catch (      Throwable e) {
        if (LOGGER.isLoggable(Level.FINEST))         LOGGER.log(Level.FINEST,e.getLocalizedMessage(),e);
      }
    }
  }
  return Raster.createWritableRaster(sampleModel,imgBuffer,null);
}

Fixed version (ground truth):
/** 
 * Read data from the required region of the raster.
 * @param item a <code>GDALCommonIIOImageMetadata</code> related to the actual dataset
 * @param srcRegion the source Region to be read
 * @param dstRegion the destination Region of the image read
 * @param selectedBands an array specifying the requested bands
 * @return the read <code>Raster</code>
 */
private Raster readDatasetRaster(GDALCommonIIOImageMetadata item,Rectangle srcRegion,Rectangle dstRegion,int[] selectedBands,SampleModel destSampleModel) throws IOException {
  SampleModel destSm=destSampleModel != null ? destSampleModel : item.getSampleModel();
  SampleModel sampleModel=null;
  DataBuffer imgBuffer=null;
  final String datasetName=item.getDatasetName();
  final Dataset dataset=GDALUtilities.acquireDataSet(datasetName,gdalconst.GA_ReadOnly);
  Band pBand=null;
  if (dataset == null)   throw new IOException("String_Node_Str" + datasetName);
  try {
    int dstWidth=dstRegion.width;
    int dstHeight=dstRegion.height;
    int srcRegionXOffset=srcRegion.x;
    int srcRegionYOffset=srcRegion.y;
    int srcRegionWidth=srcRegion.width;
    int srcRegionHeight=srcRegion.height;
    if (LOGGER.isLoggable(Level.FINE))     LOGGER.fine("String_Node_Str" + srcRegion.toString());
    final int nBands=selectedBands != null ? selectedBands.length : destSm.getNumBands();
    int[] banks=new int[nBands];
    int[] offsets=new int[nBands];
    final int pixels=dstWidth * dstHeight;
    int bufferType=0, bufferSize=0;
    int typeSizeInBytes=0;
    pBand=dataset.GetRasterBand(1);
    bufferType=pBand.getDataType();
    typeSizeInBytes=gdal.GetDataTypeSize(bufferType) / 8;
    bufferSize=nBands * pixels * typeSizeInBytes;
    boolean splitBands=false;
    if (bufferSize < 0 || destSm instanceof BandedSampleModel) {
      bufferSize=pixels * typeSizeInBytes;
      splitBands=true;
    }
    int dataBufferType=-1;
    byte[][] byteBands=new byte[nBands][];
    for (int k=0; k < nBands; k++) {
      if (k > 0 && !splitBands)       break;
      final byte[] dataBuffer=new byte[bufferSize];
      final int returnVal;
      if (!splitBands) {
        final int bandsMap[]=new int[nBands];
        if (selectedBands != null) {
          for (int i=0; i < nBands; i++)           bandsMap[i]=selectedBands[i] + 1;
        }
 else {
          for (int i=0; i < nBands; i++)           bandsMap[i]=i + 1;
        }
        returnVal=dataset.ReadRaster(srcRegionXOffset,srcRegionYOffset,srcRegionWidth,srcRegionHeight,dstWidth,dstHeight,bufferType,dataBuffer,bandsMap,nBands * typeSizeInBytes,dstWidth * nBands * typeSizeInBytes,typeSizeInBytes);
        byteBands[k]=dataBuffer;
      }
 else {
        Band rBand=null;
        try {
          rBand=dataset.GetRasterBand(k + 1);
          returnVal=rBand.ReadRaster(srcRegionXOffset,srcRegionYOffset,srcRegionWidth,srcRegionHeight,dstWidth,dstHeight,bufferType,dataBuffer);
          byteBands[k]=dataBuffer;
        }
  finally {
          if (rBand != null) {
            try {
              rBand.delete();
            }
 catch (            Throwable e) {
              if (LOGGER.isLoggable(Level.FINEST))               LOGGER.log(Level.FINEST,e.getLocalizedMessage(),e);
            }
          }
        }
      }
      if (returnVal == gdalconstConstants.CE_None) {
        if (!splitBands)         for (int band=0; band < nBands; band++) {
          banks[band]=band;
          offsets[band]=band;
        }
 else {
          banks[k]=k;
          offsets[k]=0;
        }
      }
 else {
        LOGGER.info(new StringBuilder("String_Node_Str").append(gdal.GetLastErrorMsg()).toString());
        LOGGER.info(new StringBuilder("String_Node_Str").append(gdal.GetLastErrorNo()).toString());
        LOGGER.info(new StringBuilder("String_Node_Str").append(gdal.GetLastErrorType()).toString());
        GDALUtilities.closeDataSet(dataset);
        throw new RuntimeException(gdal.GetLastErrorMsg());
      }
    }
    if (bufferType == gdalconstConstants.GDT_Byte) {
      if (!splitBands) {
        imgBuffer=new DataBufferByte(byteBands[0],nBands * pixels);
      }
 else {
        imgBuffer=new DataBufferByte(byteBands,pixels);
      }
      dataBufferType=DataBuffer.TYPE_BYTE;
    }
 else {
      ByteBuffer bands[]=new ByteBuffer[nBands];
      for (int k=0; (splitBands && k < nBands) || (k < 1 && !splitBands); k++) {
        bands[k]=ByteBuffer.wrap(byteBands[k],0,byteBands[k].length);
      }
      if (bufferType == gdalconstConstants.GDT_Int16 || bufferType == gdalconstConstants.GDT_UInt16) {
        if (!splitBands) {
          short[] shorts=new short[nBands * pixels];
          bands[0].order(ByteOrder.nativeOrder());
          final ShortBuffer buff=bands[0].asShortBuffer();
          buff.get(shorts,0,nBands * pixels);
          if (bufferType == gdalconstConstants.GDT_Int16)           imgBuffer=new DataBufferShort(shorts,nBands * pixels);
 else           imgBuffer=new DataBufferUShort(shorts,nBands * pixels);
        }
 else {
          short[][] shorts=new short[nBands][];
          for (int i=0; i < nBands; i++) {
            shorts[i]=new short[pixels];
            bands[i].order(ByteOrder.nativeOrder());
            bands[i].asShortBuffer().get(shorts[i],0,pixels);
          }
          if (bufferType == gdalconstConstants.GDT_Int16)           imgBuffer=new DataBufferShort(shorts,pixels);
 else           imgBuffer=new DataBufferUShort(shorts,pixels);
        }
        if (bufferType == gdalconstConstants.GDT_UInt16)         dataBufferType=DataBuffer.TYPE_USHORT;
 else         dataBufferType=DataBuffer.TYPE_SHORT;
      }
 else       if (bufferType == gdalconstConstants.GDT_Int32 || bufferType == gdalconstConstants.GDT_UInt32) {
        if (!splitBands) {
          int[] ints=new int[nBands * pixels];
          bands[0].order(ByteOrder.nativeOrder());
          final IntBuffer buff=bands[0].asIntBuffer();
          buff.get(ints,0,nBands * pixels);
          imgBuffer=new DataBufferInt(ints,nBands * pixels);
        }
 else {
          int[][] ints=new int[nBands][];
          for (int i=0; i < nBands; i++) {
            ints[i]=new int[pixels];
            bands[i].order(ByteOrder.nativeOrder());
            bands[i].asIntBuffer().get(ints[i],0,pixels);
          }
          imgBuffer=new DataBufferInt(ints,pixels);
        }
        dataBufferType=DataBuffer.TYPE_INT;
      }
 else       if (bufferType == gdalconstConstants.GDT_Float32) {
        if (!splitBands) {
          float[] floats=new float[nBands * pixels];
          bands[0].order(ByteOrder.nativeOrder());
          final FloatBuffer buff=bands[0].asFloatBuffer();
          buff.get(floats,0,nBands * pixels);
          imgBuffer=new DataBufferFloat(floats,nBands * pixels);
        }
 else {
          float[][] floats=new float[nBands][];
          for (int i=0; i < nBands; i++) {
            floats[i]=new float[pixels];
            bands[i].order(ByteOrder.nativeOrder());
            bands[i].asFloatBuffer().get(floats[i],0,pixels);
          }
          imgBuffer=new DataBufferFloat(floats,pixels);
        }
        dataBufferType=DataBuffer.TYPE_FLOAT;
      }
 else       if (bufferType == gdalconstConstants.GDT_Float64) {
        if (!splitBands) {
          double[] doubles=new double[nBands * pixels];
          bands[0].order(ByteOrder.nativeOrder());
          final DoubleBuffer buff=bands[0].asDoubleBuffer();
          buff.get(doubles,0,nBands * pixels);
          imgBuffer=new DataBufferDouble(doubles,nBands * pixels);
        }
 else {
          double[][] doubles=new double[nBands][];
          for (int i=0; i < nBands; i++) {
            doubles[i]=new double[pixels];
            bands[i].order(ByteOrder.nativeOrder());
            bands[i].asDoubleBuffer().get(doubles[i],0,pixels);
          }
          imgBuffer=new DataBufferDouble(doubles,pixels);
        }
        dataBufferType=DataBuffer.TYPE_DOUBLE;
      }
 else {
        LOGGER.info("String_Node_Str" + bufferType);
      }
    }
    if (splitBands)     sampleModel=new BandedSampleModel(dataBufferType,dstWidth,dstHeight,dstWidth,banks,offsets);
 else     sampleModel=new PixelInterleavedSampleModel(dataBufferType,dstWidth,dstHeight,nBands,dstWidth * nBands,offsets);
  }
  finally {
    if (pBand != null) {
      try {
        pBand.delete();
      }
 catch (      Throwable e) {
        if (LOGGER.isLoggable(Level.FINEST))         LOGGER.log(Level.FINEST,e.getLocalizedMessage(),e);
      }
    }
    if (dataset != null) {
      try {
        GDALUtilities.closeDataSet(dataset);
      }
 catch (      Throwable e) {
        if (LOGGER.isLoggable(Level.FINEST))         LOGGER.log(Level.FINEST,e.getLocalizedMessage(),e);
      }
    }
  }
  return Raster.createWritableRaster(sampleModel,imgBuffer,null);
}