Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/FontClass/CreateFont/DeleteFont.java
38828 views
/*1* Copyright (c) 2004, 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.io.*;24import java.awt.*;2526public class DeleteFont {2728public static void main(String args[]) throws Exception {2930String font = "A.ttf";31String sep = System.getProperty("file.separator");32String testSrc = System.getenv("TESTSRC");33if (testSrc != null) {34font = testSrc + sep + font;35}36System.out.println("Using font file: " + font);37FileInputStream fis = new FileInputStream(font);38Font f = Font.createFont(Font.TRUETYPE_FONT, fis);39f.toString();40f.deriveFont(Font.BOLD);41f.canDisplay('X');4243InputStream in = new InputStream() {44public int read() {45throw new RuntimeException();46}47};48boolean gotException = false;49try {50Font.createFont(java.awt.Font.TRUETYPE_FONT, in);51} catch (IOException e) {52gotException = true;53}54if (!gotException) {55throw new RuntimeException("No expected IOException");56}57badRead(-2, Font.TRUETYPE_FONT);58badRead(8193, Font.TRUETYPE_FONT);5960badRead(-2, Font.TYPE1_FONT);61badRead(8193, Font.TYPE1_FONT);6263// Make sure GC has a chance to clean up before we exit.64System.gc(); System.gc();65}6667static void badRead(final int retval, int fontType) {68int num = 2;69byte[] buff = new byte[16*8192]; // Multiple of 8192 is important.70for (int ct=0; ct<num; ++ct) {71try {72Font.createFont(73fontType,74new ByteArrayInputStream(buff) {75@Override76public int read(byte[] buff, int off, int len) {77int read = super.read(buff, off, len);78return read<0 ? retval : read;79}80}81);82} catch (Throwable exc) {83//exc.printStackTrace();84}85}86}87}88899091