< prev index next >

test/jdk/java/awt/FontClass/CreateFont/BigFont.java

Print this page


   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         if (System.getSecurityManager() == null) {
  64             System.setSecurityManager(new SecurityManager());
  65         }
  66         // Larger than size for a single font.
  67         int fontSize = 64 * 1000 * 1000;
  68         SizedInputStream sis = new SizedInputStream(fontSize);
  69         try {
  70              Font font = Font.createFont(Font.TRUETYPE_FONT, sis);
  71         } catch (Throwable t) {
  72             t.printStackTrace();
  73             if (t instanceof FontFormatException ||
  74                 fontSize <= sis.getCurrentSize())
  75             {
  76                 System.out.println(sis.getCurrentSize());
  77                 System.out.println(t);
  78                 throw new RuntimeException("Allowed file to be too large.");
  79             }


  85         /*
  86         if (fileName == null) {
  87             return;
  88         }
  89         int size = getFileSize(fileName);
  90         if (size == 0) {
  91             return;
  92         }
  93         int fontCnt = 1000 * 1000 * 1000 / size;
  94         loadMany(size, fontCnt, fileName);
  95         System.gc(); System.gc();
  96         fontCnt = fontCnt / 2;
  97         System.out.println("Applet " + id + " load more.");
  98         loadMany(size, fontCnt, fileName);
  99         */
 100         System.out.println("Applet " + id + " finished.");
 101     }
 102 
 103     int getFileSize(String fileName) {
 104         try {
 105             URL url = new URL(getCodeBase(), fileName);



 106             InputStream inStream = url.openStream();
 107             BufferedInputStream fontStream = new BufferedInputStream(inStream);
 108             int size = 0;
 109             while (fontStream.read() != -1) {
 110                 size++;
 111             }
 112             fontStream.close();
 113             return size;
 114         } catch (IOException e) {
 115             return 0;
 116         }
 117 
 118     }
 119     void loadMany(int oneFont, int fontCnt, String fileName) {
 120         System.out.println("fontcnt= " + fontCnt);
 121         Font[] fonts = new Font[fontCnt];
 122         int totalSize = 0;
 123         boolean gotException = false;
 124         for (int i=0; i<fontCnt; i++) {
 125             try {
 126                 URL url = new URL(getCodeBase(), fileName);



 127                 InputStream inStream = url.openStream();
 128                 BufferedInputStream fontStream =
 129                     new BufferedInputStream(inStream);
 130                 fonts[i] = Font.createFont(Font.TRUETYPE_FONT, fontStream);
 131                 totalSize += oneFont;
 132                 fontStream.close();
 133             } catch (Throwable t) {
 134                 gotException = true;
 135                 System.out.println("Applet " + id + " " + t);
 136             }
 137         }
 138         if (!gotException) {
 139           throw new RuntimeException("No expected exception");
 140         }
 141     }
 142 }
 143 
   1 /*
   2  * Copyright (c) 2008, 2018, 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.awt.Font;
  25 import java.awt.FontFormatException;
  26 import java.io.BufferedInputStream;
  27 import java.io.IOException;
  28 import java.io.InputStream;
  29 import java.net.URL;
  30 import java.nio.file.Paths;
  31 
  32 /**
  33  * @test
  34  * @key headful
  35  * @bug 6522586
  36  * @summary Enforce limits on font creation
  37  * @run main BigFont 1 A.ttf
  38  * @run main BigFont 2 A.ttf
  39  */
  40 public class BigFont {
  41 
  42    static private class SizedInputStream extends InputStream {
  43 
  44        int size;
  45        int cnt = 0;
  46 
  47        SizedInputStream(int size) {
  48            this.size = size;
  49        }
  50 
  51        public int read() {
  52            if (cnt < size) {
  53               cnt++;
  54               return 0;
  55            } else {
  56               return -1;
  57            }
  58        }
  59 
  60        public int getCurrentSize() {
  61            return cnt;
  62        }
  63    }
  64 
  65     static String id;
  66     static String fileName;
  67 
  68     public static void main(final String[] args) {
  69         id = args[0];
  70         fileName = args[1];
  71 
  72         System.out.println("Applet " + id + " "+
  73                            Thread.currentThread().getThreadGroup());
  74         if (System.getSecurityManager() == null) {
  75             System.setSecurityManager(new SecurityManager());
  76         }
  77         // Larger than size for a single font.
  78         int fontSize = 64 * 1000 * 1000;
  79         SizedInputStream sis = new SizedInputStream(fontSize);
  80         try {
  81              Font font = Font.createFont(Font.TRUETYPE_FONT, sis);
  82         } catch (Throwable t) {
  83             t.printStackTrace();
  84             if (t instanceof FontFormatException ||
  85                 fontSize <= sis.getCurrentSize())
  86             {
  87                 System.out.println(sis.getCurrentSize());
  88                 System.out.println(t);
  89                 throw new RuntimeException("Allowed file to be too large.");
  90             }


  96         /*
  97         if (fileName == null) {
  98             return;
  99         }
 100         int size = getFileSize(fileName);
 101         if (size == 0) {
 102             return;
 103         }
 104         int fontCnt = 1000 * 1000 * 1000 / size;
 105         loadMany(size, fontCnt, fileName);
 106         System.gc(); System.gc();
 107         fontCnt = fontCnt / 2;
 108         System.out.println("Applet " + id + " load more.");
 109         loadMany(size, fontCnt, fileName);
 110         */
 111         System.out.println("Applet " + id + " finished.");
 112     }
 113 
 114     int getFileSize(String fileName) {
 115         try {
 116             String path = Paths.get(System.getProperty("test.src", "."),
 117                                     fileName).toAbsolutePath().normalize()
 118                                              .toString();
 119             URL url = new URL(path);
 120             InputStream inStream = url.openStream();
 121             BufferedInputStream fontStream = new BufferedInputStream(inStream);
 122             int size = 0;
 123             while (fontStream.read() != -1) {
 124                 size++;
 125             }
 126             fontStream.close();
 127             return size;
 128         } catch (IOException e) {
 129             return 0;
 130         }
 131 
 132     }
 133     void loadMany(int oneFont, int fontCnt, String fileName) {
 134         System.out.println("fontcnt= " + fontCnt);
 135         Font[] fonts = new Font[fontCnt];
 136         int totalSize = 0;
 137         boolean gotException = false;
 138         for (int i=0; i<fontCnt; i++) {
 139             try {
 140                 String path = Paths.get(System.getProperty("test.src", "."),
 141                                         fileName).toAbsolutePath().normalize()
 142                                                  .toString();
 143                 URL url = new URL(path);
 144                 InputStream inStream = url.openStream();
 145                 BufferedInputStream fontStream =
 146                     new BufferedInputStream(inStream);
 147                 fonts[i] = Font.createFont(Font.TRUETYPE_FONT, fontStream);
 148                 totalSize += oneFont;
 149                 fontStream.close();
 150             } catch (Throwable t) {
 151                 gotException = true;
 152                 System.out.println("Applet " + id + " " + t);
 153             }
 154         }
 155         if (!gotException) {
 156           throw new RuntimeException("No expected exception");
 157         }
 158     }
 159 }
 160 
< prev index next >