Skip to content

Commit

Permalink
#134: UrlAsInput accepts URI and String
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jun 14, 2017
1 parent 0359abd commit 0fc720a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
35 changes: 32 additions & 3 deletions src/main/java/org/cactoos/io/UrlAsInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import org.cactoos.Input;
import org.cactoos.Scalar;
import org.cactoos.func.IoCheckedScalar;

/**
* URL as Input.
Expand All @@ -42,19 +45,45 @@ public final class UrlAsInput implements Input {
/**
* The URL.
*/
private final URL source;
private final Scalar<URL> source;

/**
* Ctor.
* @param url The URL
* @since 0.6
*/
public UrlAsInput(final String url) {
this(() -> new URL(url));
}

/**
* Ctor.
* @param url The URL
* @since 0.6
*/
public UrlAsInput(final URI url) {
this(url::toURL);
}

/**
* Ctor.
* @param url The URL
*/
public UrlAsInput(final URL url) {
this.source = url;
this(() -> url);
}

/**
* Ctor.
* @param src Source
*/
public UrlAsInput(final Scalar<URL> src) {
this.source = src;
}

@Override
public InputStream stream() throws IOException {
return this.source.openStream();
return new IoCheckedScalar<>(this.source).asValue().openStream();
}

}
4 changes: 1 addition & 3 deletions src/test/java/org/cactoos/io/UrlAsInputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ public void readsRealUrl() throws IOException {
"Can't fetch bytes from the URL",
new BytesAsText(
new InputAsBytes(
new UrlAsInput(
home.toURL()
)
new UrlAsInput(home)
)
),
new TextHasString(
Expand Down

0 comments on commit 0fc720a

Please sign in to comment.