jdk/test/java/awt/FontClass/CreateFont/DeleteFont.java
author prr
Tue, 03 Mar 2009 16:10:37 -0800
changeset 2609 1db65c97bddc
parent 2606 09ad5edb5330
child 5506 202f599c92aa
permissions -rw-r--r--
2163516: Font.createFont can be persuaded to leak temporary files Reviewed-by: igor
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2606
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
     1
/*
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
     2
 * Copyright 2004-2008 Sun Microsystems, Inc.  All Rights Reserved.
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
     4
 *
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
     7
 * published by the Free Software Foundation.
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
     8
 *
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    13
 * accompanied this code).
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    14
 *
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    18
 *
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    21
 * have any questions.
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    22
 */
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    23
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    24
import java.io.*;
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    25
import java.awt.*;
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    26
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    27
public class DeleteFont {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    28
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    29
    public static void main(String args[]) throws Exception {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    30
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    31
        String font = "A.ttf";
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    32
        String sep = System.getProperty("file.separator");
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    33
        String testSrc = System.getenv("TESTSRC");
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    34
        if (testSrc != null) {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    35
            font = testSrc + sep + font;
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    36
        }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    37
        System.out.println("Using font file: " + font);
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    38
        FileInputStream fis = new FileInputStream(font);
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    39
        Font f = Font.createFont(Font.TRUETYPE_FONT, fis);
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    40
        f.toString();
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    41
        f.deriveFont(Font.BOLD);
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    42
        f.canDisplay('X');
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    43
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    44
       InputStream in = new InputStream() {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    45
            public int read() {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    46
                throw new RuntimeException();
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    47
            }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    48
        };
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    49
        boolean gotException = false;
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    50
        try {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    51
           Font.createFont(java.awt.Font.TRUETYPE_FONT, in);
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    52
        } catch (IOException e) {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    53
            gotException = true;
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    54
        }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    55
        if (!gotException) {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    56
            throw new RuntimeException("No expected IOException");
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    57
        }
2609
1db65c97bddc 2163516: Font.createFont can be persuaded to leak temporary files
prr
parents: 2606
diff changeset
    58
        badRead(-2, Font.TRUETYPE_FONT);
1db65c97bddc 2163516: Font.createFont can be persuaded to leak temporary files
prr
parents: 2606
diff changeset
    59
        badRead(8193, Font.TRUETYPE_FONT);
1db65c97bddc 2163516: Font.createFont can be persuaded to leak temporary files
prr
parents: 2606
diff changeset
    60
1db65c97bddc 2163516: Font.createFont can be persuaded to leak temporary files
prr
parents: 2606
diff changeset
    61
        badRead(-2, Font.TYPE1_FONT);
1db65c97bddc 2163516: Font.createFont can be persuaded to leak temporary files
prr
parents: 2606
diff changeset
    62
        badRead(8193, Font.TYPE1_FONT);
1db65c97bddc 2163516: Font.createFont can be persuaded to leak temporary files
prr
parents: 2606
diff changeset
    63
1db65c97bddc 2163516: Font.createFont can be persuaded to leak temporary files
prr
parents: 2606
diff changeset
    64
        // Make sure GC has a chance to clean up before we exit.
1db65c97bddc 2163516: Font.createFont can be persuaded to leak temporary files
prr
parents: 2606
diff changeset
    65
        System.gc(); System.gc();
2606
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    66
    }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    67
2609
1db65c97bddc 2163516: Font.createFont can be persuaded to leak temporary files
prr
parents: 2606
diff changeset
    68
    static void badRead(final int retval, int fontType) {
2606
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    69
        int num = 2;
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    70
        byte[] buff = new byte[16*8192]; // Multiple of 8192 is important.
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    71
        for (int ct=0; ct<num; ++ct) {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    72
            try {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    73
                Font.createFont(
2609
1db65c97bddc 2163516: Font.createFont can be persuaded to leak temporary files
prr
parents: 2606
diff changeset
    74
                    fontType,
2606
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    75
                    new ByteArrayInputStream(buff) {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    76
                        @Override
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    77
                        public int read(byte[] buff, int off, int len) {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    78
                            int read = super.read(buff, off, len);
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    79
                            return read<0 ? retval : read;
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    80
                        }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    81
                    }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    82
                );
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    83
            } catch (Throwable exc) {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    84
                //exc.printStackTrace();
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    85
            }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    86
        }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    87
    }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    88
}
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff changeset
    89