Skip to content

Commit

Permalink
A null inputstream should return an empty doc
Browse files Browse the repository at this point in the history
Fixes #2252
  • Loading branch information
jhy committed Jan 2, 2025
1 parent 9546278 commit e99c87d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/jsoup/Jsoup.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public static Document parse(Path path, @Nullable String charsetName, String bas
@param baseUri The URL where the HTML was retrieved from, to resolve relative links against.
@return sane HTML
@throws IOException if the file could not be found, or read, or if the charsetName is invalid.
@throws IOException if the stream could not be read, or if the charsetName is invalid.
*/
public static Document parse(InputStream in, @Nullable String charsetName, String baseUri) throws IOException {
return DataUtil.load(in, charsetName, baseUri);
Expand All @@ -278,7 +278,7 @@ public static Document parse(InputStream in, @Nullable String charsetName, Strin
@param parser alternate {@link Parser#xmlParser() parser} to use.
@return sane HTML
@throws IOException if the file could not be found, or read, or if the charsetName is invalid.
@throws IOException if the stream could not be read, or if the charsetName is invalid.
*/
public static Document parse(InputStream in, @Nullable String charsetName, String baseUri, Parser parser) throws IOException {
return DataUtil.load(in, charsetName, baseUri, parser);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jsoup/internal/ControllableInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ private ControllableInputStream(SimpleBufferedInput in, int maxSize) {

/**
* If this InputStream is not already a ControllableInputStream, let it be one.
* @param in the input stream to (maybe) wrap
* @param in the input stream to (maybe) wrap. A {@code null} input will create an empty wrapped stream.
* @param maxSize the maximum size to allow to be read. 0 == infinite.
* @return a controllable input stream
*/
public static ControllableInputStream wrap(InputStream in, int maxSize) {
public static ControllableInputStream wrap(@Nullable InputStream in, int maxSize) {
// bufferSize currently unused; consider implementing as a min size in the SoftPool recycler
if (in instanceof ControllableInputStream)
return (ControllableInputStream) in;
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/jsoup/internal/SimpleBufferedInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ class SimpleBufferedInput extends FilterInputStream {
private int bufMark = -1;
private boolean inReadFully = false; // true when the underlying inputstream has been read fully

SimpleBufferedInput(InputStream in) {
SimpleBufferedInput(@Nullable InputStream in) {
super(in);
if (in == null) inReadFully = true; // effectively an empty stream
}

@Override
Expand Down Expand Up @@ -148,7 +149,7 @@ public void reset() throws IOException {

@Override
public void close() throws IOException {
super.close();
if (in != null) super.close();
if (byteBuf == null) return; // already closed, or never allocated
BufferPool.release(byteBuf); // return the buffer to the pool
byteBuf = null; // NPE further attempts to read
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/org/jsoup/helper/DataUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -337,4 +337,11 @@ void handlesUnlimitedRead() throws IOException {

assertEquals(input, read);
}

@Test void controllableInputStreamAllowsNull() throws IOException {
ControllableInputStream is = ControllableInputStream.wrap(null, 0);
assertNotNull(is);
assertTrue(is.baseReadFully());
is.close();
}
}
10 changes: 10 additions & 0 deletions src/test/java/org/jsoup/parser/HtmlParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.stream.Stream;

Expand Down Expand Up @@ -1962,4 +1963,13 @@ private static void assertMathNamespace(Element el) {
assertEquals("hidden", div.attr("id"));
assertNotNull(div.attribute("--"));
}

@Test void nullStreamReturnsEmptyDoc() throws IOException {
// https://github.com/jhy/jsoup/issues/2252
InputStream stream = null;
Document doc = Jsoup.parse(stream, null, "");
// don't want to mark parse(stream) as @Nullable, as it's more useful to show the warning. But support it, for backwards compat
assertNotNull(doc);
assertEquals("", doc.title());
}
}

0 comments on commit e99c87d

Please sign in to comment.