From c08f6c4d1e64ef92f726008b8690e35b8e47fe59 Mon Sep 17 00:00:00 2001
From: akwizgran <akwizgran@users.sourceforge.net>
Date: Thu, 6 Oct 2011 19:00:33 +0100
Subject: [PATCH] Merged transports' finish() and dispose() methods.

---
 .../transport/batch/BatchTransportReader.java  | 12 +++---------
 .../transport/batch/BatchTransportWriter.java  | 12 +++---------
 .../stream/StreamTransportConnection.java      | 13 +++----------
 .../plugins/file/FileTransportReader.java      | 18 +++++++-----------
 .../plugins/file/FileTransportWriter.java      | 18 +++++++-----------
 .../plugins/socket/SimpleSocketPlugin.java     |  2 +-
 .../sf/briar/plugins/socket/SocketPlugin.java  |  1 +
 .../socket/SocketTransportConnection.java      | 13 ++-----------
 .../plugins/file/RemovableDrivePluginTest.java |  3 +--
 .../plugins/socket/SimpleSocketPluginTest.java |  2 +-
 10 files changed, 29 insertions(+), 65 deletions(-)

diff --git a/api/net/sf/briar/api/transport/batch/BatchTransportReader.java b/api/net/sf/briar/api/transport/batch/BatchTransportReader.java
index 031fb75418..e0e05dc501 100644
--- a/api/net/sf/briar/api/transport/batch/BatchTransportReader.java
+++ b/api/net/sf/briar/api/transport/batch/BatchTransportReader.java
@@ -13,14 +13,8 @@ public interface BatchTransportReader {
 	InputStream getInputStream();
 
 	/**
-	 * Finishes reading from the transport. This method should be called after
-	 * closing the input stream.
+	 * Closes the reader and disposes of any associated state. The argument
+	 * should be false if an exception was thrown while using the reader.
 	 */
-	void finish() throws IOException;
-
-	/**
-	 * Disposes of any associated state. This method must be called even if the
-	 * reader is not used, or if an exception is thrown while using the reader.
-	 */
-	void dispose() throws IOException;
+	void dispose(boolean success) throws IOException;
 }
diff --git a/api/net/sf/briar/api/transport/batch/BatchTransportWriter.java b/api/net/sf/briar/api/transport/batch/BatchTransportWriter.java
index 4ea8763306..510d66a524 100644
--- a/api/net/sf/briar/api/transport/batch/BatchTransportWriter.java
+++ b/api/net/sf/briar/api/transport/batch/BatchTransportWriter.java
@@ -16,14 +16,8 @@ public interface BatchTransportWriter {
 	OutputStream getOutputStream();
 
 	/**
-	 * Finishes writing to the transport. This method should be called after
-	 * flushing and closing the output stream.
+	 * Closes the writer and disposes of any associated state. The argument
+	 * should be false if an exception was thrown while using the writer.
 	 */
-	void finish() throws IOException;
-
-	/**
-	 * Disposes of any associated state. This method must be called even if the
-	 * writer is not used, or if an exception is thrown while using the writer.
-	 */
-	void dispose() throws IOException;
+	void dispose(boolean success) throws IOException;
 }
diff --git a/api/net/sf/briar/api/transport/stream/StreamTransportConnection.java b/api/net/sf/briar/api/transport/stream/StreamTransportConnection.java
index aa3c25c818..71ffa8da87 100644
--- a/api/net/sf/briar/api/transport/stream/StreamTransportConnection.java
+++ b/api/net/sf/briar/api/transport/stream/StreamTransportConnection.java
@@ -18,15 +18,8 @@ public interface StreamTransportConnection {
 	OutputStream getOutputStream() throws IOException;
 
 	/**
-	 * Finishes using the transport. This method should be called after closing
-	 * the input and output streams.
+	 * Closes the connection and disposes of any associated state. The argument
+	 * should be false if an exception was thrown while using the connection.
 	 */
-	void finish() throws IOException;
-
-	/**
-	 * Disposes of any associated state. This method must be called even if the
-	 * connection is not used, or if an exception is thrown while using the
-	 * connection.
-	 */
-	void dispose() throws IOException;
+	void dispose(boolean success) throws IOException;
 }
diff --git a/components/net/sf/briar/plugins/file/FileTransportReader.java b/components/net/sf/briar/plugins/file/FileTransportReader.java
index c44976ca5a..a5fafef358 100644
--- a/components/net/sf/briar/plugins/file/FileTransportReader.java
+++ b/components/net/sf/briar/plugins/file/FileTransportReader.java
@@ -12,8 +12,6 @@ class FileTransportReader implements BatchTransportReader {
 	private final InputStream in;
 	private final FilePlugin plugin;
 
-	private boolean streamInUse = false;
-
 	FileTransportReader(File file, InputStream in, FilePlugin plugin) {
 		this.file = file;
 		this.in = in;
@@ -21,17 +19,15 @@ class FileTransportReader implements BatchTransportReader {
 	}
 
 	public InputStream getInputStream() {
-		streamInUse = true;
 		return in;
 	}
 
-	public void finish() throws IOException {
-		streamInUse = false;
-		plugin.readerFinished(file);
-	}
-
-	public void dispose() throws IOException {
-		if(streamInUse) in.close();
-		file.delete();
+	public void dispose(boolean success) throws IOException {
+		try {
+			in.close();
+			if(success) plugin.readerFinished(file);
+		} finally {
+			file.delete();
+		}
 	}
 }
diff --git a/components/net/sf/briar/plugins/file/FileTransportWriter.java b/components/net/sf/briar/plugins/file/FileTransportWriter.java
index b5d3532555..19bf88a1ce 100644
--- a/components/net/sf/briar/plugins/file/FileTransportWriter.java
+++ b/components/net/sf/briar/plugins/file/FileTransportWriter.java
@@ -13,8 +13,6 @@ class FileTransportWriter implements BatchTransportWriter {
 	private final long capacity;
 	private final FilePlugin plugin;
 
-	private boolean streamInUse = false;
-
 	FileTransportWriter(File file, OutputStream out, long capacity,
 			FilePlugin plugin) {
 		this.file = file;
@@ -28,17 +26,15 @@ class FileTransportWriter implements BatchTransportWriter {
 	}
 
 	public OutputStream getOutputStream() {
-		streamInUse = true;
 		return out;
 	}
 
-	public void finish() {
-		streamInUse = false;
-		plugin.writerFinished(file);
-	}
-
-	public void dispose() throws IOException {
-		if(streamInUse) out.close();
-		file.delete();
+	public void dispose(boolean success) throws IOException {
+		try {
+			out.close();
+			if(success) plugin.writerFinished(file);
+		} finally {
+			file.delete();
+		}
 	}
 }
diff --git a/components/net/sf/briar/plugins/socket/SimpleSocketPlugin.java b/components/net/sf/briar/plugins/socket/SimpleSocketPlugin.java
index be8d73dfe5..7f5e915be7 100644
--- a/components/net/sf/briar/plugins/socket/SimpleSocketPlugin.java
+++ b/components/net/sf/briar/plugins/socket/SimpleSocketPlugin.java
@@ -12,7 +12,7 @@ import java.util.concurrent.Executor;
 import net.sf.briar.api.ContactId;
 import net.sf.briar.api.TransportId;
 
-public class SimpleSocketPlugin extends SocketPlugin {
+class SimpleSocketPlugin extends SocketPlugin {
 
 	public static final int TRANSPORT_ID = 1;
 
diff --git a/components/net/sf/briar/plugins/socket/SocketPlugin.java b/components/net/sf/briar/plugins/socket/SocketPlugin.java
index ef693baa72..fc2cfaf374 100644
--- a/components/net/sf/briar/plugins/socket/SocketPlugin.java
+++ b/components/net/sf/briar/plugins/socket/SocketPlugin.java
@@ -135,6 +135,7 @@ implements StreamTransportPlugin {
 	}
 
 	public synchronized void poll() {
+		// Subclasses may not support polling
 		if(!shouldPoll()) throw new UnsupportedOperationException();
 		if(!started) return;
 		for(ContactId c : remoteProperties.keySet()) {
diff --git a/components/net/sf/briar/plugins/socket/SocketTransportConnection.java b/components/net/sf/briar/plugins/socket/SocketTransportConnection.java
index 92488fefff..bfc68198f4 100644
--- a/components/net/sf/briar/plugins/socket/SocketTransportConnection.java
+++ b/components/net/sf/briar/plugins/socket/SocketTransportConnection.java
@@ -11,28 +11,19 @@ class SocketTransportConnection implements StreamTransportConnection {
 
 	private final Socket socket;
 
-	private boolean streamInUse = false;
-
 	SocketTransportConnection(Socket socket) {
 		this.socket = socket;
 	}
 
 	public InputStream getInputStream() throws IOException {
-		streamInUse = true;
 		return socket.getInputStream();
 	}
 
 	public OutputStream getOutputStream() throws IOException {
-		streamInUse = true;
 		return socket.getOutputStream();
 	}
 
-	public void finish() throws IOException {
-		// FIXME: Tell the plugin?
-		streamInUse = false;
-	}
-
-	public void dispose() throws IOException {
-		if(streamInUse) socket.close();
+	public void dispose(boolean success) throws IOException {
+		socket.close();
 	}
 }
diff --git a/test/net/sf/briar/plugins/file/RemovableDrivePluginTest.java b/test/net/sf/briar/plugins/file/RemovableDrivePluginTest.java
index 4c942a4a1b..2bede14174 100644
--- a/test/net/sf/briar/plugins/file/RemovableDrivePluginTest.java
+++ b/test/net/sf/briar/plugins/file/RemovableDrivePluginTest.java
@@ -289,10 +289,9 @@ public class RemovableDrivePluginTest extends TestCase {
 		out.write(new byte[123]);
 		out.flush();
 		out.close();
-		writer.finish();
 		assertEquals(123L, files[0].length());
 		// Disposing of the writer should delete the file
-		writer.dispose();
+		writer.dispose(true);
 		files = drive1.listFiles();
 		assertTrue(files == null || files.length == 0);
 
diff --git a/test/net/sf/briar/plugins/socket/SimpleSocketPluginTest.java b/test/net/sf/briar/plugins/socket/SimpleSocketPluginTest.java
index 6ea3fbf492..6093a7e360 100644
--- a/test/net/sf/briar/plugins/socket/SimpleSocketPluginTest.java
+++ b/test/net/sf/briar/plugins/socket/SimpleSocketPluginTest.java
@@ -107,7 +107,7 @@ public class SimpleSocketPluginTest extends TestCase {
 		assertTrue(latch.await(1, TimeUnit.SECONDS));
 		assertFalse(error.get());
 		// Clean up
-		conn.getInputStream().close(); // FIXME: Change the API
+		conn.dispose(true);
 		ss.close();
 		plugin.stop();
 	}
-- 
GitLab