@Override
 public String toString() {
   return "FlashTarget [target ID="
       + TargetTypes.toString(mTarget.getId())
       + ", data.length="
       + mData.length
       + ", type="
       + mType
       + ", startPage="
       + mStartPage
       + "]";
 }
  // def _internal_flash(self, target, current_file_number=1, total_files=1):
  public void internalFlash(FlashTarget flashTarget, int currentFileNo, int totalFiles) {
    Target t_data = flashTarget.getTarget();
    byte[] image = flashTarget.getData();
    int pageSize = t_data.getPageSize();
    int startPage = flashTarget.getStartPage();

    String flashingTo =
        "Flashing to " + TargetTypes.toString(t_data.getId()) + " (" + flashTarget.getType() + ")";
    mLogger.info(flashingTo);
    notifyUpdateStatus(flashingTo);

    // if len(image) > ((t_data.flash_pages - start_page) * t_data.page_size):
    if (image.length > ((t_data.getFlashPages() - startPage) * pageSize)) {
      mLogger.error("Error: Not enough space to flash the image file.");
      // raise Exception()
      return;
    }

    int noOfPages = (image.length / pageSize) + 1;
    mLogger.info(image.length - 1 + " bytes (" + noOfPages + " pages) ");

    // For each page
    int bufferCounter = 0; // Buffer counter
    int i = 0;
    for (i = 0; i < ((image.length - 1) / pageSize) + 1; i++) {
      // Load the buffer
      int end = 0;
      if (((i + 1) * pageSize) > image.length) {
        // buff = image[i * t_data.page_size:]
        end = image.length;
      } else {
        // buff = image[i * t_data.page_size:(i + 1) * t_data.page_size])
        end = (i + 1) * pageSize;
      }
      byte[] buffer = Arrays.copyOfRange(image, i * pageSize, end);

      notifyUpdateProgress(i + 1, noOfPages);

      if (isCancelled()) {
        break;
      }

      this.mCload.uploadBuffer(t_data.getId(), bufferCounter, 0, buffer);

      bufferCounter++;

      // Flash when the complete buffers are full
      if (bufferCounter >= t_data.getBufferPages()) {
        String buffersFull = "Flashing page " + (i + 1) + "...";
        mLogger.info(buffersFull);
        notifyUpdateStatus(buffersFull);
        notifyUpdateProgress(i + 1, noOfPages);
        if (!this.mCload.writeFlash(
            t_data.getId(), 0, startPage + i - (bufferCounter - 1), bufferCounter)) {
          handleFlashError();
          // raise Exception()
          return;
        }
        bufferCounter = 0;
      }
    }
    if (isCancelled()) {
      mLogger.info("Flashing cancelled!");
      return;
    }
    if (bufferCounter > 0) {
      mLogger.info("BufferCounter: " + bufferCounter);
      notifyUpdateProgress(i, noOfPages);
      if (!this.mCload.writeFlash(
          t_data.getId(),
          0,
          (startPage + ((image.length - 1) / pageSize)) - (bufferCounter - 1),
          bufferCounter)) {
        handleFlashError();
        // raise Exception()
        return;
      }
    }
    mLogger.info("Flashing done!");
    notifyUpdateStatus("Flashing done!");
  }