Example #1
0
  /**
   * Crea el fichero de indices con la informacion inicial necesaria para un archivo concreto.
   *
   * @param fichero es el fichero de indices que tendra toda la informacion.
   * @param archivo es el objeto que representa el archivo en concreto.
   * @param fragmentos es la lista de fragmentos que faltan de dicho archivo.
   */
  public void crearFicheroIndices(File fichero, Archivo archivo, Vector<Fragmento> fragmentos) {
    Vector<Fragmento> fragTengo = new Vector<Fragmento>(), fragFaltan = fragmentos;
    Indices indices = new Indices(archivo, fragTengo, fragFaltan);
    ByteArrayOutputStream bs = new ByteArrayOutputStream();
    try {
      ObjectOutputStream os = new ObjectOutputStream(bs);
      os.writeObject(indices);
      os.close();

    } catch (IOException e) {
      /*System.out.println("Error -> posibles causas: ");
      System.out.println( "\tProblemas al crear un flujo de bytes serializable" );
      System.out.println( "\tProblemas al serializar el objeto indice" );
      System.out.println( "\tProblemas al cerrar el flujo serializable" );*/
      ControlDeErrores.getInstancia()
          .registrarError(
              ErrorEGorilla.ERROR_DISCO,
              "Error -> posibles causas: "
                  + "\tProblemas al crear un flujo de bytes serializable"
                  + "\tProblemas al serializar el objeto indice"
                  + "\tProblemas al cerrar el flujo serializable");
      // e.printStackTrace();
    }

    byte[] bytes = bs.toByteArray(); // devuelve byte[]

    try {
      if (fichero.exists() == true) {
        String nombreNuevoFichero = fichero.getName();
        nombreNuevoFichero += "_" + archivo.getHash();
        fichero = new File(nombreNuevoFichero);
      }
      FileOutputStream ficheroIndices = new FileOutputStream(fichero);
      BufferedOutputStream bufferedOutput = new BufferedOutputStream(ficheroIndices);
      bufferedOutput.write(bytes, 0, bytes.length);
      bufferedOutput.close();
      // creo que tambien hace falta cerrar el otro
      ficheroIndices.close();
    } catch (FileNotFoundException e) {
      System.out.println("No existe el fichero <" + fichero.getName() + ">");
    } catch (IOException e) {
      /*System.out.println("Error -> posibles causas: ");
      System.out.println( "\tProblemas al escribir en el fichero <"+fichero.getName()+">" );
      System.out.println( "\tProblemas al cerrar el flujo o el fichero <"+fichero.getName()+">" );*/
      ControlDeErrores.getInstancia()
          .registrarError(
              ErrorEGorilla.ERROR_DISCO,
              "Error -> posibles causas: "
                  + "\tProblemas al escribir en el fichero <"
                  + fichero.getName()
                  + ">"
                  + "\tProblemas al cerrar el flujo o el fichero <"
                  + fichero.getName()
                  + ">");
      // e.printStackTrace();
    }
  }
Example #2
0
  /**
   * Guarda la nueva informacion sobre los indices de un archivo concreto en su fichero
   * correspondiente.
   *
   * @param fichero es el fichero donde se almacenara el indice.
   * @param indices es el objeto que contiene la informacion sobre lo que tenemos del archivo.
   */
  public void guardarFicheroIndices(File fichero, Indices indices) {
    try {
      ByteArrayOutputStream bs = new ByteArrayOutputStream();
      ObjectOutputStream os = new ObjectOutputStream(bs);
      os.writeObject(indices);
      os.close();
      byte[] bytes = bs.toByteArray(); // devuelve byte[]

      FileOutputStream ficheroIndices = new FileOutputStream(fichero);
      BufferedOutputStream bufferedOutput = new BufferedOutputStream(ficheroIndices);
      // Pese a existir ya el fichero, como voy anadiendo fragmentos, el file aumenta
      // por lo q sobreescribo el fichero con mas bytes y no menos, ya que si fueran menos
      // quedarian bytes malos en el file
      bufferedOutput.write(bytes, 0, bytes.length);
      bufferedOutput.close();
      // creo que tambien hace falta cerrar el otro
      ficheroIndices.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }