Skip to content

Commit

Permalink
Tidied up interrupt check, added test case to make sure it works.
Browse files Browse the repository at this point in the history
  • Loading branch information
jhy committed Oct 30, 2016
1 parent cd45173 commit 032c973
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ jsoup changelog

* Added Node.root() to get the topmost ancestor of a Node.

* Allow the Jsoup.Connect thread to be interrupted when reading the input stream; helps when reading from a long stream
of data that doesn't read timeout.
<https://github.com/jhy/jsoup/pull/712>

* In Jsoup.Connect, now detects if a header value is actually in UTF-8 vs the HTTP spec of ISO-8859, and converts
the header value appropriately.

Expand Down
11 changes: 4 additions & 7 deletions src/main/java/org/jsoup/helper/DataUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ static Document parseByteData(ByteBuffer byteData, String charsetName, String ba
}

/**
* Read the input stream into a byte buffer.
* Read the input stream into a byte buffer. To deal with slow input streams, you may interrupt the thread this
* method is executing on. The data read until being interrupted will be available.
* @param inStream the input stream to read from
* @param maxSize the maximum size in bytes to read from the stream. Set to 0 to be unlimited.
* @return the filled byte buffer
Expand All @@ -153,7 +154,7 @@ public static ByteBuffer readToByteBuffer(InputStream inStream, int maxSize) thr
int read;
int remaining = maxSize;

while (!Thread.currentThread().isInterrupted()) {
while (!Thread.interrupted()) {
read = inStream.read(buffer);
if (read == -1) break;
if (capped) {
Expand All @@ -165,11 +166,7 @@ public static ByteBuffer readToByteBuffer(InputStream inStream, int maxSize) thr
}
outStream.write(buffer, 0, read);
}

if (Thread.currentThread().isInterrupted()) {
Thread.interrupted();
throw new IOException("JSoup thread has been interrupted");
}

return ByteBuffer.wrap(outStream.toByteArray());
}

Expand Down
25 changes: 25 additions & 0 deletions src/test/java/org/jsoup/integration/UrlConnectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -771,4 +771,29 @@ public void inWildUtfRedirect2() throws IOException {
doc.location()
);
}

@Test public void canInterruptRead() throws IOException, InterruptedException {
final String[] body = new String[1];
Thread runner = new Thread(new Runnable() {
public void run() {
try {
Connection.Response res = Jsoup.connect("http://jsscxml.org/serverload.stream")
.timeout(10 * 1000)
.execute();
body[0] = res.body();
} catch (IOException e) {
throw new RuntimeException(e);
}

}
});

runner.start();
Thread.sleep(1000 * 5);
runner.interrupt();
assertTrue(runner.isInterrupted());
runner.join();

assertTrue(body[0].length() > 0);
}
}

0 comments on commit 032c973

Please sign in to comment.