Skip to content

Commit 032c973

Browse files
committed
Tidied up interrupt check, added test case to make sure it works.
1 parent cd45173 commit 032c973

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

CHANGES

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ jsoup changelog
1111

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

14+
* Allow the Jsoup.Connect thread to be interrupted when reading the input stream; helps when reading from a long stream
15+
of data that doesn't read timeout.
16+
<https://github.com/jhy/jsoup/pull/712>
17+
1418
* In Jsoup.Connect, now detects if a header value is actually in UTF-8 vs the HTTP spec of ISO-8859, and converts
1519
the header value appropriately.
1620

src/main/java/org/jsoup/helper/DataUtil.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ static Document parseByteData(ByteBuffer byteData, String charsetName, String ba
139139
}
140140

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

156-
while (!Thread.currentThread().isInterrupted()) {
157+
while (!Thread.interrupted()) {
157158
read = inStream.read(buffer);
158159
if (read == -1) break;
159160
if (capped) {
@@ -165,11 +166,7 @@ public static ByteBuffer readToByteBuffer(InputStream inStream, int maxSize) thr
165166
}
166167
outStream.write(buffer, 0, read);
167168
}
168-
169-
if (Thread.currentThread().isInterrupted()) {
170-
Thread.interrupted();
171-
throw new IOException("JSoup thread has been interrupted");
172-
}
169+
173170
return ByteBuffer.wrap(outStream.toByteArray());
174171
}
175172

src/test/java/org/jsoup/integration/UrlConnectTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,4 +771,29 @@ public void inWildUtfRedirect2() throws IOException {
771771
doc.location()
772772
);
773773
}
774+
775+
@Test public void canInterruptRead() throws IOException, InterruptedException {
776+
final String[] body = new String[1];
777+
Thread runner = new Thread(new Runnable() {
778+
public void run() {
779+
try {
780+
Connection.Response res = Jsoup.connect("http://jsscxml.org/serverload.stream")
781+
.timeout(10 * 1000)
782+
.execute();
783+
body[0] = res.body();
784+
} catch (IOException e) {
785+
throw new RuntimeException(e);
786+
}
787+
788+
}
789+
});
790+
791+
runner.start();
792+
Thread.sleep(1000 * 5);
793+
runner.interrupt();
794+
assertTrue(runner.isInterrupted());
795+
runner.join();
796+
797+
assertTrue(body[0].length() > 0);
798+
}
774799
}

0 commit comments

Comments
 (0)