Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/FontClass/CreateFont/BigFont.java
38828 views
/*1* Copyright (c) 2008, 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.applet.*;24import java.awt.*;25import java.io.*;26import java.net.*;2728public class BigFont extends Applet {2930static private class SizedInputStream extends InputStream {3132int size;33int cnt = 0;3435SizedInputStream(int size) {36this.size = size;37}3839public int read() {40if (cnt < size) {41cnt++;42return 0;43} else {44return -1;45}46}4748public int getCurrentSize() {49return cnt;50}51}5253String id;54String fileName;5556public void init() {57id = getParameter("number");58fileName = getParameter("font");5960System.out.println("Applet " + id + " "+61Thread.currentThread().getThreadGroup());62// Larger than size for a single font.63int fontSize = 64 * 1000 * 1000;64SizedInputStream sis = new SizedInputStream(fontSize);65try {66Font font = Font.createFont(Font.TRUETYPE_FONT, sis);67} catch (Throwable t) {68if (t instanceof FontFormatException ||69fontSize <= sis.getCurrentSize())70{71System.out.println(sis.getCurrentSize());72System.out.println(t);73throw new RuntimeException("Allowed file to be too large.");74}75}76// The following part of the test was verified manually but77// is impractical to enable because it requires a fairly large78// valid font to be part of the test, and we can't easily include79// that, nor dependably reference one from the applet environment.80/*81if (fileName == null) {82return;83}84int size = getFileSize(fileName);85if (size == 0) {86return;87}88int fontCnt = 1000 * 1000 * 1000 / size;89loadMany(size, fontCnt, fileName);90System.gc(); System.gc();91fontCnt = fontCnt / 2;92System.out.println("Applet " + id + " load more.");93loadMany(size, fontCnt, fileName);94*/95System.out.println("Applet " + id + " finished.");96}9798int getFileSize(String fileName) {99try {100URL url = new URL(getCodeBase(), fileName);101InputStream inStream = url.openStream();102BufferedInputStream fontStream = new BufferedInputStream(inStream);103int size = 0;104while (fontStream.read() != -1) {105size++;106}107fontStream.close();108return size;109} catch (IOException e) {110return 0;111}112113}114void loadMany(int oneFont, int fontCnt, String fileName) {115System.out.println("fontcnt= " + fontCnt);116Font[] fonts = new Font[fontCnt];117int totalSize = 0;118boolean gotException = false;119for (int i=0; i<fontCnt; i++) {120try {121URL url = new URL(getCodeBase(), fileName);122InputStream inStream = url.openStream();123BufferedInputStream fontStream =124new BufferedInputStream(inStream);125fonts[i] = Font.createFont(Font.TRUETYPE_FONT, fontStream);126totalSize += oneFont;127fontStream.close();128} catch (Throwable t) {129gotException = true;130System.out.println("Applet " + id + " " + t);131}132}133if (!gotException) {134throw new RuntimeException("No expected exception");135}136}137}138139140141