From cc6e9d53adf58aaf02feb507940c4f1c38992c71 Mon Sep 17 00:00:00 2001
From: akwizgran <michael@briarproject.org>
Date: Fri, 19 Oct 2012 21:52:53 +0100
Subject: [PATCH] Allow duplex connections' dispose() methods to throw
 IOExceptions.

---
 .../duplex/DuplexTransportConnection.java     |  2 +-
 .../simplex/SimplexTransportReader.java       |  1 -
 .../simplex/SimplexTransportWriter.java       |  1 -
 .../BluetoothTransportConnection.java         | 14 ++-------
 .../socket/SocketTransportConnection.java     | 14 ++-------
 .../plugins/tor/TorTransportConnection.java   | 14 ++-------
 .../protocol/duplex/DuplexConnection.java     |  6 +++-
 .../transport/ConnectionDispatcherImpl.java   | 30 ++++++++++++-------
 test/net/sf/briar/plugins/DuplexTest.java     | 12 ++++++--
 9 files changed, 45 insertions(+), 49 deletions(-)

diff --git a/api/net/sf/briar/api/plugins/duplex/DuplexTransportConnection.java b/api/net/sf/briar/api/plugins/duplex/DuplexTransportConnection.java
index f5c40e1e46..6fc1d3cd57 100644
--- a/api/net/sf/briar/api/plugins/duplex/DuplexTransportConnection.java
+++ b/api/net/sf/briar/api/plugins/duplex/DuplexTransportConnection.java
@@ -28,5 +28,5 @@ public interface DuplexTransportConnection {
 	 * of an exception and the second argument indicates whether the connection
 	 * was recognised, which may affect how resources are disposed of.
 	 */
-	void dispose(boolean exception, boolean recognised);
+	void dispose(boolean exception, boolean recognised) throws IOException;
 }
diff --git a/api/net/sf/briar/api/plugins/simplex/SimplexTransportReader.java b/api/net/sf/briar/api/plugins/simplex/SimplexTransportReader.java
index 5ffc0635c1..b7f0f35ed9 100644
--- a/api/net/sf/briar/api/plugins/simplex/SimplexTransportReader.java
+++ b/api/net/sf/briar/api/plugins/simplex/SimplexTransportReader.java
@@ -18,7 +18,6 @@ public interface SimplexTransportReader {
 	 * argument indicates whether the reader is being closed because of an
 	 * exception and the second argument indicates whether the connection was
 	 * recognised, which may affect how resources are disposed of.
-	 * @throws IOException 
 	 */
 	void dispose(boolean exception, boolean recognised) throws IOException;
 }
diff --git a/api/net/sf/briar/api/plugins/simplex/SimplexTransportWriter.java b/api/net/sf/briar/api/plugins/simplex/SimplexTransportWriter.java
index 09e80a5cfa..c48b2795de 100644
--- a/api/net/sf/briar/api/plugins/simplex/SimplexTransportWriter.java
+++ b/api/net/sf/briar/api/plugins/simplex/SimplexTransportWriter.java
@@ -25,7 +25,6 @@ public interface SimplexTransportWriter {
 	 * Closes the writer and disposes of any associated resources. The
 	 * argument indicates whether the writer is being closed because of an
 	 * exception, which may affect how resources are disposed of.
-	 * @throws IOException 
 	 */
 	void dispose(boolean exception) throws IOException;
 }
diff --git a/components/net/sf/briar/plugins/bluetooth/BluetoothTransportConnection.java b/components/net/sf/briar/plugins/bluetooth/BluetoothTransportConnection.java
index 4a110cf94b..bc2c1fbd44 100644
--- a/components/net/sf/briar/plugins/bluetooth/BluetoothTransportConnection.java
+++ b/components/net/sf/briar/plugins/bluetooth/BluetoothTransportConnection.java
@@ -3,8 +3,6 @@ package net.sf.briar.plugins.bluetooth;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.util.logging.Level;
-import java.util.logging.Logger;
 
 import javax.microedition.io.StreamConnection;
 
@@ -12,9 +10,6 @@ import net.sf.briar.api.plugins.duplex.DuplexTransportConnection;
 
 class BluetoothTransportConnection implements DuplexTransportConnection {
 
-	private static final Logger LOG =
-		Logger.getLogger(BluetoothTransportConnection.class.getName());
-
 	private final StreamConnection stream;
 
 	BluetoothTransportConnection(StreamConnection stream) {
@@ -33,11 +28,8 @@ class BluetoothTransportConnection implements DuplexTransportConnection {
 		return true;
 	}
 
-	public void dispose(boolean exception, boolean recognised) {
-		try {
-			stream.close();
-		} catch(IOException e) {
-			if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
-		}
+	public void dispose(boolean exception, boolean recognised)
+			throws IOException {
+		stream.close();
 	}
 }
diff --git a/components/net/sf/briar/plugins/socket/SocketTransportConnection.java b/components/net/sf/briar/plugins/socket/SocketTransportConnection.java
index a9e147faf8..953f74f6d7 100644
--- a/components/net/sf/briar/plugins/socket/SocketTransportConnection.java
+++ b/components/net/sf/briar/plugins/socket/SocketTransportConnection.java
@@ -4,16 +4,11 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.Socket;
-import java.util.logging.Level;
-import java.util.logging.Logger;
 
 import net.sf.briar.api.plugins.duplex.DuplexTransportConnection;
 
 class SocketTransportConnection implements DuplexTransportConnection {
 
-	private static final Logger LOG =
-		Logger.getLogger(SocketTransportConnection.class.getName());
-
 	private final Socket socket;
 
 	SocketTransportConnection(Socket socket) {
@@ -32,11 +27,8 @@ class SocketTransportConnection implements DuplexTransportConnection {
 		return true;
 	}
 
-	public void dispose(boolean exception, boolean recognised) {
-		try {
-			socket.close();
-		} catch(IOException e) {
-			if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
-		}
+	public void dispose(boolean exception, boolean recognised)
+			throws IOException {
+		socket.close();
 	}
 }
diff --git a/components/net/sf/briar/plugins/tor/TorTransportConnection.java b/components/net/sf/briar/plugins/tor/TorTransportConnection.java
index 4a8d74256f..c258e0dbe7 100644
--- a/components/net/sf/briar/plugins/tor/TorTransportConnection.java
+++ b/components/net/sf/briar/plugins/tor/TorTransportConnection.java
@@ -3,8 +3,6 @@ package net.sf.briar.plugins.tor;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.util.logging.Level;
-import java.util.logging.Logger;
 
 import net.sf.briar.api.plugins.duplex.DuplexTransportConnection;
 
@@ -12,9 +10,6 @@ import org.silvertunnel.netlib.api.NetSocket;
 
 class TorTransportConnection implements DuplexTransportConnection {
 
-	private static final Logger LOG =
-		Logger.getLogger(TorTransportConnection.class.getName());
-
 	private final NetSocket socket;
 
 	TorTransportConnection(NetSocket socket) {
@@ -33,11 +28,8 @@ class TorTransportConnection implements DuplexTransportConnection {
 		return true;
 	}
 
-	public void dispose(boolean exception, boolean recognised) {
-		try {
-			socket.close();
-		} catch(IOException e) {
-			if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
-		}
+	public void dispose(boolean exception, boolean recognised)
+			throws IOException {
+		socket.close();
 	}
 }
diff --git a/components/net/sf/briar/protocol/duplex/DuplexConnection.java b/components/net/sf/briar/protocol/duplex/DuplexConnection.java
index 02da9b9e43..37f6c6bb52 100644
--- a/components/net/sf/briar/protocol/duplex/DuplexConnection.java
+++ b/components/net/sf/briar/protocol/duplex/DuplexConnection.java
@@ -232,7 +232,11 @@ abstract class DuplexConnection implements DatabaseListener {
 	private void dispose(boolean exception, boolean recognised) {
 		if(disposed.getAndSet(true)) return;
 		ByteUtils.erase(ctx.getSecret());
-		transport.dispose(exception, recognised);
+		try {
+			transport.dispose(exception, recognised);
+		} catch(IOException e) {
+			if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
+		}
 	}
 
 	// This task runs on a database thread
diff --git a/components/net/sf/briar/transport/ConnectionDispatcherImpl.java b/components/net/sf/briar/transport/ConnectionDispatcherImpl.java
index f6954abcc4..3a17968e39 100644
--- a/components/net/sf/briar/transport/ConnectionDispatcherImpl.java
+++ b/components/net/sf/briar/transport/ConnectionDispatcherImpl.java
@@ -127,21 +127,31 @@ class ConnectionDispatcherImpl implements ConnectionDispatcher {
 		}
 
 		public void run() {
+			byte[] tag;
 			try {
-				byte[] tag = readTag(transport.getInputStream());
-				ConnectionContext ctx = recogniser.acceptConnection(transportId,
-						tag);
-				if(ctx == null) {
-					transport.dispose(false, false);
-				} else {
-					duplexConnFactory.createIncomingConnection(ctx, transport);
-				}
+				tag = readTag(transport.getInputStream());
+			} catch(IOException e) {
+				if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
+				dispose(true, false);
+				return;
+			}
+			ConnectionContext ctx = null;
+			try {
+				ctx = recogniser.acceptConnection(transportId, tag);
 			} catch(DbException e) {
 				if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
-				transport.dispose(true, false);
+				dispose(true, false);
+				return;
+			}
+			if(ctx == null) dispose(false, false);
+			else duplexConnFactory.createIncomingConnection(ctx, transport);
+		}
+
+		private void dispose(boolean exception, boolean recognised) {
+			try {
+				transport.dispose(exception, recognised);
 			} catch(IOException e) {
 				if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
-				transport.dispose(true, false);
 			}
 		}
 	}
diff --git a/test/net/sf/briar/plugins/DuplexTest.java b/test/net/sf/briar/plugins/DuplexTest.java
index cd87fca94b..80eee278f0 100644
--- a/test/net/sf/briar/plugins/DuplexTest.java
+++ b/test/net/sf/briar/plugins/DuplexTest.java
@@ -41,7 +41,11 @@ abstract class DuplexTest {
 			d.dispose(false, true);
 		} catch(IOException e) {
 			e.printStackTrace();
-			d.dispose(true, true);
+			try {
+				d.dispose(true, true);
+			} catch(IOException e1) {
+				e1.printStackTrace();
+			}
 		}
 	}
 
@@ -65,7 +69,11 @@ abstract class DuplexTest {
 			d.dispose(false, true);
 		} catch(IOException e) {
 			e.printStackTrace();
-			d.dispose(true, true);
+			try {
+				d.dispose(true, true);
+			} catch(IOException e1) {
+				e1.printStackTrace();
+			}
 		}
 	}
 
-- 
GitLab