1 /*****************************************************/
\r
2 /* This java file is a part of the */
\r
4 /* - Plouf's Java IRC Client - */
\r
6 /* Copyright (C) 2002 - 2005 Philippe Detournay */
\r
8 /* All contacts : theplouf@yahoo.com */
\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
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
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
28 /*****************************************************/
\r
32 import irc.IRCConfiguration;
\r
33 import irc.ListenerGroup;
\r
35 import irc.dcc.prv.DCCFileHandler;
\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
45 * The DCCFile, used for file transferts.
\r
47 public class DCCFile extends Source {
\r
48 private OutputStream _os;
\r
49 private InputStream _is;
\r
51 private ListenerGroup _listeners;
\r
52 private boolean _down = false;
\r
55 private DCCFileHandler _handler;
\r
58 * Create a new DCCFile.
\r
61 * the global configuration.
\r
63 * the file to transfert.
\r
67 public DCCFile(IRCConfiguration config, File f, DCCFileHandler handler) {
\r
68 super(config, handler);
\r
69 _listeners = new ListenerGroup();
\r
81 public void addDCCFileListener(DCCFileListener lis) {
\r
82 _listeners.addListener(lis);
\r
86 * Remove a listener.
\r
89 * listener to remove.
\r
91 public void removeDCCFileListener(DCCFileListener lis) {
\r
92 _listeners.removeListener(lis);
\r
96 * Prepare to send the file.
\r
98 public void prepareSend() {
\r
100 _size = _ircConfiguration.getSecurityProvider().getFileSize(_file);
\r
101 // _size=(int)_file.length();
\r
102 _is = new BufferedInputStream(_ircConfiguration.getSecurityProvider().getFileInputStream(_file));
\r
104 } catch (Exception e) {
\r
105 _ircConfiguration.internalError("prepareSend failure", e, "plouf@pjirc.com");
\r
110 * Read the next bytes while sending.
\r
113 * the target buffer, where the bytes will be written.
\r
115 * the position of the first written byte in the target buffer.
\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
122 public int readBytes(byte[] buffer, int offset, int length) throws IOException {
\r
123 int actual = _is.read(buffer, offset, length);
\r
126 _listeners.sendEventAsync("transmitted", new Integer(_count), this);
\r
132 * Get the file size.
\r
134 * @return the file size, in byte.
\r
136 public int getSize() {
\r
141 * Return true if the transfert is an upload transfert.
\r
143 * @return true if uplading, false otherwise.
\r
145 public boolean isUploading() {
\r
146 return !isDownloading();
\r
150 * Return true if the transfert is a download transfert.
\r
152 * @return true if downloading, false otherwise.
\r
154 public boolean isDownloading() {
\r
159 * Notify this file the sending is terminated.
\r
161 public void fileSent() {
\r
163 _listeners.sendEventAsync("finished", this);
\r
165 } catch (Exception e) {
\r
166 _ircConfiguration.internalError("fileSent failure", e, "plouf@pjirc.com");
\r
171 * Notify this file the sending has failed.
\r
173 public void fileSentFailed() {
\r
175 _listeners.sendEventAsync("failed", this);
\r
177 } catch (Exception e) {
\r
178 _ircConfiguration.internalError("fileSentFailed failure", e, "plouf@pjirc.com");
\r
183 * Prepare to receive file.
\r
188 public void prepareReceive(int size) {
\r
192 _os = new BufferedOutputStream(_ircConfiguration.getSecurityProvider().getFileOutputStream(_file));
\r
193 } catch (Exception e) {
\r
199 * Write new bytes in the destination file.
\r
202 * the buffer to write.
\r
204 * the first byte of the buffer to write.
\r
206 * the number of bytes to write.
\r
207 * @throws IOException
\r
208 * in case of error.
\r
210 public void bytesReceived(byte[] buffer, int offset, int length) throws IOException {
\r
212 _os.write(buffer, offset, length);
\r
213 _listeners.sendEventAsync("transmitted", new Integer(_count), this);
\r
217 * Notify this dcc file the file reception is terminated.
\r
219 public void fileReceived() {
\r
221 _listeners.sendEventAsync("finished", this);
\r
223 } catch (Exception e) {
\r
224 _ircConfiguration.internalError("fileReceived failure", e, "plouf@pjirc.com");
\r
230 * Notify this dcc file the file reception has failed.
\r
232 public void fileReceiveFailed() {
\r
234 _listeners.sendEventAsync("failed", this);
\r
236 } catch (Exception e) {
\r
237 _ircConfiguration.internalError("fileReceiveFailed failure", e, "plouf@pjirc.com");
\r
242 public String getName() {
\r
243 return _file.getName();
\r
247 public void leave() {
\r
253 public boolean talkable() {
\r
258 public String getType() {
\r