Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

URI and Path specifier classes for input/output resources. #34

Merged
merged 3 commits into from
Feb 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ subprojects {

dependencies {
testCompile "org.testng:testng:6.14.3"
testCompile "com.google.jimfs:jimfs:1.1"
testCompile "org.apache.commons:commons-lang3:3.7"

compile 'commons-io:commons-io:2.5'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot to mention: is this needed in this PR?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch - its left over from previous changes and can be removed.

}

test {
Expand Down
1 change: 1 addition & 0 deletions core/-
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
some stuff
90 changes: 90 additions & 0 deletions core/src/main/java/org/htsjdk/core/api/io/IOResource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.htsjdk.core.api.io;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.nio.file.Path;
import java.util.Optional;

/**
* Interface representing htsjdk-next input/output resources.
*/
public interface IOResource {

/**
* Determine if this resource has a scheme that has an installed NIO file system provider. This does not
* guarantee the resource can be converted into a {@code java.nio.file.Path}, since the resource can be
* syntactically valid, and specify a valid file system provider, but still fail to be semantically meaningful.
* @return true if this URI has a scheme that has an installed NIO file system provider.
*/
boolean isNIO();

/**
* Return true if this {code IOResource} can be resolved to an NIO Path. If true, {@code #toPath()} can be
* safely called.
*
* There are cases where a valid URI with a valid scheme backed by an installed NIO File System
* still can't be turned into a {@code java.nio.file.Path}, i.e., the following specifies an invalid
* authority "namenode":
*
* file://namenode/to/file
*
* @return {@code true} if this {@code IOResource} can be resolved to an NIO Path.
*/
boolean isPath();

/**
* Get a {@code java.net.URI} object for this {@code IOResource}. Will not be null.
* @return The {@code URI} object for this IOResource.
*/
URI getURI();

/**
* Returns the String representation of the {{@code UR} backing this {@code IOResource} URI. This string
* may differ from the normalized string returned from a Path that has been object resolved from this
* IOResource.
*
* @return String from which this URI as originally created. Will not be null, and will always
* include a URI scheme.
*/
default String getURIString() { return getURI().toString(); }

/**
* Return the raw (source) input used to create this {@code IOResource} as a String.
*/
String getRawInputString();

/**
* Resolve this {@code IOResource} to an NIO Path. Can be safely called only if {@link #isPath()} returns true.
*/
Path toPath();

/**
* Return a string message describing why this IOResource cannot be converted to a {@code java.nio.file.Path}
* ({@code #isPath()} returns false).
*
* @return Optional<String></String> message explaining toPath failure reason, since it can fail for various reasons.
*/
Optional<String> getToPathFailureReason();

/**
* Return the scheme for this IOResource. For file resources (URIs that have no explicit scheme), this
* will return the scheme "file".
* @return the scheme String for the URI backing this {@code IOResource}, if any. Will not be null.
*/
default String getScheme() {
return getURI().getScheme();
}

/**
* Get a {@code InputStream} for this resource.
* @return {@code InputStream} for this resource.
*/
InputStream getInputStream();

/**
* Get an {@code OutputStream} for this resource.
* @return {@code OutputStream} for this URI.
*/
OutputStream getOutputStream();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.htsjdk.core.exception;

public class HtsjdkIOException extends HtsjdkException {

/**
* Constructs an HtsjdkIOException exception.
*
* @param message detailed message.
*/
public HtsjdkIOException(String message) {
super(message);
}

/**
* Constructs an HtsjdkIOException exception with a specified cause.
*
* @param message detailed message.
* @param cause cause of the exception.
*/
public HtsjdkIOException(String message, Throwable cause) {
super(message, cause);
}

/**
* Constructs an HtsjdkIOException exception with a message constructed from the cause.
*
* @param cause cause of the exception.
*/
public HtsjdkIOException(Throwable cause) {
super(cause);
}
}
22 changes: 22 additions & 0 deletions core/src/main/java/org/htsjdk/core/utils/IOUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.htsjdk.core.utils;

import java.io.File;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No File in htsjdk3. This class should be removed and the only method usages modified in favor of Files.createTempFile

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point - done.

import java.io.IOException;
import java.nio.file.Path;

public class IOUtils {

/**
* Create a temporary file using a given name prefix and name suffix and return a {@link java.nio.file.Path}.
* @param prefix
* @param suffix
* @return temp File that will be deleted on exit
* @throws IOException
*/
public static Path createTempPath(final String prefix, final String suffix) throws IOException {
final File tempFile = File.createTempFile(prefix, suffix);
tempFile.deleteOnExit();
return tempFile.toPath();
}

}
Loading