Added finish() methods to transport readers/writers, and added a

readerFinished() callback method to FilePlugin.
This commit is contained in:
akwizgran
2011-10-05 18:59:30 +01:00
parent 775d76d040
commit 80cba1e7f7
5 changed files with 33 additions and 8 deletions

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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();

View File

@@ -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");