Add @SuppressWarnings annotation.
[jSite.git] / src / de / todesbaum / util / freenet / fcp2 / Connection.java
index 9c6b74d..82b4a6b 100644 (file)
@@ -34,6 +34,7 @@ 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.StreamCopier.ProgressListener;
 import de.todesbaum.util.io.TempFileInputStream;
 
 /**
@@ -71,6 +72,9 @@ public class Connection {
        /** The NodeHello message sent by the node on connect. */
        protected Message nodeHello;
 
+       /** The temp directory to use. */
+       private String tempDirectory;
+
        /**
         * Creates a new connection to the specified node with the specified name.
         *
@@ -113,7 +117,7 @@ public class Connection {
         *            The received message
         */
        protected void fireMessageReceived(Message message) {
-               for (ConnectionListener connectionListener: connectionListeners) {
+               for (ConnectionListener connectionListener : connectionListeners) {
                        connectionListener.messageReceived(this, message);
                }
        }
@@ -122,7 +126,7 @@ public class Connection {
         * Notifies listeners about the loss of the connection.
         */
        protected void fireConnectionTerminated() {
-               for (ConnectionListener connectionListener: connectionListeners) {
+               for (ConnectionListener connectionListener : connectionListeners) {
                        connectionListener.connectionTerminated(this);
                }
        }
@@ -137,6 +141,17 @@ public class Connection {
        }
 
        /**
+        * Sets the temp directory to use for creation of temporary files.
+        *
+        * @param tempDirectory
+        *            The temp directory to use, or {@code null} to use the default
+        *            temp directory
+        */
+       public void setTempDirectory(String tempDirectory) {
+               this.tempDirectory = tempDirectory;
+       }
+
+       /**
         * Connects to the node.
         *
         * @return <code>true</code> if the connection succeeded and the node
@@ -246,6 +261,22 @@ public class Connection {
         *             if an I/O error occurs
         */
        public synchronized void execute(Command command) throws IllegalStateException, IOException {
+               execute(command, null);
+       }
+
+       /**
+        * Executes the specified command.
+        *
+        * @param command
+        *            The command to execute
+        * @param progressListener
+        *            A progress listener for a payload transfer
+        * @throws IllegalStateException
+        *             if the connection is not connected
+        * @throws IOException
+        *             if an I/O error occurs
+        */
+       public synchronized void execute(Command command, ProgressListener progressListener) throws IllegalStateException, IOException {
                if (nodeSocket == null) {
                        throw new IllegalStateException("connection is not connected");
                }
@@ -257,7 +288,7 @@ public class Connection {
                        InputStream payloadInputStream = null;
                        try {
                                payloadInputStream = command.getPayload();
-                               StreamCopier.copy(payloadInputStream, nodeOutputStream, command.getPayloadLength());
+                               StreamCopier.copy(payloadInputStream, nodeOutputStream, command.getPayloadLength(), progressListener);
                        } finally {
                                Closer.close(payloadInputStream);
                        }
@@ -293,6 +324,7 @@ public class Connection {
                 * Main loop of the reader. Lines are read and converted into
                 * {@link Message} objects.
                 */
+               @SuppressWarnings("synthetic-access")
                public void run() {
                        LineInputStream nodeReader = null;
                        try {
@@ -313,7 +345,7 @@ public class Connection {
                                                /* need to read message from stream now */
                                                File tempFile = null;
                                                try {
-                                                       tempFile = File.createTempFile("fcpv2", "data");
+                                                       tempFile = File.createTempFile("fcpv2", "data", (tempDirectory != null) ? new File(tempDirectory) : null);
                                                        tempFile.deleteOnExit();
                                                        FileOutputStream tempFileOutputStream = new FileOutputStream(tempFile);
                                                        long dataLength = Long.parseLong(message.get("DataLength"));