Skip to content

Commit

Permalink
Allow http file to be closed early
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Sellman <[email protected]>
  • Loading branch information
tom-seqera committed Oct 15, 2024
1 parent 0c9b333 commit 33458a4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
13 changes: 13 additions & 0 deletions modules/nf-commons/src/main/nextflow/extension/FilesEx.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

package nextflow.extension

import groovy.transform.stc.SimpleType
import org.codehaus.groovy.runtime.IOGroovyMethods

import java.nio.file.OpenOption

import static java.nio.file.StandardCopyOption.*

import java.nio.ByteBuffer
Expand Down Expand Up @@ -617,6 +622,14 @@ class FilesEx {
renameTo( self, FileHelper.asPath(target) )
}

static <T> T withInputStream(Path self, OpenOption option, @ClosureParams(value = SimpleType.class, options = "java.io.InputStream") Closure<T> closure) throws IOException {
return IOGroovyMethods.withStream(newInputStream(self, option), closure)
}

static InputStream newInputStream(Path self, OpenOption... options) {
return Files.newInputStream(self, options)
}

/**
* List the content of a given path folder. The method is semantically
* equivalent to {@code File#list()}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package nextflow.file.http

import java.nio.file.OpenOption

/**
* Options which can be used to modify how an HTTP resource is opened as a file.
*/
enum HttpOpenOption implements OpenOption {
/**
* If this option is supplied when opening an HTTP file, no exception will be thrown if the stream is
* closed before all data has been received.
*
* This can be useful, for example, to read just the first line without downloading the whole file.
*/
ALLOW_EARLY_CLOSE
}
Original file line number Diff line number Diff line change
Expand Up @@ -341,18 +341,21 @@ abstract class XFileSystemProvider extends FileSystemProvider {
if (path.class != XPath)
throw new ProviderMismatchException()

boolean allowEarlyClose = false
if (options.length > 0) {
for (OpenOption opt: options) {
// All OpenOption values except for APPEND and WRITE are allowed
if (opt == StandardOpenOption.APPEND ||
opt == StandardOpenOption.WRITE)
throw new UnsupportedOperationException("'$opt' not allowed");
if (opt == StandardOpenOption.APPEND || opt == StandardOpenOption.WRITE)
throw new UnsupportedOperationException("'$opt' not allowed")

if (opt == HttpOpenOption.ALLOW_EARLY_CLOSE)
allowEarlyClose = true
}
}

final conn = toConnection(path)
final length = conn.getContentLengthLong()
return length>0
return length>0 && !allowEarlyClose
? new FixedInputStream(conn.getInputStream(), length)
: conn.getInputStream()
}
Expand Down

0 comments on commit 33458a4

Please sign in to comment.