🚧 Parse token from DCC SEND
[xudocci.git] / src / main / java / net / pterodactylus / irc / DccReceiver.java
index 9ddca13..e024d4e 100644 (file)
@@ -23,8 +23,6 @@ import java.io.OutputStream;
 import java.net.InetAddress;
 import java.net.Socket;
 import java.util.concurrent.TimeUnit;
-import java.util.logging.Level;
-import java.util.logging.Logger;
 
 import net.pterodactylus.irc.event.DccDownloadFailed;
 import net.pterodactylus.irc.event.DccDownloadFinished;
@@ -34,6 +32,7 @@ import net.pterodactylus.xdcc.util.io.BandwidthCountingInputStream;
 import com.google.common.eventbus.EventBus;
 import com.google.common.io.Closeables;
 import com.google.common.util.concurrent.AbstractExecutionThreadService;
+import org.apache.log4j.Logger;
 
 /**
  * Service that receives a file offered by a {@link DccSendReceived}.
@@ -155,7 +154,7 @@ public class DccReceiver extends AbstractExecutionThreadService {
         * @return The current rate of the download, in bytes/second
         */
        public long currentRate() {
-               return inputStream.getCurrentRate();
+               return (inputStream != null) ? inputStream.getCurrentRate() : 0;
        }
 
        /**
@@ -164,7 +163,7 @@ public class DccReceiver extends AbstractExecutionThreadService {
         * @return The overall rate of the download, in bytes/second
         */
        public long overallRate() {
-               return inputStream.getOverallRate();
+               return (inputStream != null) ? inputStream.getOverallRate() : 0;
        }
 
        //
@@ -176,6 +175,7 @@ public class DccReceiver extends AbstractExecutionThreadService {
                Socket socket = null;
                try {
                        socket = new Socket(inetAddress, port);
+                       socket.setSoTimeout((int) TimeUnit.MINUTES.toMillis(3));
                        InputStream socketInputStream = socket.getInputStream();
                        inputStream = new BandwidthCountingInputStream(socketInputStream, 5, TimeUnit.SECONDS);
                        byte[] buffer = new byte[65536];
@@ -189,9 +189,13 @@ public class DccReceiver extends AbstractExecutionThreadService {
                                progress += r;
                        }
                        outputStream.flush();
-                       eventBus.post(new DccDownloadFinished(this));
+                       if ((size == -1) || (progress == size)) {
+                               eventBus.post(new DccDownloadFinished(this));
+                       } else {
+                               eventBus.post(new DccDownloadFailed(this, new IOException("Download aborted.")));
+                       }
                } catch (IOException ioe1) {
-                       logger.log(Level.WARNING, "I/O error while receiving DCC!", ioe1);
+                       logger.warn("I/O error while receiving DCC!", ioe1);
                        eventBus.post(new DccDownloadFailed(this, ioe1));
                } finally {
                        Closeables.close(inputStream, true);