|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2020 the original author or authors. |
| 2 | + * Copyright 2002-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
19 | 19 | import java.io.File;
|
20 | 20 | import java.io.IOException;
|
21 | 21 | import java.io.InputStream;
|
| 22 | +import java.io.UncheckedIOException; |
22 | 23 | import java.net.HttpURLConnection;
|
23 | 24 | import java.net.MalformedURLException;
|
24 | 25 | import java.net.URI;
|
|
37 | 38 | * case of the {@code "file:"} protocol.
|
38 | 39 | *
|
39 | 40 | * @author Juergen Hoeller
|
| 41 | + * @author Sam Brannen |
40 | 42 | * @since 28.12.2003
|
41 | 43 | * @see java.net.URL
|
42 | 44 | */
|
@@ -135,6 +137,49 @@ public UrlResource(String protocol, String location, @Nullable String fragment)
|
135 | 137 | }
|
136 | 138 |
|
137 | 139 |
|
| 140 | + /** |
| 141 | + * Create a new {@code UrlResource} from the given {@link URI}. |
| 142 | + * <p>This factory method is a convenience for {@link #UrlResource(URI)} that |
| 143 | + * catches any {@link MalformedURLException} and rethrows it wrapped in an |
| 144 | + * {@link UncheckedIOException}; suitable for use in {@link java.util.Stream} |
| 145 | + * and {@link java.util.Optional} APIs or other scenarios when a checked |
| 146 | + * {@link IOException} is undesirable. |
| 147 | + * @param uri a URI |
| 148 | + * @throws UncheckedIOException if the given URL path is not valid |
| 149 | + * @since 6.0 |
| 150 | + * @see #UrlResource(URI) |
| 151 | + */ |
| 152 | + public static UrlResource from(URI uri) throws UncheckedIOException { |
| 153 | + try { |
| 154 | + return new UrlResource(uri); |
| 155 | + } |
| 156 | + catch (MalformedURLException ex) { |
| 157 | + throw new UncheckedIOException(ex); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Create a new {@code UrlResource} from the given URL path. |
| 163 | + * <p>This factory method is a convenience for {@link #UrlResource(String)} |
| 164 | + * that catches any {@link MalformedURLException} and rethrows it wrapped in an |
| 165 | + * {@link UncheckedIOException}; suitable for use in {@link java.util.Stream} |
| 166 | + * and {@link java.util.Optional} APIs or other scenarios when a checked |
| 167 | + * {@link IOException} is undesirable. |
| 168 | + * @param path a URL path |
| 169 | + * @throws UncheckedIOException if the given URL path is not valid |
| 170 | + * @since 6.0 |
| 171 | + * @see #UrlResource(String) |
| 172 | + */ |
| 173 | + public static UrlResource from(String path) throws UncheckedIOException { |
| 174 | + try { |
| 175 | + return new UrlResource(path); |
| 176 | + } |
| 177 | + catch (MalformedURLException ex) { |
| 178 | + throw new UncheckedIOException(ex); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + |
138 | 183 | /**
|
139 | 184 | * Determine a cleaned URL for the given original URL.
|
140 | 185 | * @param originalUrl the original URL
|
|
0 commit comments