Program: 80175
Project: 80175
Buggy source:
/** 
 * Replaces a block of bytes in the middle of the LOB with a another block of bytes, which may be of a different size. <p> The new byte array may not be be of same length as the original, thus it may result in resizing the total lob.
 * @param buf byte array which will be written inplace of old block
 * @param stPos inclusive starting position of current block
 * @param endPos exclusive end position of current block
 * @return Current position after write.
 * @throws IOExcepton if writing to temporary file fails
 * @throws StandardException
 * @throws SQLException
 */
synchronized long replaceBytes(byte[] buf,long stPos,long endPos) throws IOException, SQLException, StandardException {
  long length=getLength();
  long finalLength=length - endPos + stPos + buf.length;
  if (isBytes) {
    if (finalLength > bufferSize) {
      byte[] tmpBytes=dataBytes;
      init(tmpBytes,stPos);
      write(buf,0,buf.length,getLength());
      if (endPos < length)       write(tmpBytes,(int)endPos,(int)(length - endPos),getLength());
    }
 else {
      byte[] tmpByte=new byte[(int)finalLength];
      System.arraycopy(dataBytes,0,tmpByte,0,(int)stPos);
      System.arraycopy(buf,0,tmpByte,(int)stPos,(int)buf.length);
      if (endPos < length)       System.arraycopy(dataBytes,(int)endPos,tmpByte,(int)(stPos + buf.length),(int)(length - endPos));
      dataBytes=tmpByte;
    }
  }
 else {
    byte tmp[]=new byte[0];
    LOBFile oldFile=tmpFile;
    init(tmp,0);
    byte[] tmpByte=new byte[1024];
    long sz=stPos;
    oldFile.seek(0);
    while (sz != 0) {
      int readLen=(int)Math.min(1024,sz);
      int actualLength=oldFile.read(tmpByte,0,readLen);
      if (actualLength == -1)       break;
      tmpFile.write(tmpByte,0,actualLength);
      sz-=actualLength;
    }
    tmpFile.write(buf);
    oldFile.seek(endPos);
    int rdLen;
    if (endPos < length) {
      do {
        rdLen=oldFile.read(tmpByte,0,1024);
        if (rdLen == -1)         break;
        tmpFile.write(tmpByte,0,rdLen);
      }
 while (true);
    }
  }
  updateCount++;
  return stPos + buf.length;
}

Fixed version (ground truth):
/** 
 * Replaces a block of bytes in the middle of the LOB with a another block of bytes, which may be of a different size. <p> The new byte array may not be be of same length as the original, thus it may result in resizing the total lob.
 * @param buf byte array which will be written inplace of old block
 * @param stPos inclusive starting position of current block
 * @param endPos exclusive end position of current block
 * @return Current position after write.
 * @throws IOExcepton if writing to temporary file fails
 * @throws StandardException
 * @throws SQLException
 */
synchronized long replaceBytes(byte[] buf,long stPos,long endPos) throws IOException, SQLException, StandardException {
  long length=getLength();
  long finalLength=length - endPos + stPos + buf.length;
  if (isBytes) {
    if (finalLength > bufferSize) {
      byte[] tmpBytes=dataBytes;
      init(tmpBytes,stPos);
      write(buf,0,buf.length,getLength());
      if (endPos < length)       write(tmpBytes,(int)endPos,(int)(length - endPos),getLength());
    }
 else {
      byte[] tmpByte=new byte[(int)finalLength];
      System.arraycopy(dataBytes,0,tmpByte,0,(int)stPos);
      System.arraycopy(buf,0,tmpByte,(int)stPos,(int)buf.length);
      if (endPos < length)       System.arraycopy(dataBytes,(int)endPos,tmpByte,(int)(stPos + buf.length),(int)(length - endPos));
      dataBytes=tmpByte;
    }
  }
 else {
    byte tmp[]=new byte[0];
    LOBFile oldFile=tmpFile;
    StorageFile oldStoreFile=lobFile;
    init(tmp,0);
    byte[] tmpByte=new byte[1024];
    long sz=stPos;
    oldFile.seek(0);
    while (sz != 0) {
      int readLen=(int)Math.min(1024,sz);
      int actualLength=oldFile.read(tmpByte,0,readLen);
      if (actualLength == -1)       break;
      tmpFile.write(tmpByte,0,actualLength);
      sz-=actualLength;
    }
    tmpFile.write(buf);
    oldFile.seek(endPos);
    int rdLen;
    if (endPos < length) {
      do {
        rdLen=oldFile.read(tmpByte,0,1024);
        if (rdLen == -1)         break;
        tmpFile.write(tmpByte,0,rdLen);
      }
 while (true);
      oldFile.close();
      deleteFile(oldStoreFile);
    }
  }
  updateCount++;
  return stPos + buf.length;
}