Path: blob/master/test/jdk/java/util/Base64/TestEncodingDecodingLength.java
41149 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.nio.ByteBuffer;24import java.util.Arrays;25import java.util.Base64;2627/**28* @test29* @bug 8210583 8217969 821826530* @summary Tests Base64.Encoder.encode and Base64.Decoder.decode31* with the large size of input array/buffer32* @requires (sun.arch.data.model == "64" & os.maxMemory >= 10g)33* @run main/othervm -Xms6g -Xmx8g TestEncodingDecodingLength34*35*/3637public class TestEncodingDecodingLength {3839public static void main(String[] args) {40int size = Integer.MAX_VALUE - 8;41byte[] inputBytes = new byte[size];42byte[] outputBytes = new byte[size];4344// Check encoder with large array length45Base64.Encoder encoder = Base64.getEncoder();46checkOOM("encode(byte[])", () -> encoder.encode(inputBytes));47checkIAE("encode(byte[] byte[])", () -> encoder.encode(inputBytes, outputBytes));48checkOOM("encodeToString(byte[])", () -> encoder.encodeToString(inputBytes));49checkOOM("encode(ByteBuffer)", () -> encoder.encode(ByteBuffer.wrap(inputBytes)));5051// Check decoder with large array length,52// should not throw any exception53Arrays.fill(inputBytes, (byte) 86);54Base64.Decoder decoder = Base64.getDecoder();55decoder.decode(inputBytes);56decoder.decode(inputBytes, outputBytes);57decoder.decode(ByteBuffer.wrap(inputBytes));58}5960private static final void checkOOM(String methodName, Runnable r) {61try {62r.run();63throw new RuntimeException("OutOfMemoryError should have been thrown by: " + methodName);64} catch (OutOfMemoryError er) {}65}6667private static final void checkIAE(String methodName, Runnable r) {68try {69r.run();70throw new RuntimeException("IllegalArgumentException should have been thrown by: " + methodName);71} catch (IllegalArgumentException iae) {}72}73}74757677