|
1 | 1 | package org.jsoup.helper;
|
2 | 2 |
|
3 | 3 | import org.jsoup.Jsoup;
|
| 4 | +import org.jsoup.integration.ParseTest; |
4 | 5 | import org.jsoup.nodes.Document;
|
5 | 6 | import org.jsoup.parser.Parser;
|
6 | 7 | import org.junit.jupiter.api.Test;
|
7 | 8 |
|
8 | 9 | import java.io.*;
|
| 10 | +import java.nio.ByteBuffer; |
9 | 11 | import java.nio.charset.Charset;
|
10 | 12 | import java.nio.charset.StandardCharsets;
|
11 | 13 | import java.nio.file.Files;
|
@@ -228,4 +230,49 @@ public void handlesFakeGzipFile() throws IOException {
|
228 | 230 | assertEquals("This is not gzipped", doc.title());
|
229 | 231 | assertEquals("And should still be readable.", doc.selectFirst("p").text());
|
230 | 232 | }
|
| 233 | + |
| 234 | + // an input stream to give a range of output sizes, that changes on each read |
| 235 | + static class VaryingReadInputStream extends InputStream { |
| 236 | + final InputStream in; |
| 237 | + int stride = 0; |
| 238 | + |
| 239 | + VaryingReadInputStream(InputStream in) { |
| 240 | + this.in = in; |
| 241 | + } |
| 242 | + |
| 243 | + public int read() throws IOException { |
| 244 | + return in.read(); |
| 245 | + } |
| 246 | + |
| 247 | + public int read(byte[] b) throws IOException { |
| 248 | + return in.read(b, 0, Math.min(b.length, ++stride)); |
| 249 | + } |
| 250 | + |
| 251 | + public int read(byte[] b, int off, int len) throws IOException { |
| 252 | + return in.read(b, off, Math.min(len, ++stride)); |
| 253 | + } |
| 254 | + } |
| 255 | + |
| 256 | + @Test |
| 257 | + void handlesChunkedInputStream() throws IOException { |
| 258 | + File inputFile = ParseTest.getFile("/htmltests/large.html"); |
| 259 | + String input = ParseTest.getFileAsString(inputFile); |
| 260 | + VaryingReadInputStream stream = new VaryingReadInputStream(ParseTest.inputStreamFrom(input)); |
| 261 | + |
| 262 | + Document expected = Jsoup.parse(input, "https://example.com"); |
| 263 | + Document doc = Jsoup.parse(stream, null, "https://example.com"); |
| 264 | + assertTrue(doc.hasSameValue(expected)); |
| 265 | + } |
| 266 | + |
| 267 | + @Test |
| 268 | + void handlesUnlimitedRead() throws IOException { |
| 269 | + File inputFile = ParseTest.getFile("/htmltests/large.html"); |
| 270 | + String input = ParseTest.getFileAsString(inputFile); |
| 271 | + VaryingReadInputStream stream = new VaryingReadInputStream(ParseTest.inputStreamFrom(input)); |
| 272 | + |
| 273 | + ByteBuffer byteBuffer = DataUtil.readToByteBuffer(stream, 0); |
| 274 | + String read = new String(byteBuffer.array()); |
| 275 | + |
| 276 | + assertEquals(input, read); |
| 277 | + } |
231 | 278 | }
|
0 commit comments