[3030] | 1 | /* |
---|
| 2 | * Jeti, a Java Jabber client, Copyright (C) 2004 E.S. de Boer |
---|
| 3 | * |
---|
| 4 | * This program is free software; you can redistribute it and/or modify |
---|
| 5 | * it under the terms of the GNU General Public License as published by |
---|
| 6 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | * (at your option) any later version. |
---|
| 8 | * |
---|
| 9 | * This program is distributed in the hope that it will be useful, |
---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | * GNU General Public License for more details. |
---|
| 13 | * |
---|
| 14 | * You should have received a copy of the GNU General Public License |
---|
| 15 | * along with this program; if not, write to the Free Software |
---|
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 17 | * |
---|
| 18 | * For questions, comments etc, |
---|
| 19 | * use the website at http://jeti.jabberstudio.org |
---|
| 20 | * or mail me at eric@jeti.tk |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | package nu.fw.jeti.plugins.filetransfer.ibb; |
---|
| 24 | |
---|
| 25 | import java.io.BufferedOutputStream; |
---|
| 26 | import java.io.IOException; |
---|
| 27 | import java.util.LinkedList; |
---|
| 28 | |
---|
| 29 | import nu.fw.jeti.jabber.Backend; |
---|
| 30 | import nu.fw.jeti.jabber.JID; |
---|
| 31 | import nu.fw.jeti.jabber.elements.InfoQuery; |
---|
| 32 | import nu.fw.jeti.plugins.filetransfer.GetFileWindow; |
---|
| 33 | import nu.fw.jeti.plugins.filetransfer.StreamReceive; |
---|
| 34 | import nu.fw.jeti.util.Base64; |
---|
| 35 | import nu.fw.jeti.util.Popups; |
---|
| 36 | |
---|
| 37 | //24-okt-2004 |
---|
| 38 | public class IBBReceive extends Thread implements StreamReceive |
---|
| 39 | { |
---|
| 40 | private String sid; |
---|
| 41 | private JID jid; |
---|
| 42 | private Backend backend; |
---|
| 43 | private long bytes; |
---|
| 44 | private LinkedList queue = new LinkedList(); |
---|
| 45 | private volatile boolean isDownloading=true; |
---|
| 46 | private GetFileWindow getFileWindow; |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | public IBBReceive(JID jid,String sid,Backend backend,GetFileWindow window) |
---|
| 50 | { |
---|
| 51 | this.jid = jid; |
---|
| 52 | this.sid=sid; |
---|
| 53 | this.backend = backend; |
---|
| 54 | getFileWindow =window; |
---|
| 55 | setPriority(NORM_PRIORITY); |
---|
| 56 | start(); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | public void addData(String data) |
---|
| 60 | { |
---|
| 61 | synchronized(queue) |
---|
| 62 | { |
---|
| 63 | queue.addLast(data); |
---|
| 64 | queue.notifyAll(); |
---|
| 65 | } |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | public void stopDownloading() |
---|
| 69 | { |
---|
| 70 | isDownloading = false; |
---|
| 71 | synchronized(queue){queue.notifyAll();} |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | public long getBytes() |
---|
| 75 | { |
---|
| 76 | return bytes; |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | public void cancel() |
---|
| 80 | { |
---|
| 81 | backend.send(new InfoQuery(jid,"set",new IBBExtension(sid))); |
---|
| 82 | stopDownloading(); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | public void run() |
---|
| 86 | { |
---|
| 87 | |
---|
| 88 | BufferedOutputStream out= new BufferedOutputStream(getFileWindow.getOutputStream()); |
---|
| 89 | // try{ |
---|
| 90 | // out = new BufferedOutputStream (new FileOutputStream(file)); |
---|
| 91 | // }catch(FileNotFoundException e2) |
---|
| 92 | // { |
---|
| 93 | // Popups.errorPopup(file.getAbsolutePath() + " could not be openend in write mode","File transfer"); |
---|
| 94 | // getFileWindow.stopDownloading(); |
---|
| 95 | // } |
---|
| 96 | if(out!=null) |
---|
| 97 | { |
---|
| 98 | try{ |
---|
| 99 | while(!queue.isEmpty() || isDownloading) |
---|
| 100 | { |
---|
| 101 | String base64Data; |
---|
| 102 | synchronized(queue) |
---|
| 103 | { |
---|
| 104 | if (queue.isEmpty()) |
---|
| 105 | { |
---|
| 106 | try |
---|
| 107 | { |
---|
| 108 | //System.out.println("waiting"); |
---|
| 109 | queue.wait(); |
---|
| 110 | } |
---|
| 111 | catch(InterruptedException e) |
---|
| 112 | {//bug when thrown? called when interrupted |
---|
| 113 | e.printStackTrace(); |
---|
| 114 | return; |
---|
| 115 | } |
---|
| 116 | continue; |
---|
| 117 | } |
---|
| 118 | base64Data = (String)queue.removeFirst(); |
---|
| 119 | //System.out.println("data read"); |
---|
| 120 | } |
---|
| 121 | //System.out.println(base64Data); |
---|
| 122 | //System.out.println(Base64.decode2(base64Data)); |
---|
| 123 | |
---|
| 124 | byte[] data = Base64.decode(base64Data); |
---|
| 125 | |
---|
| 126 | |
---|
| 127 | //System.out.println("data converted"); |
---|
| 128 | out.write(data, 0, data.length); |
---|
| 129 | //System.out.println("data written"); |
---|
| 130 | bytes+=data.length; |
---|
| 131 | //progressMonitor.setProgress(bytes); |
---|
| 132 | //bytes++; |
---|
| 133 | //if (Thread.interrupted()) throw new InterruptedException(); |
---|
| 134 | //yield(); |
---|
| 135 | } |
---|
| 136 | }catch (IOException e2) //probably out of hd |
---|
| 137 | { |
---|
| 138 | Popups.errorPopup(e2.getMessage() + " while downloading " ,"File transfer"); |
---|
| 139 | //download not ok |
---|
| 140 | backend.send(new InfoQuery(jid,"set",new IBBExtension(sid))); |
---|
| 141 | getFileWindow.stopDownloading(); |
---|
| 142 | } |
---|
| 143 | finally |
---|
| 144 | { |
---|
| 145 | try |
---|
| 146 | { |
---|
| 147 | out.close(); |
---|
| 148 | } catch (IOException e) |
---|
| 149 | { |
---|
| 150 | e.printStackTrace(); |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | getFileWindow.stopDownloading(); |
---|
| 155 | //System.out.println("downloaded"); |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | } |
---|
| 159 | /* |
---|
| 160 | * Overrides for emacs |
---|
| 161 | * Local variables: |
---|
| 162 | * tab-width: 4 |
---|
| 163 | * End: |
---|
| 164 | */ |
---|