From 4e549f4044570ccee73c3bf8508d1d7a50bb77ee Mon Sep 17 00:00:00 2001 From: florian <60937022+cdr-chakotay@users.noreply.github.com> Date: Fri, 9 Jul 2021 17:55:03 +0200 Subject: [PATCH 1/5] Update TusUploader.java Make uploads pausable without closing file input Streams --- .../java/io/tus/java/client/TusUploader.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/main/java/io/tus/java/client/TusUploader.java b/src/main/java/io/tus/java/client/TusUploader.java index 4fca7ba..6d7f651 100644 --- a/src/main/java/io/tus/java/client/TusUploader.java +++ b/src/main/java/io/tus/java/client/TusUploader.java @@ -260,6 +260,24 @@ public long getOffset() { public URL getUploadURL() { return uploadURL; } + + /** + * You can call this method even before the entire file has been uploaded. + * It pauses the upload properly to be resumed in future. + * Be aware it doesn't release local resources as it does not close the File's Input Stream. + * To be safe use {@link TusUploader#finish()}. + * @throws ProtocolException Thrown if the server sends an unexpected status + * code + * @throws IOException Thrown if an exception occurs while cleaning up. + */ + public void pause() throws ProtocolException, IOException { + finishConnection(); + if (upload.getSize() == offset) { + client.uploadFinished(upload); + } + input.seekTo(0); + } + /** * Finish the request by closing the HTTP connection and the InputStream. From 5f2731d155d3dbf6ee00d1dd64f36c3978413231 Mon Sep 17 00:00:00 2001 From: florian <60937022+cdr-chakotay@users.noreply.github.com> Date: Wed, 14 Jul 2021 16:24:24 +0200 Subject: [PATCH 2/5] Update TusUploader.java Add a pause functionality, which doesn't close the InputStream --- .../java/io/tus/java/client/TusUploader.java | 42 +++++++++++-------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/src/main/java/io/tus/java/client/TusUploader.java b/src/main/java/io/tus/java/client/TusUploader.java index 6d7f651..75ed80b 100644 --- a/src/main/java/io/tus/java/client/TusUploader.java +++ b/src/main/java/io/tus/java/client/TusUploader.java @@ -261,23 +261,7 @@ public URL getUploadURL() { return uploadURL; } - /** - * You can call this method even before the entire file has been uploaded. - * It pauses the upload properly to be resumed in future. - * Be aware it doesn't release local resources as it does not close the File's Input Stream. - * To be safe use {@link TusUploader#finish()}. - * @throws ProtocolException Thrown if the server sends an unexpected status - * code - * @throws IOException Thrown if an exception occurs while cleaning up. - */ - public void pause() throws ProtocolException, IOException { - finishConnection(); - if (upload.getSize() == offset) { - client.uploadFinished(upload); - } - input.seekTo(0); - } - + /** * Finish the request by closing the HTTP connection and the InputStream. @@ -298,6 +282,30 @@ public void finish() throws ProtocolException, IOException { // that we will not need to read from it again in the future. input.close(); } + + /** + * Finish the request by closing the HTTP connection. You can choose whether to close the Input Stream or not. + * You can call this method even before the entire file has been uploaded. + * It pauses the upload properly to be resumed in future. + * Be aware it doesn't release local resources as it does not close the File's + * Input Stream if {@code closeStream = false} + * To be safe use {@link TusUploader#finish()}. + * @param closeInputStream Determines whether the InputStream is closed with the HTTP connection. Not closing the + * Input Stream may be useful for future upload a future continuation of the upload. + * @throws ProtocolException Thrown if the server sends an unexpected status code + * @throws IOException Thrown if an exception occurs while cleaning up. + */ + public void finish(boolean closeInputStream) throws ProtocolException, IOException { + if (closeInputStream){ + finish(); + } else { + finishConnection(); + if (upload.getSize() == offset) { + client.uploadFinished(upload); + } + input.seekTo(0); + } + } private void finishConnection() throws ProtocolException, IOException { if(output != null) output.close(); From 16c94d3b5699f0bdee8268d0856b11d7c2e08c90 Mon Sep 17 00:00:00 2001 From: florian <60937022+cdr-chakotay@users.noreply.github.com> Date: Wed, 14 Jul 2021 16:30:07 +0200 Subject: [PATCH 3/5] Update TusUploader.java --- src/main/java/io/tus/java/client/TusUploader.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/io/tus/java/client/TusUploader.java b/src/main/java/io/tus/java/client/TusUploader.java index 75ed80b..3aa0516 100644 --- a/src/main/java/io/tus/java/client/TusUploader.java +++ b/src/main/java/io/tus/java/client/TusUploader.java @@ -260,8 +260,6 @@ public long getOffset() { public URL getUploadURL() { return uploadURL; } - - /** * Finish the request by closing the HTTP connection and the InputStream. @@ -288,7 +286,7 @@ public void finish() throws ProtocolException, IOException { * You can call this method even before the entire file has been uploaded. * It pauses the upload properly to be resumed in future. * Be aware it doesn't release local resources as it does not close the File's - * Input Stream if {@code closeStream = false} + * Input Stream if {@code closeStream == false} * To be safe use {@link TusUploader#finish()}. * @param closeInputStream Determines whether the InputStream is closed with the HTTP connection. Not closing the * Input Stream may be useful for future upload a future continuation of the upload. From e40c8a60aed71e25e9ec335694c06c3174cf3880 Mon Sep 17 00:00:00 2001 From: florian <60937022+cdr-chakotay@users.noreply.github.com> Date: Fri, 6 Aug 2021 00:28:04 +0200 Subject: [PATCH 4/5] Integrate requested changes --- .../java/io/tus/java/client/TusUploader.java | 34 ++++++++----------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/src/main/java/io/tus/java/client/TusUploader.java b/src/main/java/io/tus/java/client/TusUploader.java index 3aa0516..476357d 100644 --- a/src/main/java/io/tus/java/client/TusUploader.java +++ b/src/main/java/io/tus/java/client/TusUploader.java @@ -271,37 +271,31 @@ public URL getUploadURL() { * @throws IOException Thrown if an exception occurs while cleaning up. */ public void finish() throws ProtocolException, IOException { - finishConnection(); - if (upload.getSize() == offset) { - client.uploadFinished(upload); - } - - // Close the TusInputStream after checking the response and closing the connection to ensure - // that we will not need to read from it again in the future. - input.close(); + finish(true); } /** * Finish the request by closing the HTTP connection. You can choose whether to close the Input Stream or not. - * You can call this method even before the entire file has been uploaded. - * It pauses the upload properly to be resumed in future. - * Be aware it doesn't release local resources as it does not close the File's - * Input Stream if {@code closeStream == false} + * You can call this method even before the entire file has been uploaded. Use this behavior to + * enable pausing uploads. + * Be aware it doesn't release local resources if you do not close the + * Input Stream: {@code closeStream == false}. * To be safe use {@link TusUploader#finish()}. * @param closeInputStream Determines whether the InputStream is closed with the HTTP connection. Not closing the * Input Stream may be useful for future upload a future continuation of the upload. * @throws ProtocolException Thrown if the server sends an unexpected status code * @throws IOException Thrown if an exception occurs while cleaning up. */ - public void finish(boolean closeInputStream) throws ProtocolException, IOException { + public void finish(boolean closeInputStream) throws ProtocolException, IOException { + finishConnection(); + if (upload.getSize() == offset) { + client.uploadFinished(upload); + } + + // Close the TusInputStream after checking the response and closing the connection to ensure + // that we will not need to read from it again in the future. if (closeInputStream){ - finish(); - } else { - finishConnection(); - if (upload.getSize() == offset) { - client.uploadFinished(upload); - } - input.seekTo(0); + input.close(); } } From 2949ce39e8d8a630623068848257a40b74f92eee Mon Sep 17 00:00:00 2001 From: Marius Date: Tue, 10 Aug 2021 11:05:37 +0200 Subject: [PATCH 5/5] Code formatting and docs --- src/main/java/io/tus/java/client/TusUploader.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/tus/java/client/TusUploader.java b/src/main/java/io/tus/java/client/TusUploader.java index 476357d..8d87820 100644 --- a/src/main/java/io/tus/java/client/TusUploader.java +++ b/src/main/java/io/tus/java/client/TusUploader.java @@ -265,6 +265,7 @@ public URL getUploadURL() { * Finish the request by closing the HTTP connection and the InputStream. * You can call this method even before the entire file has been uploaded. Use this behavior to * enable pausing uploads. + * This method is equivalent to calling {@code finish(false)}. * * @throws ProtocolException Thrown if the server sends an unexpected status * code @@ -275,12 +276,13 @@ public void finish() throws ProtocolException, IOException { } /** - * Finish the request by closing the HTTP connection. You can choose whether to close the Input Stream or not. + * Finish the request by closing the HTTP connection. You can choose whether to close the InputStream or not. * You can call this method even before the entire file has been uploaded. Use this behavior to * enable pausing uploads. - * Be aware it doesn't release local resources if you do not close the - * Input Stream: {@code closeStream == false}. - * To be safe use {@link TusUploader#finish()}. + * + * Be aware that it doesn't automatically release local resources if {@code closeStream == false} and you do + * not close the InputStream on your own. To be safe use {@link TusUploader#finish()}. + * * @param closeInputStream Determines whether the InputStream is closed with the HTTP connection. Not closing the * Input Stream may be useful for future upload a future continuation of the upload. * @throws ProtocolException Thrown if the server sends an unexpected status code @@ -294,8 +296,8 @@ public void finish(boolean closeInputStream) throws ProtocolException, IOExcepti // Close the TusInputStream after checking the response and closing the connection to ensure // that we will not need to read from it again in the future. - if (closeInputStream){ - input.close(); + if (closeInputStream) { + input.close(); } }