whitespace fixed
[jSite.git] / src / de / todesbaum / util / freenet / fcp2 / Connection.java
index 15757d0..9c6b74d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * todesbaum-lib - 
+ * todesbaum-lib -
  * Copyright (C) 2006 David Roden
  *
  * This program is free software; you can redistribute it and/or modify
@@ -31,15 +31,16 @@ import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.List;
 
+import de.todesbaum.util.io.Closer;
 import de.todesbaum.util.io.LineInputStream;
 import de.todesbaum.util.io.StreamCopier;
 import de.todesbaum.util.io.TempFileInputStream;
 
 /**
  * A physical connection to a Freenet node.
- * 
+ *
  * @author David Roden <droden@gmail.com>
- * @version $Id: Connection.java 413 2006-03-29 12:22:31Z bombe $
+ * @version $Id$
  */
 public class Connection {
 
@@ -72,7 +73,7 @@ public class Connection {
 
        /**
         * Creates a new connection to the specified node with the specified name.
-        * 
+        *
         * @param node
         *            The node to connect to
         * @param name
@@ -85,7 +86,7 @@ public class Connection {
 
        /**
         * Adds a listener that gets notified on connection events.
-        * 
+        *
         * @param connectionListener
         *            The listener to add
         */
@@ -96,7 +97,7 @@ public class Connection {
        /**
         * Removes a listener from the list of registered listeners. Only the first
         * matching listener is removed.
-        * 
+        *
         * @param connectionListener
         *            The listener to remove
         * @see List#remove(java.lang.Object)
@@ -107,7 +108,7 @@ public class Connection {
 
        /**
         * Notifies listeners about a received message.
-        * 
+        *
         * @param message
         *            The received message
         */
@@ -128,7 +129,7 @@ public class Connection {
 
        /**
         * Returns the name of the connection.
-        * 
+        *
         * @return The name of the connection
         */
        public String getName() {
@@ -137,7 +138,7 @@ public class Connection {
 
        /**
         * Connects to the node.
-        * 
+        *
         * @return <code>true</code> if the connection succeeded and the node
         *         returned a NodeHello message
         * @throws IOException
@@ -155,9 +156,6 @@ public class Connection {
                        nodeSocket.setReceiveBufferSize(65535);
                        nodeInputStream = nodeSocket.getInputStream();
                        nodeOutputStream = nodeSocket.getOutputStream();
-                       // nodeWriter = new TeeWriter(new
-                       // OutputStreamWriter(nodeOutputStream, Charset.forName("UTF-8")),
-                       // new PrintWriter(System.out));
                        nodeWriter = new OutputStreamWriter(nodeOutputStream, Charset.forName("UTF-8"));
                        nodeReader = new NodeReader(nodeInputStream);
                        Thread nodeReaderThread = new Thread(nodeReader);
@@ -182,7 +180,7 @@ public class Connection {
 
        /**
         * Returns whether this connection is still connected to the node.
-        * 
+        *
         * @return <code>true</code> if this connection is still valid,
         *         <code>false</code> otherwise
         */
@@ -192,7 +190,7 @@ public class Connection {
 
        /**
         * Returns the NodeHello message the node sent on connection.
-        * 
+        *
         * @return The NodeHello message of the node
         */
        public Message getNodeHello() {
@@ -231,12 +229,15 @@ public class Connection {
                        }
                        nodeSocket = null;
                }
+               synchronized (this) {
+                       notify();
+               }
                fireConnectionTerminated();
        }
 
        /**
         * Executes the specified command.
-        * 
+        *
         * @param command
         *            The command to execute
         * @throws IllegalStateException
@@ -253,7 +254,13 @@ public class Connection {
                nodeWriter.write("EndMessage" + Command.LINEFEED);
                nodeWriter.flush();
                if (command.hasPayload()) {
-                       StreamCopier.copy(command.getPayload(), nodeOutputStream, command.getPayloadLength());
+                       InputStream payloadInputStream = null;
+                       try {
+                               payloadInputStream = command.getPayload();
+                               StreamCopier.copy(payloadInputStream, nodeOutputStream, command.getPayloadLength());
+                       } finally {
+                               Closer.close(payloadInputStream);
+                       }
                        nodeOutputStream.flush();
                }
        }
@@ -262,9 +269,9 @@ public class Connection {
         * The reader thread for this connection. This is essentially a thread that
         * reads lines from the node, creates messages from them and notifies
         * listeners about the messages.
-        * 
+        *
         * @author David Roden &lt;droden@gmail.com&gt;
-        * @version $Id: Connection.java 413 2006-03-29 12:22:31Z bombe $
+        * @version $Id$
         */
        private class NodeReader implements Runnable {
 
@@ -274,7 +281,7 @@ public class Connection {
 
                /**
                 * Creates a new reader that reads from the specified input stream.
-                * 
+                *
                 * @param nodeInputStream
                 *            The input stream to read from
                 */