Path: blob/master/test/jdk/java/awt/FontClass/CreateFont/DeleteFont.java
41153 views
/*1* Copyright (c) 2004, 2020, 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.io.ByteArrayInputStream;24import java.io.FileInputStream;25import java.io.InputStream;26import java.io.IOException;27import java.awt.Font;2829public class DeleteFont {3031public static void main(String args[]) throws Exception {3233String font = "A.ttf";34String sep = System.getProperty("file.separator");35String testSrc = System.getenv("TESTSRC");36if (testSrc != null) {37font = testSrc + sep + font;38}39System.out.println("Using font file: " + font);40FileInputStream fis = new FileInputStream(font);41Font f = Font.createFont(Font.TRUETYPE_FONT, fis);42f.toString();43f.deriveFont(Font.BOLD);44f.canDisplay('X');4546InputStream in = new InputStream() {47public int read() {48throw new RuntimeException();49}50};51boolean gotException = false;52try {53Font.createFont(java.awt.Font.TRUETYPE_FONT, in);54} catch (IOException e) {55gotException = true;56}57if (!gotException) {58throw new RuntimeException("No expected IOException");59}6061badRead(-2, 16, Font.TYPE1_FONT);62badRead(8193, 14, Font.TYPE1_FONT);6364badRead(-2, 12, Font.TRUETYPE_FONT);65badRead(8193, 10, Font.TRUETYPE_FONT);6667// Make sure GC has a chance to clean up before we exit.68System.gc(); System.gc();69Thread.sleep(5000);70System.gc(); System.gc();71}7273static void badRead(final int retval, final int multiple, int fontType) {74int num = 2;75byte[] buff = new byte[multiple*8192]; // Multiple of 8192 is important.76for (int ct=0; ct<num; ++ct) {77try {78Font.createFont(79fontType,80new ByteArrayInputStream(buff) {81@Override82public int read(byte[] buff, int off, int len) {83int read = super.read(buff, off, len);84return read<0 ? retval : read;85}86}87);88} catch (Throwable exc) {89//exc.printStackTrace();90}91}92}93}94959697