gt:             if (fileNameIv != null) {
context: /*
   * This is the source code of Telegram for Android v. 3.x.x.
   * It is licensed under GNU GPL v. 2 or later.
   * You should have received a copy of the license in this archive (see LICENSE).
   *
   * Copyright Nikolai Kudashov, 2013-2017.
   */
  
  package com.porogram.profosure1.messenger;
  
  import com.porogram.profosure1.tgnet.ConnectionsManager;
  import com.porogram.profosure1.tgnet.NativeByteBuffer;
  import com.porogram.profosure1.tgnet.RequestDelegate;
  import com.porogram.profosure1.tgnet.TLObject;
  import com.porogram.profosure1.tgnet.TLRPC;
  
  import java.io.RandomAccessFile;
  import java.io.File;
  import java.nio.channels.FileChannel;
  import java.util.ArrayList;
  import java.util.Scanner;
  
  public class FileLoadOperation {
  
      private static class RequestInfo {
          private int requestToken;
          private int offset;
          private TLRPC.TL_upload_file response;
          private TLRPC.TL_upload_webFile responseWeb;
      }
  
      private final static int stateIdle = 0;
      private final static int stateDownloading = 1;
      private final static int stateFailed = 2;
      private final static int stateFinished = 3;
  
      private final static int downloadChunkSize = 1024 * 32;
      private final static int downloadChunkSizeBig = 1024 * 128;
      private final static int maxDownloadRequests = 4;
      private final static int maxDownloadRequestsBig = 2;
      private final static int bigFileSizeFrom = 1024 * 1024;
  
      private boolean started;
      private int datacenter_id;
      private TLRPC.InputFileLocation location;
      private TLRPC.TL_inputWebFileLocation webLocation;
      private volatile int state = stateIdle;
      private int downloadedBytes;
      private int totalBytesCount;
      private int bytesCountPadding;
      private FileLoadOperationDelegate delegate;
      private byte[] key;
      private byte[] iv;
      private int currentDownloadChunkSize;
      private int currentMaxDownloadRequests;
      private int requestsCount;
      private int renameRetryCount;
  
      private int nextDownloadOffset;
      private ArrayList<RequestInfo> requestInfos;
      private ArrayList<RequestInfo> delayedRequestInfos;
  
      private File cacheFileTemp;
      private File cacheFileFinal;
      private File cacheIvTemp;
  
      private String ext;
      private RandomAccessFile fileOutputStream;
      private RandomAccessFile fiv;
      private File storePath;
      private File tempPath;
      private boolean isForceRequest;
  
      private int currentType;
  
      public interface FileLoadOperationDelegate {
          void didFinishLoadingFile(FileLoadOperation operation, File finalFile);
          void didFailedLoadingFile(FileLoadOperation operation, int state);
          void didChangedLoadProgress(FileLoadOperation operation, float progress);
      }
  
      public FileLoadOperation(TLRPC.FileLocation photoLocation, String extension, int size) {
          if (photoLocation instanceof TLRPC.TL_fileEncryptedLocation) {
              location = new TLRPC.TL_inputEncryptedFileLocation();
              location.id = photoLocation.volume_id;
              location.volume_id = photoLocation.volume_id;
              location.access_hash = photoLocation.secret;
              location.local_id = photoLocation.local_id;
              iv = new byte[32];
              System.arraycopy(photoLocation.iv, 0, iv, 0, iv.length);
              key = photoLocation.key;
              datacenter_id = photoLocation.dc_id;
          } else if (photoLocation instanceof TLRPC.TL_fileLocation) {
              location = new TLRPC.TL_inputFileLocation();
              location.volume_id = photoLocation.volume_id;
              location.secret = photoLocation.secret;
              location.local_id = photoLocation.local_id;
              datacenter_id = photoLocation.dc_id;
          }
          currentType = ConnectionsManager.FileTypePhoto;
          totalBytesCount = size;
          ext = extension != null ? extension : "jpg";
      }
  
      public FileLoadOperation(TLRPC.TL_webDocument webDocument) {
          webLocation = new TLRPC.TL_inputWebFileLocation();
          webLocation.url = webDocument.url;
          webLocation.access_hash = webDocument.access_hash;
          totalBytesCount = webDocument.size;
          datacenter_id = webDocument.dc_id;
          String defaultExt = FileLoader.getExtensionByMime(webDocument.mime_type);
          if (webDocument.mime_type.startsWith("image/")) {
              currentType = ConnectionsManager.FileTypePhoto;
          } else if (webDocument.mime_type.equals("audio/ogg")) {
              currentType = ConnectionsManager.FileTypeAudio;
          } else if (webDocument.mime_type.startsWith("video/")) {
              currentType = ConnectionsManager.FileTypeVideo;
          } else {
              currentType = ConnectionsManager.FileTypeFile;
          }
          ext = ImageLoader.getHttpUrlExtension(webDocument.url, defaultExt);
      }
  
      public FileLoadOperation(TLRPC.Document documentLocation) {
          try {
              if (documentLocation instanceof TLRPC.TL_documentEncrypted) {
                  location = new TLRPC.TL_inputEncryptedFileLocation();
                  location.id = documentLocation.id;
                  location.access_hash = documentLocation.access_hash;
                  datacenter_id = documentLocation.dc_id;
                  iv = new byte[32];
                  System.arraycopy(documentLocation.iv, 0, iv, 0, iv.length);
                  key = documentLocation.key;
              } else if (documentLocation instanceof TLRPC.TL_document) {
                  location = new TLRPC.TL_inputDocumentFileLocation();
                  location.id = documentLocation.id;
                  location.access_hash = documentLocation.access_hash;
                  datacenter_id = documentLocation.dc_id;
              }
              totalBytesCount = documentLocation.size;
              if (key != null) {
                  int toAdd = 0;
                  if (totalBytesCount % 16 != 0) {
                      bytesCountPadding = 16 - totalBytesCount % 16;
                      totalBytesCount += bytesCountPadding;
                  }
              }
              ext = FileLoader.getDocumentFileName(documentLocation);
              int idx;
              if (ext == null || (idx = ext.lastIndexOf('.')) == -1) {
                  ext = "";
              } else {
                  ext = ext.substring(idx);
              }
              if ("audio/ogg".equals(documentLocation.mime_type)) {
                  currentType = ConnectionsManager.FileTypeAudio;
              } else if ("video/mp4".equals(documentLocation.mime_type)) {
                  currentType = ConnectionsManager.FileTypeVideo;
              } else {
                  currentType = ConnectionsManager.FileTypeFile;
              }
              if (ext.length() <= 1) {
                  if (documentLocation.mime_type != null) {
                      switch (documentLocation.mime_type) {
                          case "video/mp4":
                              ext = ".mp4";
                              break;
                          case "audio/ogg":
                              ext = ".ogg";
                              break;
                          default:
                              ext = "";
                              break;
                      }
                  } else {
                      ext = "";
                  }
              }
          } catch (Exception e) {
              FileLog.e(e);
              onFail(true, 0);
          }
      }
  
      public void setForceRequest(boolean forceRequest) {
          isForceRequest = forceRequest;
      }
  
      public boolean isForceRequest() {
          return isForceRequest;
      }
  
      public void setPaths(File store, File temp) {
          storePath = store;
          tempPath = temp;
      }
  
      public boolean wasStarted() {
          return started;
      }
  
      public int getCurrentType() {
          return currentType;
      }
  
      public String getFileName() {
          if (location != null) {
              return location.volume_id + "_" + location.local_id + "." + ext;
          } else {
              return Utilities.MD5(webLocation.url) + "." + ext;
          }
      }
  
      public boolean start() {
          if (state != stateIdle) {
              return false;
          }
          if (location == null && webLocation == null) {
              onFail(true, 0);
              return false;
          }
  
          String fileNameFinal;
          String fileNameTemp;
          String fileNameIv = null;
          if (webLocation != null) {
              String md5 = Utilities.MD5(webLocation.url);
              fileNameTemp = md5 + ".temp";
              fileNameFinal = md5 + "." + ext;
              if (key != null) {
                  fileNameIv = md5 + ".iv";
              }
          } else {
              if (location.volume_id != 0 && location.local_id != 0) {
                  if (datacenter_id == Integer.MIN_VALUE || location.volume_id == Integer.MIN_VALUE || datacenter_id == 0) {
                      onFail(true, 0);
                      return false;
                  }
  
                  fileNameTemp = location.volume_id + "_" + location.local_id + ".temp";
                  fileNameFinal = location.volume_id + "_" + location.local_id + "." + ext;
                  if (key != null) {
                      fileNameIv = location.volume_id + "_" + location.local_id + ".iv";
                  }
              } else {
                  if (datacenter_id == 0 || location.id == 0) {
                      onFail(true, 0);
                      return false;
                  }
  
                  fileNameTemp = datacenter_id + "_" + location.id + ".temp";
                  fileNameFinal = datacenter_id + "_" + location.id + ext;
                  if (key != null) {
                      fileNameIv = datacenter_id + "_" + location.id + ".iv";
                  }
              }
          }
          currentDownloadChunkSize = totalBytesCount >= bigFileSizeFrom ? downloadChunkSizeBig : downloadChunkSize;
          currentMaxDownloadRequests = totalBytesCount >= bigFileSizeFrom ? maxDownloadRequestsBig : maxDownloadRequests;
          requestInfos = new ArrayList<>(currentMaxDownloadRequests);
          delayedRequestInfos = new ArrayList<>(currentMaxDownloadRequests - 1);
          state = stateDownloading;
  
          cacheFileFinal = new File(storePath, fileNameFinal);
          boolean exist = cacheFileFinal.exists();
          if (exist && totalBytesCount != 0 && totalBytesCount != cacheFileFinal.length()) {
              cacheFileFinal.delete();
          }
  
          if (!cacheFileFinal.exists()) {
              cacheFileTemp = new File(tempPath, fileNameTemp);
              if (cacheFileTemp.exists()) {
                  downloadedBytes = (int) cacheFileTemp.length();
                  nextDownloadOffset = downloadedBytes = downloadedBytes / currentDownloadChunkSize * currentDownloadChunkSize;
              }
  
              if (BuildVars.DEBUG_VERSION) {
                  FileLog.d("start loading file to temp = " + cacheFileTemp + " final = " + cacheFileFinal);
              }
  
  