From 80cba1e7f76a5a3be5f34217142afd51fa3de98c Mon Sep 17 00:00:00 2001
From: akwizgran <akwizgran@users.sourceforge.net>
Date: Wed, 5 Oct 2011 18:59:30 +0100
Subject: [PATCH] Added finish() methods to transport readers/writers, and
 added a readerFinished() callback method to FilePlugin.

---
 .../api/transport/batch/BatchTransportReader.java    | 11 ++++++++---
 .../transport/stream/StreamTransportConnection.java  | 12 +++++++++---
 components/net/sf/briar/plugins/file/FilePlugin.java |  4 +++-
 .../sf/briar/plugins/file/FileTransportReader.java   |  9 ++++++++-
 .../sf/briar/plugins/file/RemovableDrivePlugin.java  |  5 +++++
 5 files changed, 33 insertions(+), 8 deletions(-)

diff --git a/api/net/sf/briar/api/transport/batch/BatchTransportReader.java b/api/net/sf/briar/api/transport/batch/BatchTransportReader.java
index 486f441d49..031fb75418 100644
--- a/api/net/sf/briar/api/transport/batch/BatchTransportReader.java
+++ b/api/net/sf/briar/api/transport/batch/BatchTransportReader.java
@@ -13,9 +13,14 @@ public interface BatchTransportReader {
 	InputStream getInputStream();
 
 	/**
-	 * Closes the reader and 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.
+	 * Finishes reading from the transport. This method should be called after
+	 * closing the input stream.
+	 */
+	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;
 }
diff --git a/api/net/sf/briar/api/transport/stream/StreamTransportConnection.java b/api/net/sf/briar/api/transport/stream/StreamTransportConnection.java
index d90a450187..c66edb69f6 100644
--- a/api/net/sf/briar/api/transport/stream/StreamTransportConnection.java
+++ b/api/net/sf/briar/api/transport/stream/StreamTransportConnection.java
@@ -18,9 +18,15 @@ public interface StreamTransportConnection {
 	OutputStream getOutputStream() throws IOException;
 
 	/**
-	 * Closes the connection and 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.
+	 * Finishes using the transport. This method should be called after closing
+	 * the input and output streams.
+	 */
+	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 close() throws IOException;
 }
diff --git a/components/net/sf/briar/plugins/file/FilePlugin.java b/components/net/sf/briar/plugins/file/FilePlugin.java
index 20191a930d..1ca4e7fd87 100644
--- a/components/net/sf/briar/plugins/file/FilePlugin.java
+++ b/components/net/sf/briar/plugins/file/FilePlugin.java
@@ -32,6 +32,7 @@ abstract class FilePlugin implements BatchTransportPlugin {
 
 	protected abstract File chooseOutputDirectory();
 	protected abstract void writerFinished(File f);
+	protected abstract void readerFinished(File f);
 
 	FilePlugin(Executor executor) {
 		this.executor = executor;
@@ -138,7 +139,8 @@ abstract class FilePlugin implements BatchTransportPlugin {
 			if(f.length() < TransportConstants.MIN_CONNECTION_LENGTH) return;
 			try {
 				FileInputStream in = new FileInputStream(f);
-				callback.readerCreated(new FileTransportReader(f, in));
+				callback.readerCreated(new FileTransportReader(f, in,
+						FilePlugin.this));
 			} catch(IOException e) {
 				return;
 			}
diff --git a/components/net/sf/briar/plugins/file/FileTransportReader.java b/components/net/sf/briar/plugins/file/FileTransportReader.java
index 50f6b55574..c44976ca5a 100644
--- a/components/net/sf/briar/plugins/file/FileTransportReader.java
+++ b/components/net/sf/briar/plugins/file/FileTransportReader.java
@@ -10,12 +10,14 @@ class FileTransportReader implements BatchTransportReader {
 
 	private final File file;
 	private final InputStream in;
+	private final FilePlugin plugin;
 
 	private boolean streamInUse = false;
 
-	FileTransportReader(File file, InputStream in) {
+	FileTransportReader(File file, InputStream in, FilePlugin plugin) {
 		this.file = file;
 		this.in = in;
+		this.plugin = plugin;
 	}
 
 	public InputStream getInputStream() {
@@ -23,6 +25,11 @@ class FileTransportReader implements BatchTransportReader {
 		return in;
 	}
 
+	public void finish() throws IOException {
+		streamInUse = false;
+		plugin.readerFinished(file);
+	}
+
 	public void dispose() throws IOException {
 		if(streamInUse) in.close();
 		file.delete();
diff --git a/components/net/sf/briar/plugins/file/RemovableDrivePlugin.java b/components/net/sf/briar/plugins/file/RemovableDrivePlugin.java
index e0f3523ae6..e3df83ed70 100644
--- a/components/net/sf/briar/plugins/file/RemovableDrivePlugin.java
+++ b/components/net/sf/briar/plugins/file/RemovableDrivePlugin.java
@@ -65,6 +65,11 @@ implements RemovableDriveMonitor.Callback {
 		}
 	}
 
+	@Override
+	protected void readerFinished(File f) {
+		callback.showMessage("REMOVABLE_DRIVE_READ_FINISHED");
+	}
+
 	@Override
 	protected void writerFinished(File f) {
 		callback.showMessage("REMOVABLE_DRIVE_WRITE_FINISHED");
-- 
GitLab