|
63 | 63 | import org.apache.commons.io.input.NullInputStream; |
64 | 64 | import org.apache.commons.io.input.StringInputStream; |
65 | 65 | import org.apache.commons.io.output.AppendableWriter; |
| 66 | +import org.apache.commons.io.output.CountingOutputStream; |
66 | 67 | import org.apache.commons.io.output.NullOutputStream; |
67 | 68 | import org.apache.commons.io.output.StringBuilderWriter; |
68 | 69 | import org.apache.commons.io.test.TestUtils; |
69 | 70 | import org.apache.commons.io.test.ThrowOnCloseReader; |
| 71 | +import org.apache.commons.lang3.StringUtils; |
70 | 72 | import org.junit.jupiter.api.BeforeEach; |
71 | 73 | import org.junit.jupiter.api.Disabled; |
72 | 74 | import org.junit.jupiter.api.Test; |
@@ -319,6 +321,39 @@ public void testAsWriterStringBuilder() { |
319 | 321 | assertSame(w, IOUtils.writer(w)); |
320 | 322 | } |
321 | 323 |
|
| 324 | + /** |
| 325 | + * IO-764 IOUtils.write() throws NegativeArraySizeException while writing big strings. |
| 326 | + * <pre> |
| 327 | + * java.lang.OutOfMemoryError: Java heap space |
| 328 | + * at java.lang.StringCoding.encode(StringCoding.java:350) |
| 329 | + * at java.lang.String.getBytes(String.java:941) |
| 330 | + * at org.apache.commons.io.IOUtils.write(IOUtils.java:3367) |
| 331 | + * at org.apache.commons.io.IOUtilsTest.testBigString(IOUtilsTest.java:1659) |
| 332 | + * </pre> |
| 333 | + */ |
| 334 | + @Test |
| 335 | + public void testBigString() throws IOException { |
| 336 | + // 3_000_000 is a size that we can allocate for the test string with Java 8 on the command line as: |
| 337 | + // mvn clean test -Dtest=IOUtilsTest -DtestBigString=3000000 |
| 338 | + // 6_000_000 failed with the above |
| 339 | + // |
| 340 | + // TODO Can we mock the test string for this test to pretend to be larger? |
| 341 | + // Mocking the length seems simple but how about the data? |
| 342 | + final int repeat = Integer.getInteger("testBigString", 3_000_000); |
| 343 | + String data; |
| 344 | + try { |
| 345 | + data = StringUtils.repeat("\uD83D", repeat); |
| 346 | + } catch (OutOfMemoryError e) { |
| 347 | + System.err.printf("Don't fail the test if we cannot build the fixture, just log, fixture size = %,d%n.", repeat); |
| 348 | + e.printStackTrace(); |
| 349 | + return; |
| 350 | + } |
| 351 | + try (CountingOutputStream os = new CountingOutputStream(NullOutputStream.INSTANCE)) { |
| 352 | + IOUtils.write(data, os, StandardCharsets.UTF_8); |
| 353 | + assertEquals(repeat, os.getByteCount()); |
| 354 | + } |
| 355 | + } |
| 356 | + |
322 | 357 | @Test |
323 | 358 | public void testClose() { |
324 | 359 | assertDoesNotThrow(() -> IOUtils.close((Closeable) null)); |
|
0 commit comments