From 780f5f2cbc444909e8b16bccd93a68c330b2ae22 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne <31027858+Isira-Seneviratne@users.noreply.github.com> Date: Wed, 13 Sep 2023 04:23:33 +0530 Subject: [PATCH] Switch to Java's UncheckedIOException. (#1989) * Switch to Java's UncheckedIOException. * Path to deprecation in next release The internal throws will continue to throw the internal UncheckedIOException shim, which now extends the java.io version. But the declared / visible throws are java.io. So in the next release we can mark Deprecated, and then remove subsequently. --------- Co-authored-by: Jonathan Hedley --- README.md | 2 +- pom.xml | 1 + src/main/java/org/jsoup/Connection.java | 1 + src/main/java/org/jsoup/UncheckedIOException.java | 7 ++++++- src/main/java/org/jsoup/helper/DataUtil.java | 6 +++--- src/test/java/org/jsoup/integration/SessionIT.java | 2 +- src/test/java/org/jsoup/parser/CharacterReaderTest.java | 2 +- 7 files changed, 14 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 7add27211e..96033b8272 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ jsoup is an open source project distributed under the liberal [MIT license](http 3. Enjoy! ### Android support -When used in Android projects, [core library desugaring](https://developer.android.com/studio/write/java8-support#library-desugaring) should be enabled to support Java 8+ features. +When used in Android projects, [core library desugaring](https://developer.android.com/studio/write/java8-support#library-desugaring) with the [NIO specification](https://developer.android.com/studio/write/java11-nio-support-table) should be enabled to support Java 8+ features. ## Development and support If you have any questions on how to use jsoup, or have ideas for future development, please get in touch via the [mailing list](https://jsoup.org/discussion). diff --git a/pom.xml b/pom.xml index f01b755394..e58fa78906 100644 --- a/pom.xml +++ b/pom.xml @@ -81,6 +81,7 @@ java.util.function.Consumer java.util.function.Supplier java.lang.ThreadLocal + java.io.UncheckedIOException diff --git a/src/main/java/org/jsoup/Connection.java b/src/main/java/org/jsoup/Connection.java index c6afd7f469..4e279a988f 100644 --- a/src/main/java/org/jsoup/Connection.java +++ b/src/main/java/org/jsoup/Connection.java @@ -8,6 +8,7 @@ import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; +import java.io.UncheckedIOException; import java.net.CookieStore; import java.net.Proxy; import java.net.URL; diff --git a/src/main/java/org/jsoup/UncheckedIOException.java b/src/main/java/org/jsoup/UncheckedIOException.java index e7a9cc3dc5..65d878f227 100644 --- a/src/main/java/org/jsoup/UncheckedIOException.java +++ b/src/main/java/org/jsoup/UncheckedIOException.java @@ -2,7 +2,12 @@ import java.io.IOException; -public class UncheckedIOException extends RuntimeException { +/** + * @deprecated Use {@link java.io.UncheckedIOException} instead. This class acted as a compatibility shim for Java + * versions prior to 1.8. + */ +// todo annotate @Deprecated in next release (after previous @Deprecations clear) +public class UncheckedIOException extends java.io.UncheckedIOException { public UncheckedIOException(IOException cause) { super(cause); } diff --git a/src/main/java/org/jsoup/helper/DataUtil.java b/src/main/java/org/jsoup/helper/DataUtil.java index 3f34450b7f..69e1f29893 100644 --- a/src/main/java/org/jsoup/helper/DataUtil.java +++ b/src/main/java/org/jsoup/helper/DataUtil.java @@ -1,6 +1,5 @@ package org.jsoup.helper; -import org.jsoup.UncheckedIOException; import org.jsoup.internal.ConstrainableInputStream; import org.jsoup.internal.Normalizer; import org.jsoup.internal.StringUtil; @@ -23,6 +22,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; +import java.io.UncheckedIOException; import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.CharBuffer; @@ -163,7 +163,7 @@ static Document parseInputStream(@Nullable @WillClose InputStream input, @Nullab else doc = parser.parseInput(defaultDecoded.toString(), baseUri); } catch (UncheckedIOException e) { - throw e.ioException(); + throw e.getCause(); } // look for or HTML5 @@ -218,7 +218,7 @@ else if (first instanceof Comment) { doc = parser.parseInput(reader, baseUri); } catch (UncheckedIOException e) { // io exception when parsing (not seen before because reading the stream as we go) - throw e.ioException(); + throw e.getCause(); } Charset charset = charsetName.equals(defaultCharsetName) ? UTF_8 : Charset.forName(charsetName); doc.outputSettings().charset(charset); diff --git a/src/test/java/org/jsoup/integration/SessionIT.java b/src/test/java/org/jsoup/integration/SessionIT.java index 0bcdb17d1d..4ee30f8b18 100644 --- a/src/test/java/org/jsoup/integration/SessionIT.java +++ b/src/test/java/org/jsoup/integration/SessionIT.java @@ -2,7 +2,6 @@ import org.jsoup.Connection; import org.jsoup.Jsoup; -import org.jsoup.UncheckedIOException; import org.jsoup.integration.servlets.FileServlet; import org.jsoup.integration.servlets.SlowRider; import org.jsoup.nodes.Document; @@ -10,6 +9,7 @@ import org.junit.jupiter.api.Test; import java.io.IOException; +import java.io.UncheckedIOException; import java.util.concurrent.atomic.AtomicInteger; import static org.junit.jupiter.api.Assertions.assertEquals; diff --git a/src/test/java/org/jsoup/parser/CharacterReaderTest.java b/src/test/java/org/jsoup/parser/CharacterReaderTest.java index 37d74948cf..5aa9a58e74 100644 --- a/src/test/java/org/jsoup/parser/CharacterReaderTest.java +++ b/src/test/java/org/jsoup/parser/CharacterReaderTest.java @@ -1,12 +1,12 @@ package org.jsoup.parser; -import org.jsoup.UncheckedIOException; import org.jsoup.integration.ParseTest; import org.junit.jupiter.api.Test; import java.io.BufferedReader; import java.io.IOException; import java.io.StringReader; +import java.io.UncheckedIOException; import static org.junit.jupiter.api.Assertions.*;