001/** 002 * Copyright (C) 2014 Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/ 003 * 004 * This file is part of Dicoogle/dicoogle. 005 * 006 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * Dicoogle/dicoogle is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with Dicoogle. If not, see <http://www.gnu.org/licenses/>. 018 */ 019 020package pt.ua.dicoogle.rGUI.fileTransfer; 021 022import java.io.FileOutputStream; 023import java.io.IOException; 024import java.io.InputStream; 025import java.net.InetAddress; 026import java.net.Socket; 027import org.slf4j.Logger; 028import org.slf4j.LoggerFactory; 029import org.slf4j.Logger; 030import org.slf4j.LoggerFactory; 031import javax.net.SocketFactory; 032import javax.net.ssl.SSLSocketFactory; 033import pt.ua.dicoogle.core.ClientSettings; 034import pt.ua.dicoogle.rGUI.RFileBrowser.RemoteFile; 035 036/** 037 * 038 * @author Samuel Campos <samuelcampos@ua.pt> 039 */ 040@Deprecated 041public class FileReceiver extends Thread { 042 private RemoteFile file; 043 private InetAddress serverAddr; 044 private int serverPort; 045 private TransferStatus ts; 046 047 private Socket socket; 048 049 private String filePath; 050 051 public FileReceiver(RemoteFile file, InetAddress serverAddr, int serverPort, TransferStatus ts) throws IOException{ 052 SocketFactory socketFactory = SSLSocketFactory.getDefault(); 053 socket = socketFactory.createSocket(serverAddr, serverPort); 054 055 this.file = file; 056 this.serverAddr = serverAddr; 057 this.serverPort = serverPort; 058 this.ts = ts; 059 060 String dirPath = ClientSettings.getInstance().getTempFilesDir(); 061 062 //if the temporary folder is not defined 063 if (dirPath == null || dirPath.equals("")) 064 dirPath = "."; 065 066 filePath = dirPath + "/" + file.getName(); 067 ts.setFilePath(filePath); 068 } 069 070 @Override 071 public void run(){ 072 //DebugManager.getInstance().debug("Starting transfer Thread..."); 073 074 long sizeTransfered = receiver(); 075 076 if(sizeTransfered != -1) 077 //DebugManager.getInstance().debug("Transfer complete! File: " + file.getName()); 078 079 return; 080 } 081 082 public String getFilePath(){ 083 return filePath; 084 } 085 086 private long receiver(){ 087 long transferedBytes = 0; 088 089 try{ 090 // Buffer size = 1 KB 091 byte data[] = new byte[1024]; 092 093 InputStream in = socket.getInputStream(); 094 FileOutputStream out = new FileOutputStream(filePath); 095 096 int size; 097 // write in file 098 while ((size = in.read(data)) != -1){ 099 transferedBytes += size; 100 101 out.write(data, 0, size); 102 out.flush(); 103 104 // refresh the progress bar 105 if(ts != null) 106 ts.setTransferedBytes(transferedBytes); 107 } 108 109 // Freeing resources 110 out.close(); 111 in.close(); 112 socket.close(); 113 114 } catch (IOException ex) { 115 ts.errorInTransfer("There was an error downloading the file!"); 116 117 try { 118 if (!socket.isClosed()) 119 socket.close(); 120 } catch (IOException e) { 121 LoggerFactory.getLogger(FileReceiver.class).error(ex.getMessage(), e); 122 } 123 124 //LoggerFactory.getLogger(FileSender.class).error(ex.getMessage(), ex); 125 return -1; 126 } 127 128 return transferedBytes; 129 } 130}