1 package zirc.threads ; 2 3 import java.io.* ; 4 import java.net.* ; 5 import java.util.* ; 6 import java.util.Timer ; 7 import java.awt.* ; 8 import javax.swing.* ; 9 import zirc.base.* ; 10 import zirc.gui.* ; 11 12 25 32 33 public class DCCincomingThread extends Thread 34 { 35 private Socket s ; 36 RandomAccessFile file ; 37 String sender ; 38 int amount ; 39 int offset; 40 int port; 41 JLabel labelInfo ; 42 int transferred ; 43 int speed = 0 ; 44 DataInputStream in ; 45 String filename ; 46 int filesize ; 47 MainFrame frm ; 48 IRCconnexion IRCchan ; 49 private JFileChooser jFileChooser ; 50 51 public DCCincomingThread(String _ip, int _port, String _filename, int _filesize, MainFrame _frm, IRCconnexion _IRCchan, String _sender, 52 int _offset) 53 { 54 filename = _filename ; 55 filesize = _filesize ; 56 port=_port; 57 long t = Long.parseLong(_ip) ; 58 frm = _frm ; 59 IRCchan = _IRCchan ; 60 sender = _sender ; 61 offset=_offset; 62 transferred = _offset ; 63 String ip = toInetAddress(t).toString().replaceAll("/", "") ; 64 try 65 { 66 s = new Socket(ip, port) ; 67 in = new DataInputStream(s.getInputStream()) ; 68 this.start() ; 69 } 70 catch (UnknownHostException ex) 71 {ex.printStackTrace() ; 72 } 73 catch (IOException ex) 74 {ex.printStackTrace() ; 75 } 76 } 77 78 private InetAddress toInetAddress(long ipAddress) 79 { 80 InetAddress inetAddress = null ; 81 try 82 { 83 long upperBytes = 0 ; 84 long longByte ; 85 byte[] bytes = new byte[4] ; 86 for (int i = 3 ; i >= 0 ; i--) 87 { 88 longByte = (new Double (Math.floor((ipAddress - Math.abs(upperBytes)) >> (8 * i)))).longValue() ; 89 bytes[4 - i - 1] = (byte)longByte ; 90 upperBytes += longByte << (8 * i) ; 91 } 92 inetAddress = InetAddress.getByAddress(bytes) ; 93 94 } 95 catch (UnknownHostException e) 96 { 97 e.printStackTrace() ; 98 } 99 return inetAddress ; 100 } 101 102 public void run() 105 { 106 try 107 { 108 file = null ; 109 DCCtransfer dccfrm = new DCCtransfer(frm, IRCchan, "DCC " + filename, this, sender, filename,transferred,port ) ; 110 byte b[] = new byte[1024] ; 111 Timer speedTimer = new Timer () ; 112 Speed speedTask = new Speed() ; 113 jFileChooser = new JFileChooser("/"); 114 jFileChooser.setSelectedFile(new File(filename)) ; 115 if (jFileChooser.showSaveDialog(frm) == JFileChooser.APPROVE_OPTION) 116 { 117 String t = jFileChooser.getSelectedFile().getAbsolutePath() ; 118 file = new RandomAccessFile(t, "rw") ; 120 } 121 else 122 { 123 s.close() ; 124 } 125 126 dccfrm.getProgressBar().setMaximum(filesize) ; 127 128 frm.getMdiPanel().add(dccfrm) ; 129 dccfrm.setVisible(true) ; 130 labelInfo = dccfrm.getInfoLabel() ; 131 132 speedTimer.schedule(speedTask, 1000, 100) ; 134 135 while (transferred < filesize) 137 { 138 amount = in.read(b) ; 139 file.write(b,offset,amount); 140 transferred += amount ; 141 dccfrm.getProgressBar().setValue(transferred) ; 142 } 143 144 speedTimer.cancel() ; 145 speedTimer.cancel() ; 146 if (transferred >= filesize) 147 { 148 labelInfo.setText(filesize + " / " + filesize + " " + speed + " ko/s") ; 149 } 150 file.close() ; in.close() ; 153 s.close() ; 154 } 155 catch (UnknownHostException ex) 156 { 157 ex.printStackTrace() ; 158 } 159 catch (IOException ex) 160 { 161 ex.printStackTrace() ; 162 } 163 } 164 165 public void abortTransfer() 166 { 167 try 168 { 169 file.close() ; in.close() ; 172 s.close() ; 173 } 174 catch (IOException ex) 175 { 176 } 177 } 178 179 180 class Speed extends TimerTask 182 { 183 long startPoint = System.currentTimeMillis() ; 184 long passedTime = 0 ; 185 public void run() 186 { 187 passedTime = (System.currentTimeMillis() - startPoint) / 1000 ; speed = ((int)(((transferred + 1) / 1000) / passedTime)) ; 189 labelInfo.setText(transferred + " / " + filesize + " " + speed + " ko/s") ; 190 } 191 } 192 193 public DCCincomingThread() 194 { 195 try 196 { 197 jbInit() ; 198 } 199 catch (Exception e) 200 { 201 e.printStackTrace() ; 202 } 203 } 204 205 private void jbInit() throws Exception 206 { 207 jFileChooser.setBorder(BorderFactory.createLineBorder(Color.gray)) ; 208 } 209 } 210 | Popular Tags |