20 * or visit www.oracle.com if you need additional information or have any |
20 * or visit www.oracle.com if you need additional information or have any |
21 * questions. |
21 * questions. |
22 */ |
22 */ |
23 |
23 |
24 import java.nio.ByteBuffer; |
24 import java.nio.ByteBuffer; |
|
25 import java.util.Arrays; |
25 import java.util.Base64; |
26 import java.util.Base64; |
26 |
27 |
27 /** |
28 /** |
28 * @test |
29 * @test |
29 * @bug 8210583 |
30 * @bug 8210583 8217969 8218265 |
30 * @summary Tests Base64.Encoder.encode/Decoder.decode for the large size |
31 * @summary Tests Base64.Encoder.encode and Base64.Decoder.decode |
31 * of resulting bytes which can not be allocated |
32 * with the large size of input array/buffer |
32 * @requires os.maxMemory >= 6g |
33 * @requires os.maxMemory >= 8g |
33 * @run main/othervm -Xms4g -Xmx6g TestEncodingDecodingLength |
34 * @run main/othervm -Xms6g -Xmx8g TestEncodingDecodingLength |
34 * |
35 * |
35 */ |
36 */ |
36 |
37 |
37 public class TestEncodingDecodingLength { |
38 public class TestEncodingDecodingLength { |
38 |
39 |
39 public static void main(String[] args) { |
40 public static void main(String[] args) { |
40 int size = Integer.MAX_VALUE - 2; |
41 int size = Integer.MAX_VALUE - 8; |
41 byte[] inputBytes = new byte[size]; |
42 byte[] inputBytes = new byte[size]; |
42 byte[] outputBytes = new byte[size]; |
43 byte[] outputBytes = new byte[size]; |
43 |
44 |
44 // Check encoder with large array length |
45 // Check encoder with large array length |
45 Base64.Encoder encoder = Base64.getEncoder(); |
46 Base64.Encoder encoder = Base64.getEncoder(); |
46 checkOOM("encode(byte[])", () -> encoder.encode(inputBytes)); |
47 checkOOM("encode(byte[])", () -> encoder.encode(inputBytes)); |
47 checkIAE("encode(byte[] byte[])", () -> encoder.encode(inputBytes, outputBytes)); |
48 checkIAE("encode(byte[] byte[])", () -> encoder.encode(inputBytes, outputBytes)); |
48 checkOOM("encodeToString(byte[])", () -> encoder.encodeToString(inputBytes)); |
49 checkOOM("encodeToString(byte[])", () -> encoder.encodeToString(inputBytes)); |
49 checkOOM("encode(ByteBuffer)", () -> encoder.encode(ByteBuffer.allocate(size))); |
50 checkOOM("encode(ByteBuffer)", () -> encoder.encode(ByteBuffer.wrap(inputBytes))); |
50 |
51 |
51 // Check decoder with large array length |
52 // Check decoder with large array length, |
|
53 // should not throw any exception |
|
54 Arrays.fill(inputBytes, (byte) 86); |
52 Base64.Decoder decoder = Base64.getDecoder(); |
55 Base64.Decoder decoder = Base64.getDecoder(); |
53 checkOOM("decode(byte[])", () -> decoder.decode(inputBytes)); |
56 decoder.decode(inputBytes); |
54 checkIAE("decode(byte[], byte[])", () -> decoder.decode(inputBytes, outputBytes)); |
57 decoder.decode(inputBytes, outputBytes); |
55 checkOOM("decode(ByteBuffer)", () -> decoder.decode(ByteBuffer.allocate(size))); |
58 decoder.decode(ByteBuffer.wrap(inputBytes)); |
56 } |
59 } |
57 |
60 |
58 private static final void checkOOM(String methodName, Runnable r) { |
61 private static final void checkOOM(String methodName, Runnable r) { |
59 try { |
62 try { |
60 r.run(); |
63 r.run(); |