]> git.somenet.org - irc/pjirc-ng.git/blob - src/main/java/irc/dcc/DCCFile.java
Pjirc 2.2.1 as available on the net, reformatted and made it compile.
[irc/pjirc-ng.git] / src / main / java / irc / dcc / DCCFile.java
1 /*****************************************************/\r
2 /*          This java file is a part of the          */\r
3 /*                                                   */\r
4 /*           -  Plouf's Java IRC Client  -           */\r
5 /*                                                   */\r
6 /*   Copyright (C)  2002 - 2005 Philippe Detournay   */\r
7 /*                                                   */\r
8 /*         All contacts : theplouf@yahoo.com         */\r
9 /*                                                   */\r
10 /*  PJIRC is free software; you can redistribute     */\r
11 /*  it and/or modify it under the terms of the GNU   */\r
12 /*  General Public License as published by the       */\r
13 /*  Free Software Foundation; version 2 or later of  */\r
14 /*  the License.                                     */\r
15 /*                                                   */\r
16 /*  PJIRC is distributed in the hope that it will    */\r
17 /*  be useful, but WITHOUT ANY WARRANTY; without     */\r
18 /*  even the implied warranty of MERCHANTABILITY or  */\r
19 /*  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU   */\r
20 /*  General Public License for more details.         */\r
21 /*                                                   */\r
22 /*  You should have received a copy of the GNU       */\r
23 /*  General Public License along with PJIRC; if      */\r
24 /*  not, write to the Free Software Foundation,      */\r
25 /*  Inc., 59 Temple Place, Suite 330, Boston,        */\r
26 /*  MA  02111-1307  USA                              */\r
27 /*                                                   */\r
28 /*****************************************************/\r
29 \r
30 package irc.dcc;\r
31 \r
32 import irc.IRCConfiguration;\r
33 import irc.ListenerGroup;\r
34 import irc.Source;\r
35 import irc.dcc.prv.DCCFileHandler;\r
36 \r
37 import java.io.BufferedInputStream;\r
38 import java.io.BufferedOutputStream;\r
39 import java.io.File;\r
40 import java.io.IOException;\r
41 import java.io.InputStream;\r
42 import java.io.OutputStream;\r
43 \r
44 /**\r
45  * The DCCFile, used for file transferts.\r
46  */\r
47 public class DCCFile extends Source {\r
48         private OutputStream _os;\r
49         private InputStream _is;\r
50         private File _file;\r
51         private ListenerGroup _listeners;\r
52         private boolean _down = false;\r
53         private int _size;\r
54         private int _count;\r
55         private DCCFileHandler _handler;\r
56 \r
57         /**\r
58          * Create a new DCCFile.\r
59          * \r
60          * @param config\r
61          *          the global configuration.\r
62          * @param f\r
63          *          the file to transfert.\r
64          * @param handler\r
65          *          the file handler.\r
66          */\r
67         public DCCFile(IRCConfiguration config, File f, DCCFileHandler handler) {\r
68                 super(config, handler);\r
69                 _listeners = new ListenerGroup();\r
70                 _handler = handler;\r
71                 _count = 0;\r
72                 _file = f;\r
73         }\r
74 \r
75         /**\r
76          * Add a listener.\r
77          * \r
78          * @param lis\r
79          *          listener to add.\r
80          */\r
81         public void addDCCFileListener(DCCFileListener lis) {\r
82                 _listeners.addListener(lis);\r
83         }\r
84 \r
85         /**\r
86          * Remove a listener.\r
87          * \r
88          * @param lis\r
89          *          listener to remove.\r
90          */\r
91         public void removeDCCFileListener(DCCFileListener lis) {\r
92                 _listeners.removeListener(lis);\r
93         }\r
94 \r
95         /**\r
96          * Prepare to send the file.\r
97          */\r
98         public void prepareSend() {\r
99                 try {\r
100                         _size = _ircConfiguration.getSecurityProvider().getFileSize(_file);\r
101                         // _size=(int)_file.length();\r
102                         _is = new BufferedInputStream(_ircConfiguration.getSecurityProvider().getFileInputStream(_file));\r
103                         _down = false;\r
104                 } catch (Exception e) {\r
105                         _ircConfiguration.internalError("prepareSend failure", e, "plouf@pjirc.com");\r
106                 }\r
107         }\r
108 \r
109         /**\r
110          * Read the next bytes while sending.\r
111          * \r
112          * @param buffer\r
113          *          the target buffer, where the bytes will be written.\r
114          * @param offset\r
115          *          the position of the first written byte in the target buffer.\r
116          * @param length\r
117          *          the maximum amount of bytes to read.\r
118          * @return the number of read bytes, or -1 if there is no more bytes to read.\r
119          * @throws IOException\r
120          *           in case of error.\r
121          */\r
122         public int readBytes(byte[] buffer, int offset, int length) throws IOException {\r
123                 int actual = _is.read(buffer, offset, length);\r
124                 if (actual >= 0) {\r
125                         _count += actual;\r
126                         _listeners.sendEventAsync("transmitted", new Integer(_count), this);\r
127                 }\r
128                 return actual;\r
129         }\r
130 \r
131         /**\r
132          * Get the file size.\r
133          * \r
134          * @return the file size, in byte.\r
135          */\r
136         public int getSize() {\r
137                 return _size;\r
138         }\r
139 \r
140         /**\r
141          * Return true if the transfert is an upload transfert.\r
142          * \r
143          * @return true if uplading, false otherwise.\r
144          */\r
145         public boolean isUploading() {\r
146                 return !isDownloading();\r
147         }\r
148 \r
149         /**\r
150          * Return true if the transfert is a download transfert.\r
151          * \r
152          * @return true if downloading, false otherwise.\r
153          */\r
154         public boolean isDownloading() {\r
155                 return _down;\r
156         }\r
157 \r
158         /**\r
159          * Notify this file the sending is terminated.\r
160          */\r
161         public void fileSent() {\r
162                 try {\r
163                         _listeners.sendEventAsync("finished", this);\r
164                         _is.close();\r
165                 } catch (Exception e) {\r
166                         _ircConfiguration.internalError("fileSent failure", e, "plouf@pjirc.com");\r
167                 }\r
168         }\r
169 \r
170         /**\r
171          * Notify this file the sending has failed.\r
172          */\r
173         public void fileSentFailed() {\r
174                 try {\r
175                         _listeners.sendEventAsync("failed", this);\r
176                         _is.close();\r
177                 } catch (Exception e) {\r
178                         _ircConfiguration.internalError("fileSentFailed failure", e, "plouf@pjirc.com");\r
179                 }\r
180         }\r
181 \r
182         /**\r
183          * Prepare to receive file.\r
184          * \r
185          * @param size\r
186          *          the file size.\r
187          */\r
188         public void prepareReceive(int size) {\r
189                 _down = true;\r
190                 _size = size;\r
191                 try {\r
192                         _os = new BufferedOutputStream(_ircConfiguration.getSecurityProvider().getFileOutputStream(_file));\r
193                 } catch (Exception e) {\r
194                         _os = null;\r
195                 }\r
196         }\r
197 \r
198         /**\r
199          * Write new bytes in the destination file.\r
200          * \r
201          * @param buffer\r
202          *          the buffer to write.\r
203          * @param offset\r
204          *          the first byte of the buffer to write.\r
205          * @param length\r
206          *          the number of bytes to write.\r
207          * @throws IOException\r
208          *           in case of error.\r
209          */\r
210         public void bytesReceived(byte[] buffer, int offset, int length) throws IOException {\r
211                 _count += length;\r
212                 _os.write(buffer, offset, length);\r
213                 _listeners.sendEventAsync("transmitted", new Integer(_count), this);\r
214         }\r
215 \r
216         /**\r
217          * Notify this dcc file the file reception is terminated.\r
218          */\r
219         public void fileReceived() {\r
220                 try {\r
221                         _listeners.sendEventAsync("finished", this);\r
222                         _os.close();\r
223                 } catch (Exception e) {\r
224                         _ircConfiguration.internalError("fileReceived failure", e, "plouf@pjirc.com");\r
225                 }\r
226 \r
227         }\r
228 \r
229         /**\r
230          * Notify this dcc file the file reception has failed.\r
231          */\r
232         public void fileReceiveFailed() {\r
233                 try {\r
234                         _listeners.sendEventAsync("failed", this);\r
235                         _os.close();\r
236                 } catch (Exception e) {\r
237                         _ircConfiguration.internalError("fileReceiveFailed failure", e, "plouf@pjirc.com");\r
238                 }\r
239         }\r
240 \r
241         @Override\r
242         public String getName() {\r
243                 return _file.getName();\r
244         }\r
245 \r
246         @Override\r
247         public void leave() {\r
248                 _handler.close();\r
249                 _handler.leave();\r
250         }\r
251 \r
252         @Override\r
253         public boolean talkable() {\r
254                 return false;\r
255         }\r
256 \r
257         @Override\r
258         public String getType() {\r
259                 return "DCCFile";\r
260         }\r
261 }\r