Program: 168506
Project: 168506
Buggy source:
/** 
 * {@inheritDoc}
 */
@Override public int read(byte[] data,int pos,int len){
  long offset=fsOffset;
  long bytesToSkip=logicalPosition;
  int extIndex;
  long currentExtentLength;
  for (extIndex=0; extIndex < extentDescriptors.length; ++extIndex) {
    CommonHFSExtentDescriptor cur=extentDescriptors[extIndex];
    currentExtentLength=cur.getBlockCount() * allocationBlockSize;
    if (bytesToSkip >= currentExtentLength) {
      if (extIndex < extentDescriptors.length - 1)       bytesToSkip-=currentExtentLength;
 else {
        return -1;
      }
    }
 else {
      offset=fsOffset + firstBlockByteOffset + (cur.getStartBlock() * allocationBlockSize)+ bytesToSkip;
      break;
    }
  }
  if (logicalPosition != lastLogicalPos) {
    sourceFile.seek(offset);
  }
 else   if (sourceFile.getFilePointer() != lastPhysicalPos) {
    sourceFile.seek(lastPhysicalPos);
  }
  int bytesLeftToRead=len;
  for (; extIndex < extentDescriptors.length; ++extIndex) {
    CommonHFSExtentDescriptor cur=extentDescriptors[extIndex];
    long bytesInExtent=cur.getBlockCount() * allocationBlockSize - bytesToSkip;
    int bytesToReadFromExtent=(bytesInExtent < bytesLeftToRead) ? (int)bytesInExtent : bytesLeftToRead;
    int bytesReadFromExtent=0;
    while (bytesReadFromExtent < bytesToReadFromExtent) {
      int bytesToRead=bytesToReadFromExtent - bytesReadFromExtent;
      int positionInArray=pos + (len - bytesLeftToRead) + bytesReadFromExtent;
      int bytesRead=sourceFile.read(data,positionInArray,bytesToRead);
      if (bytesRead > 0)       bytesReadFromExtent+=bytesRead;
 else {
        lastPhysicalPos=sourceFile.getFilePointer();
        int totalBytesRead=positionInArray - pos;
        logicalPosition+=totalBytesRead;
        return totalBytesRead;
      }
    }
    bytesLeftToRead-=bytesReadFromExtent;
    bytesToSkip=0;
    if (bytesLeftToRead == 0)     break;
  }
  lastPhysicalPos=sourceFile.getFilePointer();
  logicalPosition+=len - bytesLeftToRead;
  if (bytesLeftToRead < len)   return len - bytesLeftToRead;
 else   return -1;
}

Fixed version (ground truth):
/** 
 * {@inheritDoc}
 */
@Override public int read(byte[] data,int pos,int len){
  long offset=fsOffset;
  long bytesToSkip=logicalPosition;
  int extIndex;
  long currentExtentLength;
  for (extIndex=0; extIndex < extentDescriptors.length; ++extIndex) {
    CommonHFSExtentDescriptor cur=extentDescriptors[extIndex];
    currentExtentLength=cur.getBlockCount() * allocationBlockSize;
    if (bytesToSkip >= currentExtentLength) {
      if (extIndex < extentDescriptors.length - 1)       bytesToSkip-=currentExtentLength;
 else {
        return -1;
      }
    }
 else {
      offset=fsOffset + firstBlockByteOffset + (cur.getStartBlock() * allocationBlockSize)+ bytesToSkip;
      break;
    }
  }
  if (logicalPosition != lastLogicalPos) {
    sourceFile.seek(offset);
  }
 else   if (sourceFile.getFilePointer() != lastPhysicalPos) {
    sourceFile.seek(lastPhysicalPos);
  }
  long bytesLeftInStream=forkLength - logicalPosition;
  int totalBytesToRead=bytesLeftInStream < len ? (int)bytesLeftInStream : len;
  int bytesLeftToRead=totalBytesToRead;
  for (; extIndex < extentDescriptors.length; ++extIndex) {
    CommonHFSExtentDescriptor cur=extentDescriptors[extIndex];
    long bytesInExtent=cur.getBlockCount() * allocationBlockSize - bytesToSkip;
    int bytesToReadFromExtent=(bytesInExtent < bytesLeftToRead) ? (int)bytesInExtent : bytesLeftToRead;
    int bytesReadFromExtent=0;
    while (bytesReadFromExtent < bytesToReadFromExtent) {
      int bytesToRead=bytesToReadFromExtent - bytesReadFromExtent;
      int positionInArray=pos + (totalBytesToRead - bytesLeftToRead) + bytesReadFromExtent;
      int bytesRead=sourceFile.read(data,positionInArray,bytesToRead);
      if (bytesRead > 0)       bytesReadFromExtent+=bytesRead;
 else {
        lastPhysicalPos=sourceFile.getFilePointer();
        int totalBytesRead=positionInArray - pos;
        logicalPosition+=totalBytesRead;
        return totalBytesRead;
      }
    }
    bytesLeftToRead-=bytesReadFromExtent;
    bytesToSkip=0;
    if (bytesLeftToRead == 0)     break;
  }
  lastPhysicalPos=sourceFile.getFilePointer();
  logicalPosition+=totalBytesToRead - bytesLeftToRead;
  if (bytesLeftToRead < totalBytesToRead) {
    int bytesRead=totalBytesToRead - bytesLeftToRead;
    return bytesRead;
  }
 else   return -1;
}