/** Añadir Bytes. MODO NO FIABLE. */
  void addBytes(byte[] aBytes, int iBytes) throws IOException {

    if (this.file == null) return;

    if (lBytesLeidos < lFileSize) {
      // Ajustar tamaño...
      lBytesLeidos += iBytes;
      this.jDialogRecepcion.setBytesRecibidos(lBytesLeidos);
      try {
        this.fileOutputStream.write(aBytes, 0, iBytes);
      } catch (IOException e) {
        mensajeErrorEscribiendo(e.getMessage());
        throw e;
      }
    } else {
      if (this.jDialogRecepcion != null) this.jDialogRecepcion.setVisible(false);

      this.resumenRecepcion();

      // Cerrar  Flujos
      if (this.fileOutputStream != null) this.fileOutputStream.close();

      // Eliminar de ProtocolcFTP
      this.protocolcFTP.removeFileRecepcion(this.id_socket);
    }
  }
  /** Método Run() */
  public void run() {
    // Log.log("\n\nFILERECEPCION","");
    try {
      // RECIBIR UN FICHERO
      this.receiveFile();

      if (this.id_socketIn != null) this.protocolcFTP.removeFileRecepcion(this.id_socketIn);

    } catch (ClusterNetInvalidParameterException e) {
      protocolcFTP.getFTP().error(e.getMessage());
    } catch (ClusterNetExcepcion e) {
      protocolcFTP.getFTP().error(e.getMessage());
    } catch (IOException e) {
      protocolcFTP.getFTP().error(e.getMessage());
    } finally {

      Log.log("*.- FIN FileRecepcion FIABLE.", "");
    }
  }
  /** Recibir UN FICHERO */
  private void receiveFile() throws IOException {
    this.file = null;

    // Log.log("receiveFile()","");

    // ---------------------------------
    // Recibir ID...
    if (!receiveIDFTPMulticast()) {
      tirarBytes();
      return;
    }

    // Recibir FileSize...
    lFileSize = this.receiveFileSize();
    if (lFileSize <= 0) {
      tirarBytes();
      return;
    }

    // Recibir FileName...
    sFileName = this.receiveFileName();
    if (sFileName == null) {
      tirarBytes();
      return;
    }

    // Fichero temporal..
    this.file = File.createTempFile("org.clusternet.ftp" + System.currentTimeMillis(), ".tmp");

    // Comprobar si ya existe.
    if (this.file.exists()) {
      if (!this.file.delete())
        throw new IOException(
            "The file could not deleted "
                + sFileName
                + newline
                + "1. Check you hace privilegies."
                + newline
                + "2.Check the file is not used by another application.");
    }

    // Crear el fichero...
    if (!this.file.createNewFile()) {
      // mensajeErrorEscritura();
      throw new IOException(
          "The file could not be closed "
              + sFileName
              + newline
              + "1. Check you hace privilegies."
              + newline
              + "2.Check the file is not used by another application.");
    }

    // Comprobar si se puede escribir.
    if (!this.file.canWrite()) {
      // mensajeErrorEscritura();
      throw new IOException(
          "The file could not be writed  "
              + sFileName
              + newline
              + "1. Check you hace privilegies."
              + newline
              + "2.Check the file is not used by another application.");
    }

    // Flujo de salida al fichero...
    this.fileOutputStream = new FileOutputStream(file);

    // Iniciar JDialogRecepcion
    this.jDialogRecepcion =
        new JDialogRecepcion(
            this,
            null,
            "Receiving " + this.id_socketIn.getID_Socket(),
            false,
            sFileName,
            lFileSize,
            null,
            this.id_socketIn.getID_Socket());
    this.jDialogRecepcion.show();

    try {
      if (protocolcFTP.getModo() == ClusterNet.MODE_DELAYED_RELIABLE
          || protocolcFTP.getModo() == ClusterNet.MODE_RELIABLE) {
        cFtp.getFTP().insertRecepcionString("Initializing reception of " + sFileName, null);

        lBytesLeidos = 0;
        byte[] bytes = new byte[1024 * 2];

        while (protocolcFTP.esActiva() /* && (lBytesLeidos < lFileSize) */ && (this.file != null)) {

          // if(this.id_socketIn.available() > 0)
          // {
          // Log.log("Bytes disponibles: "+bytes.length,"");
          int iBytesLeidos = this.id_socketIn.read(bytes);

          // FIN DE FLUJO???...
          if (iBytesLeidos == -1) {
            Log.log("FILERECPECION -> RECEIVEFILE : FIN DE FLUJO*", "");
            break;
          }

          // Ajustar tamaño...
          lBytesLeidos += iBytesLeidos;
          this.jDialogRecepcion.setBytesRecibidos(lBytesLeidos);
          try {
            this.fileOutputStream.write(bytes, 0, iBytesLeidos);
          } catch (IOException e) {
            mensajeErrorEscribiendo(e.getMessage());
            throw e;
          }

          // }
          // else
          //  ClusterTimer.sleep(10);
        }

        // Mostrar resumen de recepción...
        this.resumenRecepcion();

      } else {
        // NUNCA DEBE DE ENTRAR AQUI.
      }
    } finally {

      // Eliminar este objeto del treemap en la clase ProtocolcFTP
      if (this.protocolcFTP.getModo() == ClusterNet.MODE_DELAYED_RELIABLE
          || protocolcFTP.getModo() == ClusterNet.MODE_RELIABLE) {
        this.protocolcFTP.removeFileRecepcion(this.id_socketIn);
      } else {
        this.protocolcFTP.removeFileRecepcion(this.id_socket);
      }

      // desactivar el diálogo de recepción...
      if (this.jDialogRecepcion != null) this.jDialogRecepcion.setVisible(false);

      // Cerrar  Flujos...
      this.id_socketIn.close();
      this.id_socketIn = null;

      if (this.fileOutputStream != null) this.fileOutputStream.close();

      if (protocolcFTP.esActiva() && this.file != null) {
        // Cambiar Localización y Nombre del fichero...
        File MFTPfile = new File(sFileName);
        if (MFTPfile.exists()) {
          if (this.mensajeFileExists()) {
            if (!MFTPfile.delete()) {
              this.protocolcFTP
                  .getFTP()
                  .error(
                      "The file can't be deleted: "
                          + sFileName
                          + newline
                          + "1. Check you hace privilegies."
                          + newline
                          + "2.Check the file is not used by another application.");
              // Eliminar temporal
              this.file.delete();
              this.file = null;
              return;
            }

            if (!file.renameTo(MFTPfile)) {
              this.protocolcFTP
                  .getFTP()
                  .error(
                      "The file can't be renamed:"
                          + sFileName
                          + newline
                          + "1. Check you hace privilegies."
                          + newline
                          + "2.Check the file is not used by another application.");
              // Eliminar temporal
              this.file.delete();
              this.file = null;
            }
          }
        } else if (!file.renameTo(MFTPfile)) {
          this.protocolcFTP
              .getFTP()
              .error(
                  "The file can't be renamed:"
                      + sFileName
                      + newline
                      + "1. Check you hace privilegies."
                      + newline
                      + "2.Check the file is not used by another application.");
          // Eliminar temporal
          this.file.delete();
          this.file = null;
        }
      }
    }
  }