mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-18 21:59:54 +01:00
Merged transports' finish() and dispose() methods.
This commit is contained in:
@@ -13,14 +13,8 @@ public interface BatchTransportReader {
|
|||||||
InputStream getInputStream();
|
InputStream getInputStream();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finishes reading from the transport. This method should be called after
|
* Closes the reader and disposes of any associated state. The argument
|
||||||
* closing the input stream.
|
* should be false if an exception was thrown while using the reader.
|
||||||
*/
|
*/
|
||||||
void finish() throws IOException;
|
void dispose(boolean success) 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;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,14 +16,8 @@ public interface BatchTransportWriter {
|
|||||||
OutputStream getOutputStream();
|
OutputStream getOutputStream();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finishes writing to the transport. This method should be called after
|
* Closes the writer and disposes of any associated state. The argument
|
||||||
* flushing and closing the output stream.
|
* should be false if an exception was thrown while using the writer.
|
||||||
*/
|
*/
|
||||||
void finish() throws IOException;
|
void dispose(boolean success) 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;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,15 +18,8 @@ public interface StreamTransportConnection {
|
|||||||
OutputStream getOutputStream() throws IOException;
|
OutputStream getOutputStream() throws IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finishes using the transport. This method should be called after closing
|
* Closes the connection and disposes of any associated state. The argument
|
||||||
* the input and output streams.
|
* should be false if an exception was thrown while using the connection.
|
||||||
*/
|
*/
|
||||||
void finish() throws IOException;
|
void dispose(boolean success) 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;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,8 +12,6 @@ class FileTransportReader implements BatchTransportReader {
|
|||||||
private final InputStream in;
|
private final InputStream in;
|
||||||
private final FilePlugin plugin;
|
private final FilePlugin plugin;
|
||||||
|
|
||||||
private boolean streamInUse = false;
|
|
||||||
|
|
||||||
FileTransportReader(File file, InputStream in, FilePlugin plugin) {
|
FileTransportReader(File file, InputStream in, FilePlugin plugin) {
|
||||||
this.file = file;
|
this.file = file;
|
||||||
this.in = in;
|
this.in = in;
|
||||||
@@ -21,17 +19,15 @@ class FileTransportReader implements BatchTransportReader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public InputStream getInputStream() {
|
public InputStream getInputStream() {
|
||||||
streamInUse = true;
|
|
||||||
return in;
|
return in;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void finish() throws IOException {
|
public void dispose(boolean success) throws IOException {
|
||||||
streamInUse = false;
|
try {
|
||||||
plugin.readerFinished(file);
|
in.close();
|
||||||
}
|
if(success) plugin.readerFinished(file);
|
||||||
|
} finally {
|
||||||
public void dispose() throws IOException {
|
file.delete();
|
||||||
if(streamInUse) in.close();
|
}
|
||||||
file.delete();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,8 +13,6 @@ class FileTransportWriter implements BatchTransportWriter {
|
|||||||
private final long capacity;
|
private final long capacity;
|
||||||
private final FilePlugin plugin;
|
private final FilePlugin plugin;
|
||||||
|
|
||||||
private boolean streamInUse = false;
|
|
||||||
|
|
||||||
FileTransportWriter(File file, OutputStream out, long capacity,
|
FileTransportWriter(File file, OutputStream out, long capacity,
|
||||||
FilePlugin plugin) {
|
FilePlugin plugin) {
|
||||||
this.file = file;
|
this.file = file;
|
||||||
@@ -28,17 +26,15 @@ class FileTransportWriter implements BatchTransportWriter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public OutputStream getOutputStream() {
|
public OutputStream getOutputStream() {
|
||||||
streamInUse = true;
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void finish() {
|
public void dispose(boolean success) throws IOException {
|
||||||
streamInUse = false;
|
try {
|
||||||
plugin.writerFinished(file);
|
out.close();
|
||||||
}
|
if(success) plugin.writerFinished(file);
|
||||||
|
} finally {
|
||||||
public void dispose() throws IOException {
|
file.delete();
|
||||||
if(streamInUse) out.close();
|
}
|
||||||
file.delete();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import java.util.concurrent.Executor;
|
|||||||
import net.sf.briar.api.ContactId;
|
import net.sf.briar.api.ContactId;
|
||||||
import net.sf.briar.api.TransportId;
|
import net.sf.briar.api.TransportId;
|
||||||
|
|
||||||
public class SimpleSocketPlugin extends SocketPlugin {
|
class SimpleSocketPlugin extends SocketPlugin {
|
||||||
|
|
||||||
public static final int TRANSPORT_ID = 1;
|
public static final int TRANSPORT_ID = 1;
|
||||||
|
|
||||||
|
|||||||
@@ -135,6 +135,7 @@ implements StreamTransportPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void poll() {
|
public synchronized void poll() {
|
||||||
|
// Subclasses may not support polling
|
||||||
if(!shouldPoll()) throw new UnsupportedOperationException();
|
if(!shouldPoll()) throw new UnsupportedOperationException();
|
||||||
if(!started) return;
|
if(!started) return;
|
||||||
for(ContactId c : remoteProperties.keySet()) {
|
for(ContactId c : remoteProperties.keySet()) {
|
||||||
|
|||||||
@@ -11,28 +11,19 @@ class SocketTransportConnection implements StreamTransportConnection {
|
|||||||
|
|
||||||
private final Socket socket;
|
private final Socket socket;
|
||||||
|
|
||||||
private boolean streamInUse = false;
|
|
||||||
|
|
||||||
SocketTransportConnection(Socket socket) {
|
SocketTransportConnection(Socket socket) {
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
}
|
}
|
||||||
|
|
||||||
public InputStream getInputStream() throws IOException {
|
public InputStream getInputStream() throws IOException {
|
||||||
streamInUse = true;
|
|
||||||
return socket.getInputStream();
|
return socket.getInputStream();
|
||||||
}
|
}
|
||||||
|
|
||||||
public OutputStream getOutputStream() throws IOException {
|
public OutputStream getOutputStream() throws IOException {
|
||||||
streamInUse = true;
|
|
||||||
return socket.getOutputStream();
|
return socket.getOutputStream();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void finish() throws IOException {
|
public void dispose(boolean success) throws IOException {
|
||||||
// FIXME: Tell the plugin?
|
socket.close();
|
||||||
streamInUse = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void dispose() throws IOException {
|
|
||||||
if(streamInUse) socket.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -289,10 +289,9 @@ public class RemovableDrivePluginTest extends TestCase {
|
|||||||
out.write(new byte[123]);
|
out.write(new byte[123]);
|
||||||
out.flush();
|
out.flush();
|
||||||
out.close();
|
out.close();
|
||||||
writer.finish();
|
|
||||||
assertEquals(123L, files[0].length());
|
assertEquals(123L, files[0].length());
|
||||||
// Disposing of the writer should delete the file
|
// Disposing of the writer should delete the file
|
||||||
writer.dispose();
|
writer.dispose(true);
|
||||||
files = drive1.listFiles();
|
files = drive1.listFiles();
|
||||||
assertTrue(files == null || files.length == 0);
|
assertTrue(files == null || files.length == 0);
|
||||||
|
|
||||||
|
|||||||
@@ -107,7 +107,7 @@ public class SimpleSocketPluginTest extends TestCase {
|
|||||||
assertTrue(latch.await(1, TimeUnit.SECONDS));
|
assertTrue(latch.await(1, TimeUnit.SECONDS));
|
||||||
assertFalse(error.get());
|
assertFalse(error.get());
|
||||||
// Clean up
|
// Clean up
|
||||||
conn.getInputStream().close(); // FIXME: Change the API
|
conn.dispose(true);
|
||||||
ss.close();
|
ss.close();
|
||||||
plugin.stop();
|
plugin.stop();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user