/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package socius.stream; import com.healthmarketscience.rmiio.RemoteInputStreamClient; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.rmi.RemoteException; import socius.adapter.Arquivo; import socius.dispositivos.ComputadorRemoto; import socius.dispositivos.ServidorRemoto; import socius.excecoes.FalhaNaTransferenciaException; import socius.excecoes.SistemaOperacionalNaoSuportadoException; import socius.factory.arquivo.ArquivoFactory; import socius.telas.principal.PrincipalController; import socius.util.ConfigSistema; /** * * @author Lucas Dillmann <lucas [at] dillmann.com.br> */ public class DownloadThread extends Thread { private Arquivo arquivo; private PrincipalController controller; private ComputadorRemoto computador; private ServidorRemoto servidor; public long bytesTransferidos = 1L; public DownloadThread(ServidorRemoto servidor, ComputadorRemoto computador, Arquivo arquivo, PrincipalController controller) { this.arquivo = arquivo; this.controller = controller; this.computador = computador; this.servidor = servidor; } @Override public void run() { String nomeArquivo = ""; InputStream stream = null; try { try { nomeArquivo = arquivo.getFile().getName(); stream = RemoteInputStreamClient.wrap(servidor.iniciarTransferencia(computador, arquivo)); } catch (RemoteException ex) { throw new FalhaNaTransferenciaException(); } catch (IOException ex) { throw new FalhaNaTransferenciaException(); } } catch (FalhaNaTransferenciaException ex) { ex.exibeMensagem(); } String path = System.getProperty("user.home") + System.getProperty("file.separator") + ConfigSistema.getProperty("cliente.pasta_publica") + System.getProperty("file.separator") + nomeArquivo; Arquivo destino = null; OutputStream outStream = null; try { destino = ArquivoFactory.getFactory().getArquivo(path); outStream = new FileOutputStream(destino.getFile()); } catch (SistemaOperacionalNaoSuportadoException | RemoteException | FileNotFoundException ex) { ex.printStackTrace(); } byte[] buffer = new byte[1024]; int len; try { while ((len = stream.read(buffer)) != -1) { outStream.write(buffer, 0, len); outStream.flush(); bytesTransferidos += len; } } catch (IOException ex) { ex.printStackTrace(); } try { this.finalize(); } catch (Throwable ex) { } } }