jdk/test/java/awt/FontClass/CreateFont/DeleteFont.java
changeset 2606 09ad5edb5330
child 2609 1db65c97bddc
equal deleted inserted replaced
2605:e8d0473c25e8 2606:09ad5edb5330
       
     1 /*
       
     2  * Copyright 2004-2008 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 import java.io.*;
       
    25 import java.awt.*;
       
    26 
       
    27 public class DeleteFont {
       
    28 
       
    29     public static void main(String args[]) throws Exception {
       
    30 
       
    31         String font = "A.ttf";
       
    32         String sep = System.getProperty("file.separator");
       
    33         String testSrc = System.getenv("TESTSRC");
       
    34         if (testSrc != null) {
       
    35             font = testSrc + sep + font;
       
    36         }
       
    37         System.out.println("Using font file: " + font);
       
    38         FileInputStream fis = new FileInputStream(font);
       
    39         Font f = Font.createFont(Font.TRUETYPE_FONT, fis);
       
    40         f.toString();
       
    41         f.deriveFont(Font.BOLD);
       
    42         f.canDisplay('X');
       
    43 
       
    44        InputStream in = new InputStream() {
       
    45             public int read() {
       
    46                 throw new RuntimeException();
       
    47             }
       
    48         };
       
    49         boolean gotException = false;
       
    50         try {
       
    51            Font.createFont(java.awt.Font.TRUETYPE_FONT, in);
       
    52         } catch (IOException e) {
       
    53             gotException = true;
       
    54         }
       
    55         if (!gotException) {
       
    56             throw new RuntimeException("No expected IOException");
       
    57         }
       
    58         badRead(-2);
       
    59         badRead(8193);
       
    60     }
       
    61 
       
    62     static void badRead(final int retval) {
       
    63         int num = 2;
       
    64         byte[] buff = new byte[16*8192]; // Multiple of 8192 is important.
       
    65         for (int ct=0; ct<num; ++ct) {
       
    66             try {
       
    67                 Font.createFont(
       
    68                     Font.TRUETYPE_FONT,
       
    69                     new ByteArrayInputStream(buff) {
       
    70                         @Override
       
    71                         public int read(byte[] buff, int off, int len) {
       
    72                             int read = super.read(buff, off, len);
       
    73                             return read<0 ? retval : read;
       
    74                         }
       
    75                     }
       
    76                 );
       
    77             } catch (Throwable exc) {
       
    78                 //exc.printStackTrace();
       
    79             }
       
    80         }
       
    81     }
       
    82 }
       
    83