Program: 162436
Project: 162436
Buggy source:
/** 
 * Starts copy of file Supports :   {@link File},   {@link jcifs.smb.SmbFile},   {@link DocumentFile},   {@link CloudStorage}
 * @param lowOnMemory defines whether system is running low on memory, in which case we'll switch tousing streams instead of channel which maps the who buffer in memory. TODO: Use buffers even on low memory but don't map the whole file to memory but parts of it, and transfer each part instead.
 */
private void startCopy(boolean lowOnMemory) throws IOException {
  FileInputStream inputStream=null;
  FileOutputStream outputStream=null;
  FileChannel inChannel=null;
  FileChannel outChannel=null;
  BufferedInputStream bufferedInputStream=null;
  BufferedOutputStream bufferedOutputStream=null;
  try {
    if (mSourceFile.isOtgFile()) {
      ContentResolver contentResolver=mContext.getContentResolver();
      DocumentFile documentSourceFile=OTGUtil.getDocumentFile(mSourceFile.getPath(),mContext,false);
      bufferedInputStream=new BufferedInputStream(contentResolver.openInputStream(documentSourceFile.getUri()),DEFAULT_BUFFER_SIZE);
    }
 else     if (mSourceFile.isSmb()) {
      bufferedInputStream=new BufferedInputStream(mSourceFile.getInputStream(),DEFAULT_BUFFER_SIZE);
    }
 else     if (mSourceFile.isSftp()) {
      bufferedInputStream=new BufferedInputStream(mSourceFile.getInputStream(mContext),DEFAULT_BUFFER_SIZE);
    }
 else     if (mSourceFile.isDropBoxFile()) {
      CloudStorage cloudStorageDropbox=dataUtils.getAccount(OpenMode.DROPBOX);
      bufferedInputStream=new BufferedInputStream(cloudStorageDropbox.download(CloudUtil.stripPath(OpenMode.DROPBOX,mSourceFile.getPath())));
    }
 else     if (mSourceFile.isBoxFile()) {
      CloudStorage cloudStorageBox=dataUtils.getAccount(OpenMode.BOX);
      bufferedInputStream=new BufferedInputStream(cloudStorageBox.download(CloudUtil.stripPath(OpenMode.BOX,mSourceFile.getPath())));
    }
 else     if (mSourceFile.isGoogleDriveFile()) {
      CloudStorage cloudStorageGdrive=dataUtils.getAccount(OpenMode.GDRIVE);
      bufferedInputStream=new BufferedInputStream(cloudStorageGdrive.download(CloudUtil.stripPath(OpenMode.GDRIVE,mSourceFile.getPath())));
    }
 else     if (mSourceFile.isOneDriveFile()) {
      CloudStorage cloudStorageOnedrive=dataUtils.getAccount(OpenMode.ONEDRIVE);
      bufferedInputStream=new BufferedInputStream(cloudStorageOnedrive.download(CloudUtil.stripPath(OpenMode.ONEDRIVE,mSourceFile.getPath())));
    }
 else {
      File file=new File(mSourceFile.getPath());
      if (FileUtil.isReadable(file)) {
        if (mTargetFile.isOneDriveFile() || mTargetFile.isDropBoxFile() || mTargetFile.isGoogleDriveFile()|| mTargetFile.isBoxFile()|| lowOnMemory) {
          bufferedInputStream=new BufferedInputStream(new FileInputStream(file));
        }
 else {
          inChannel=new RandomAccessFile(file,"String_Node_Str").getChannel();
        }
      }
 else {
        ContentResolver contentResolver=mContext.getContentResolver();
        DocumentFile documentSourceFile=FileUtil.getDocumentFile(file,mSourceFile.isDirectory(),mContext);
        bufferedInputStream=new BufferedInputStream(contentResolver.openInputStream(documentSourceFile.getUri()),DEFAULT_BUFFER_SIZE);
      }
    }
    if (mTargetFile.isOtgFile()) {
      ContentResolver contentResolver=mContext.getContentResolver();
      DocumentFile documentTargetFile=OTGUtil.getDocumentFile(mTargetFile.getPath(),mContext,true);
      bufferedOutputStream=new BufferedOutputStream(contentResolver.openOutputStream(documentTargetFile.getUri()),DEFAULT_BUFFER_SIZE);
    }
 else     if (mTargetFile.isSftp()) {
      bufferedOutputStream=new BufferedOutputStream(mTargetFile.getOutputStream(mContext),DEFAULT_BUFFER_SIZE);
    }
 else     if (mTargetFile.isSmb()) {
      bufferedOutputStream=new BufferedOutputStream(mTargetFile.getOutputStream(mContext),DEFAULT_BUFFER_SIZE);
    }
 else     if (mTargetFile.isDropBoxFile()) {
      CloudStorage cloudStorageDropbox=dataUtils.getAccount(OpenMode.DROPBOX);
      if (mSourceFile.isDropBoxFile()) {
        cloudStorageDropbox.copy(CloudUtil.stripPath(OpenMode.DROPBOX,mSourceFile.getPath()),CloudUtil.stripPath(OpenMode.DROPBOX,mTargetFile.getPath()));
        return;
      }
 else {
        cloudStorageDropbox.upload(CloudUtil.stripPath(OpenMode.DROPBOX,mTargetFile.getPath()),bufferedInputStream,mSourceFile.getSize(),true);
        return;
      }
    }
 else     if (mTargetFile.isBoxFile()) {
      CloudStorage cloudStorageBox=dataUtils.getAccount(OpenMode.BOX);
      if (mSourceFile.isBoxFile()) {
        cloudStorageBox.copy(CloudUtil.stripPath(OpenMode.BOX,mSourceFile.getPath()),CloudUtil.stripPath(OpenMode.BOX,mTargetFile.getPath()));
        return;
      }
 else {
        cloudStorageBox.upload(CloudUtil.stripPath(OpenMode.BOX,mTargetFile.getPath()),bufferedInputStream,mSourceFile.getSize(),true);
        bufferedInputStream.close();
        return;
      }
    }
 else     if (mTargetFile.isGoogleDriveFile()) {
      CloudStorage cloudStorageGdrive=dataUtils.getAccount(OpenMode.GDRIVE);
      if (mSourceFile.isGoogleDriveFile()) {
        cloudStorageGdrive.copy(CloudUtil.stripPath(OpenMode.GDRIVE,mSourceFile.getPath()),CloudUtil.stripPath(OpenMode.GDRIVE,mTargetFile.getPath()));
        return;
      }
 else {
        cloudStorageGdrive.upload(CloudUtil.stripPath(OpenMode.GDRIVE,mTargetFile.getPath()),bufferedInputStream,mSourceFile.getSize(),true);
        bufferedInputStream.close();
        return;
      }
    }
 else     if (mTargetFile.isOneDriveFile()) {
      CloudStorage cloudStorageOnedrive=dataUtils.getAccount(OpenMode.ONEDRIVE);
      if (mSourceFile.isOneDriveFile()) {
        cloudStorageOnedrive.copy(CloudUtil.stripPath(OpenMode.ONEDRIVE,mSourceFile.getPath()),CloudUtil.stripPath(OpenMode.ONEDRIVE,mTargetFile.getPath()));
        return;
      }
 else {
        cloudStorageOnedrive.upload(CloudUtil.stripPath(OpenMode.ONEDRIVE,mTargetFile.getPath()),bufferedInputStream,mSourceFile.getSize(),true);
        bufferedInputStream.close();
        return;
      }
    }
 else {
      File file=new File(mTargetFile.getPath());
      if (FileUtil.isWritable(file)) {
        if (lowOnMemory) {
          bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(file));
        }
 else {
          outChannel=new RandomAccessFile(file,"String_Node_Str").getChannel();
        }
      }
 else {
        ContentResolver contentResolver=mContext.getContentResolver();
        DocumentFile documentTargetFile=FileUtil.getDocumentFile(file,mTargetFile.isDirectory(),mContext);
        bufferedOutputStream=new BufferedOutputStream(contentResolver.openOutputStream(documentTargetFile.getUri()),DEFAULT_BUFFER_SIZE);
      }
    }
    if (bufferedInputStream != null) {
      if (bufferedOutputStream != null)       copyFile(bufferedInputStream,bufferedOutputStream);
 else       if (outChannel != null) {
        copyFile(bufferedInputStream,outChannel);
      }
    }
 else     if (inChannel != null) {
      if (bufferedOutputStream != null)       copyFile(inChannel,bufferedOutputStream);
 else       if (outChannel != null)       copyFile(inChannel,outChannel);
    }
  }
 catch (  IOException e) {
    e.printStackTrace();
    Log.d(getClass().getSimpleName(),"String_Node_Str");
    throw new IOException();
  }
catch (  OutOfMemoryError e) {
    e.printStackTrace();
    AppConfig.toast(mContext,mContext.getString(R.string.copy_low_memory));
    startCopy(true);
  }
 finally {
    try {
      if (inChannel != null)       inChannel.close();
      if (outChannel != null)       outChannel.close();
      if (inputStream != null)       inputStream.close();
      if (outputStream != null)       outputStream.close();
      if (bufferedInputStream != null)       bufferedInputStream.close();
      if (bufferedOutputStream != null)       bufferedOutputStream.close();
    }
 catch (    IOException e) {
      e.printStackTrace();
    }
  }
}

Fixed version (ground truth):
/** 
 * Starts copy of file Supports :   {@link File},   {@link jcifs.smb.SmbFile},   {@link DocumentFile},   {@link CloudStorage}
 * @param lowOnMemory defines whether system is running low on memory, in which case we'll switch tousing streams instead of channel which maps the who buffer in memory. TODO: Use buffers even on low memory but don't map the whole file to memory but parts of it, and transfer each part instead.
 */
private void startCopy(boolean lowOnMemory) throws IOException {
  FileInputStream inputStream=null;
  FileOutputStream outputStream=null;
  FileChannel inChannel=null;
  FileChannel outChannel=null;
  BufferedInputStream bufferedInputStream=null;
  BufferedOutputStream bufferedOutputStream=null;
  try {
    if (mSourceFile.isOtgFile()) {
      ContentResolver contentResolver=mContext.getContentResolver();
      DocumentFile documentSourceFile=OTGUtil.getDocumentFile(mSourceFile.getPath(),mContext,false);
      bufferedInputStream=new BufferedInputStream(contentResolver.openInputStream(documentSourceFile.getUri()),DEFAULT_BUFFER_SIZE);
    }
 else     if (mSourceFile.isSmb()) {
      bufferedInputStream=new BufferedInputStream(mSourceFile.getInputStream(),DEFAULT_BUFFER_SIZE);
    }
 else     if (mSourceFile.isSftp()) {
      bufferedInputStream=new BufferedInputStream(mSourceFile.getInputStream(mContext),DEFAULT_BUFFER_SIZE);
    }
 else     if (mSourceFile.isDropBoxFile()) {
      CloudStorage cloudStorageDropbox=dataUtils.getAccount(OpenMode.DROPBOX);
      bufferedInputStream=new BufferedInputStream(cloudStorageDropbox.download(CloudUtil.stripPath(OpenMode.DROPBOX,mSourceFile.getPath())));
    }
 else     if (mSourceFile.isBoxFile()) {
      CloudStorage cloudStorageBox=dataUtils.getAccount(OpenMode.BOX);
      bufferedInputStream=new BufferedInputStream(cloudStorageBox.download(CloudUtil.stripPath(OpenMode.BOX,mSourceFile.getPath())));
    }
 else     if (mSourceFile.isGoogleDriveFile()) {
      CloudStorage cloudStorageGdrive=dataUtils.getAccount(OpenMode.GDRIVE);
      bufferedInputStream=new BufferedInputStream(cloudStorageGdrive.download(CloudUtil.stripPath(OpenMode.GDRIVE,mSourceFile.getPath())));
    }
 else     if (mSourceFile.isOneDriveFile()) {
      CloudStorage cloudStorageOnedrive=dataUtils.getAccount(OpenMode.ONEDRIVE);
      bufferedInputStream=new BufferedInputStream(cloudStorageOnedrive.download(CloudUtil.stripPath(OpenMode.ONEDRIVE,mSourceFile.getPath())));
    }
 else {
      File file=new File(mSourceFile.getPath());
      if (FileUtil.isReadable(file)) {
        if (mTargetFile.isOneDriveFile() || mTargetFile.isDropBoxFile() || mTargetFile.isGoogleDriveFile()|| mTargetFile.isBoxFile()|| lowOnMemory) {
          bufferedInputStream=new BufferedInputStream(new FileInputStream(file));
        }
 else {
          inChannel=new RandomAccessFile(file,"String_Node_Str").getChannel();
        }
      }
 else {
        ContentResolver contentResolver=mContext.getContentResolver();
        DocumentFile documentSourceFile=FileUtil.getDocumentFile(file,mSourceFile.isDirectory(),mContext);
        bufferedInputStream=new BufferedInputStream(contentResolver.openInputStream(documentSourceFile.getUri()),DEFAULT_BUFFER_SIZE);
      }
    }
    if (mTargetFile.isOtgFile()) {
      ContentResolver contentResolver=mContext.getContentResolver();
      DocumentFile documentTargetFile=OTGUtil.getDocumentFile(mTargetFile.getPath(),mContext,true);
      bufferedOutputStream=new BufferedOutputStream(contentResolver.openOutputStream(documentTargetFile.getUri()),DEFAULT_BUFFER_SIZE);
    }
 else     if (mTargetFile.isSftp()) {
      bufferedOutputStream=new BufferedOutputStream(mTargetFile.getOutputStream(mContext),DEFAULT_BUFFER_SIZE);
    }
 else     if (mTargetFile.isSmb()) {
      bufferedOutputStream=new BufferedOutputStream(mTargetFile.getOutputStream(mContext),DEFAULT_BUFFER_SIZE);
    }
 else     if (mTargetFile.isDropBoxFile()) {
      CloudStorage cloudStorageDropbox=dataUtils.getAccount(OpenMode.DROPBOX);
      if (mSourceFile.isDropBoxFile()) {
        cloudStorageDropbox.copy(CloudUtil.stripPath(OpenMode.DROPBOX,mSourceFile.getPath()),CloudUtil.stripPath(OpenMode.DROPBOX,mTargetFile.getPath()));
        return;
      }
 else {
        cloudStorageDropbox.upload(CloudUtil.stripPath(OpenMode.DROPBOX,mTargetFile.getPath()),bufferedInputStream,mSourceFile.getSize(),true);
        return;
      }
    }
 else     if (mTargetFile.isBoxFile()) {
      CloudStorage cloudStorageBox=dataUtils.getAccount(OpenMode.BOX);
      if (mSourceFile.isBoxFile()) {
        cloudStorageBox.copy(CloudUtil.stripPath(OpenMode.BOX,mSourceFile.getPath()),CloudUtil.stripPath(OpenMode.BOX,mTargetFile.getPath()));
        return;
      }
 else {
        cloudStorageBox.upload(CloudUtil.stripPath(OpenMode.BOX,mTargetFile.getPath()),bufferedInputStream,mSourceFile.getSize(),true);
        bufferedInputStream.close();
        return;
      }
    }
 else     if (mTargetFile.isGoogleDriveFile()) {
      CloudStorage cloudStorageGdrive=dataUtils.getAccount(OpenMode.GDRIVE);
      if (mSourceFile.isGoogleDriveFile()) {
        cloudStorageGdrive.copy(CloudUtil.stripPath(OpenMode.GDRIVE,mSourceFile.getPath()),CloudUtil.stripPath(OpenMode.GDRIVE,mTargetFile.getPath()));
        return;
      }
 else {
        cloudStorageGdrive.upload(CloudUtil.stripPath(OpenMode.GDRIVE,mTargetFile.getPath()),bufferedInputStream,mSourceFile.getSize(),true);
        bufferedInputStream.close();
        return;
      }
    }
 else     if (mTargetFile.isOneDriveFile()) {
      CloudStorage cloudStorageOnedrive=dataUtils.getAccount(OpenMode.ONEDRIVE);
      if (mSourceFile.isOneDriveFile()) {
        cloudStorageOnedrive.copy(CloudUtil.stripPath(OpenMode.ONEDRIVE,mSourceFile.getPath()),CloudUtil.stripPath(OpenMode.ONEDRIVE,mTargetFile.getPath()));
        return;
      }
 else {
        cloudStorageOnedrive.upload(CloudUtil.stripPath(OpenMode.ONEDRIVE,mTargetFile.getPath()),bufferedInputStream,mSourceFile.getSize(),true);
        bufferedInputStream.close();
        return;
      }
    }
 else {
      File file=new File(mTargetFile.getPath());
      if (FileUtil.isWritable(file)) {
        if (lowOnMemory) {
          bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(file));
        }
 else {
          outChannel=new RandomAccessFile(file,"String_Node_Str").getChannel();
        }
      }
 else {
        ContentResolver contentResolver=mContext.getContentResolver();
        DocumentFile documentTargetFile=FileUtil.getDocumentFile(file,mTargetFile.isDirectory(),mContext);
        bufferedOutputStream=new BufferedOutputStream(contentResolver.openOutputStream(documentTargetFile.getUri()),DEFAULT_BUFFER_SIZE);
      }
    }
    if (bufferedInputStream != null) {
      if (bufferedOutputStream != null)       copyFile(bufferedInputStream,bufferedOutputStream);
 else       if (outChannel != null) {
        copyFile(bufferedInputStream,outChannel);
      }
    }
 else     if (inChannel != null) {
      if (bufferedOutputStream != null)       copyFile(inChannel,bufferedOutputStream);
 else       if (outChannel != null)       copyFile(inChannel,outChannel);
    }
  }
 catch (  IOException e) {
    e.printStackTrace();
    Log.d(getClass().getSimpleName(),"String_Node_Str");
    throw new IOException();
  }
catch (  OutOfMemoryError e) {
    e.printStackTrace();
    AppConfig.toast(mContext,mContext.getString(R.string.copy_low_memory));
    startCopy(true);
  }
 finally {
    try {
      if (inChannel != null)       inChannel.close();
      if (outChannel != null)       outChannel.close();
      if (inputStream != null)       inputStream.close();
      if (outputStream != null)       outputStream.close();
      if (bufferedInputStream != null)       bufferedInputStream.close();
      if (bufferedOutputStream != null)       bufferedOutputStream.close();
    }
 catch (    IOException e) {
      e.printStackTrace();
    }
    if ((mTargetFile.isLocal() || mTargetFile.isOtgFile()) && mTargetFile.exists(mContext)) {
      DocumentFile documentFile=FileUtil.getDocumentFile(mTargetFile.getFile(),false,mContext);
      if (documentFile == null)       documentFile=DocumentFile.fromFile(mTargetFile.getFile());
      FileUtils.scanFile(documentFile.getUri(),mContext);
    }
  }
}