author | ohair |
Thu, 17 Sep 2009 13:17:57 -0700 (2009-09-17) | |
changeset 3790 | ab4f91337479 |
parent 2606 | 09ad5edb5330 |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
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 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.applet.*; |
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 |
import java.io.*; |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
27 |
import java.net.*; |
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 class BigFont extends Applet { |
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 |
static private class SizedInputStream extends InputStream { |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
32 |
|
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
33 |
int size; |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
34 |
int cnt = 0; |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
35 |
|
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
36 |
SizedInputStream(int size) { |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
37 |
this.size = size; |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
38 |
} |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
39 |
|
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
40 |
public int read() { |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
41 |
if (cnt < size) { |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
42 |
cnt++; |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
43 |
return 0; |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
44 |
} else { |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
45 |
return -1; |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
46 |
} |
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 |
public int getCurrentSize() { |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
50 |
return cnt; |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
51 |
} |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
52 |
} |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
53 |
|
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
54 |
String id; |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
55 |
String fileName; |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
56 |
|
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
57 |
public void init() { |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
58 |
id = getParameter("number"); |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
59 |
fileName = getParameter("font"); |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
60 |
|
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
61 |
System.out.println("Applet " + id + " "+ |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
62 |
Thread.currentThread().getThreadGroup()); |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
63 |
// Larger than size for a single font. |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
64 |
int fontSize = 64 * 1000 * 1000; |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
65 |
SizedInputStream sis = new SizedInputStream(fontSize); |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
66 |
try { |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
67 |
Font font = Font.createFont(Font.TRUETYPE_FONT, sis); |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
68 |
} catch (Throwable t) { |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
69 |
if (t instanceof FontFormatException || |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
70 |
fontSize <= sis.getCurrentSize()) |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
71 |
{ |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
72 |
System.out.println(sis.getCurrentSize()); |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
73 |
System.out.println(t); |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
74 |
throw new RuntimeException("Allowed file to be too large."); |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
75 |
} |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
76 |
} |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
77 |
// The following part of the test was verified manually but |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
78 |
// is impractical to enable because it requires a fairly large |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
79 |
// valid font to be part of the test, and we can't easily include |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
80 |
// that, nor dependably reference one from the applet environment. |
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 |
if (fileName == null) { |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
83 |
return; |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
84 |
} |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
85 |
int size = getFileSize(fileName); |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
86 |
if (size == 0) { |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
87 |
return; |
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 |
int fontCnt = 1000 * 1000 * 1000 / size; |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
90 |
loadMany(size, fontCnt, fileName); |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
91 |
System.gc(); System.gc(); |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
92 |
fontCnt = fontCnt / 2; |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
93 |
System.out.println("Applet " + id + " load more."); |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
94 |
loadMany(size, fontCnt, fileName); |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
95 |
*/ |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
96 |
System.out.println("Applet " + id + " finished."); |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
97 |
} |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
98 |
|
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
99 |
int getFileSize(String fileName) { |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
100 |
try { |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
101 |
URL url = new URL(getCodeBase(), fileName); |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
102 |
InputStream inStream = url.openStream(); |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
103 |
BufferedInputStream fontStream = new BufferedInputStream(inStream); |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
104 |
int size = 0; |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
105 |
while (fontStream.read() != -1) { |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
106 |
size++; |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
107 |
} |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
108 |
fontStream.close(); |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
109 |
return size; |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
110 |
} catch (IOException e) { |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
111 |
return 0; |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
112 |
} |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
113 |
|
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
114 |
} |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
115 |
void loadMany(int oneFont, int fontCnt, String fileName) { |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
116 |
System.out.println("fontcnt= " + fontCnt); |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
117 |
Font[] fonts = new Font[fontCnt]; |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
118 |
int totalSize = 0; |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
119 |
boolean gotException = false; |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
120 |
for (int i=0; i<fontCnt; i++) { |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
121 |
try { |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
122 |
URL url = new URL(getCodeBase(), fileName); |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
123 |
InputStream inStream = url.openStream(); |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
124 |
BufferedInputStream fontStream = |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
125 |
new BufferedInputStream(inStream); |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
126 |
fonts[i] = Font.createFont(Font.TRUETYPE_FONT, fontStream); |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
127 |
totalSize += oneFont; |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
128 |
fontStream.close(); |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
129 |
} catch (Throwable t) { |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
130 |
gotException = true; |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
131 |
System.out.println("Applet " + id + " " + t); |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
132 |
} |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
133 |
} |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
134 |
if (!gotException) { |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
135 |
throw new RuntimeException("No expected exception"); |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
136 |
} |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
137 |
} |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
138 |
} |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
diff
changeset
|
139 |