1 /* 2 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.applet.*; 25 import java.awt.*; 26 import java.io.*; 27 import java.net.*; 28 29 public class BigFont extends Applet { 30 31 static private class SizedInputStream extends InputStream { 32 33 int size; 34 int cnt = 0; 35 36 SizedInputStream(int size) { 37 this.size = size; 38 } 39 40 public int read() { 41 if (cnt < size) { 42 cnt++; 43 return 0; 44 } else { 45 return -1; 46 } 47 } 48 49 public int getCurrentSize() { 50 return cnt; 51 } 52 } 53 54 String id; 55 String fileName; 56 57 public void init() { 58 id = getParameter("number"); 59 fileName = getParameter("font"); 60 61 System.out.println("Applet " + id + " "+ 62 Thread.currentThread().getThreadGroup()); 63 // Larger than size for a single font. 64 int fontSize = 64 * 1000 * 1000; 65 SizedInputStream sis = new SizedInputStream(fontSize); 66 try { 67 Font font = Font.createFont(Font.TRUETYPE_FONT, sis); 68 } catch (Throwable t) { 69 if (t instanceof FontFormatException || 70 fontSize <= sis.getCurrentSize()) 71 { 72 System.out.println(sis.getCurrentSize()); 73 System.out.println(t); 74 throw new RuntimeException("Allowed file to be too large."); 75 } 76 } 77 // The following part of the test was verified manually but 78 // is impractical to enable because it requires a fairly large 79 // valid font to be part of the test, and we can't easily include 80 // that, nor dependably reference one from the applet environment. 81 /* 82 if (fileName == null) { 83 return; 84 } 85 int size = getFileSize(fileName); 86 if (size == 0) { 87 return; 88 } 89 int fontCnt = 1000 * 1000 * 1000 / size; 90 loadMany(size, fontCnt, fileName); 91 System.gc(); System.gc(); 92 fontCnt = fontCnt / 2; 93 System.out.println("Applet " + id + " load more."); 94 loadMany(size, fontCnt, fileName); 95 */ 96 System.out.println("Applet " + id + " finished."); 97 } 98 99 int getFileSize(String fileName) { 100 try { 101 URL url = new URL(getCodeBase(), fileName); 102 InputStream inStream = url.openStream(); 103 BufferedInputStream fontStream = new BufferedInputStream(inStream); 104 int size = 0; 105 while (fontStream.read() != -1) { 106 size++; 107 } 108 fontStream.close(); 109 return size; 110 } catch (IOException e) { 111 return 0; 112 } 113 114 } 115 void loadMany(int oneFont, int fontCnt, String fileName) { 116 System.out.println("fontcnt= " + fontCnt); 117 Font[] fonts = new Font[fontCnt]; 118 int totalSize = 0; 119 boolean gotException = false; 120 for (int i=0; i<fontCnt; i++) { 121 try { 122 URL url = new URL(getCodeBase(), fileName); 123 InputStream inStream = url.openStream(); 124 BufferedInputStream fontStream = 125 new BufferedInputStream(inStream); 126 fonts[i] = Font.createFont(Font.TRUETYPE_FONT, fontStream); 127 totalSize += oneFont; 128 fontStream.close(); 129 } catch (Throwable t) { 130 gotException = true; 131 System.out.println("Applet " + id + " " + t); 132 } 133 } 134 if (!gotException) { 135 throw new RuntimeException("No expected exception"); 136 } 137 } 138 } 139