try to close all streams as soon as possible to prevent leaks (inserting large sites...
[jSite.git] / src / de / todesbaum / util / freenet / fcp2 / Connection.java
index 15757d0..9932d25 100644 (file)
@@ -31,6 +31,7 @@ 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;
@@ -39,7 +40,7 @@ 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 {
 
@@ -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);
@@ -231,6 +229,9 @@ public class Connection {
                        }
                        nodeSocket = null;
                }
+               synchronized (this) {
+                       notify();
+               }
                fireConnectionTerminated();
        }
 
@@ -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();
                }
        }
@@ -264,7 +271,7 @@ public class Connection {
         * listeners about the messages.
         * 
         * @author David Roden <droden@gmail.com>
-        * @version $Id: Connection.java 413 2006-03-29 12:22:31Z bombe $
+        * @version $Id$
         */
        private class NodeReader implements Runnable {