Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/security/SecureClassLoader/DefineClassByteBuffer.java
41149 views
1
/*
2
* Copyright (c) 2003, 2011, 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
/*
25
* @test
26
* @bug 4894899 7054428
27
* @summary Test various cases of passing java.nio.ByteBuffers
28
* to defineClass().
29
*
30
* @build DefineClassByteBuffer TestClass
31
* @run main DefineClassByteBuffer
32
*/
33
34
import java.security.*;
35
import java.nio.*;
36
import java.nio.channels.*;
37
import java.io.*;
38
39
public class DefineClassByteBuffer {
40
41
static void test(ClassLoader cl) throws Exception {
42
Class c = Class.forName("TestClass", true, cl);
43
if (!"TestClass".equals(c.getName())) {
44
throw new RuntimeException("Got wrong class: " + c);
45
}
46
}
47
48
public static void main(String arg[]) throws Exception {
49
50
// Rename the compiled TestClass.class file to something else,
51
// otherwise it would be loaded by the parent class loader and
52
// DummyClassLoader will never be used, especially in /othervm mode.
53
54
File oldFile = new File(System.getProperty("test.classes", "."),
55
"TestClass.class");
56
File newFile = new File(System.getProperty("test.classes", "."),
57
"CLAZZ");
58
oldFile.renameTo(newFile);
59
60
ClassLoader[] cls = new ClassLoader[DummyClassLoader.MAX_TYPE];
61
for (int i = 0; i < cls.length; i++) {
62
cls[i] = new DummyClassLoader(i);
63
}
64
65
/* Create several instances of the class using different classloaders,
66
which are using different types of ByteBuffer. */
67
for (int i = 0; i < cls.length; i++) {
68
test(cls[i]);
69
}
70
71
if (DummyClassLoader.count != cls.length) {
72
throw new Exception("DummyClassLoader not always used");
73
}
74
}
75
76
/** Always loads the same class, using various types of ByteBuffers */
77
public static class DummyClassLoader extends SecureClassLoader {
78
79
public static final String CLASS_NAME = "TestClass";
80
81
public static final int MAPPED_BUFFER = 0;
82
public static final int DIRECT_BUFFER = 1;
83
public static final int ARRAY_BUFFER = 2;
84
public static final int WRAPPED_BUFFER = 3;
85
public static final int READ_ONLY_ARRAY_BUFFER = 4;
86
public static final int READ_ONLY_DIRECT_BUFFER = 5;
87
public static final int DUP_ARRAY_BUFFER = 6;
88
public static final int DUP_DIRECT_BUFFER = 7;
89
public static final int MAX_TYPE = 7;
90
91
int loaderType;
92
93
static int count = 0;
94
95
DummyClassLoader(int loaderType) {
96
this.loaderType = loaderType;
97
}
98
99
static ByteBuffer[] buffers = new ByteBuffer[MAX_TYPE + 1];
100
101
static ByteBuffer readClassFile(String name) {
102
try {
103
File f = new File(System.getProperty("test.classes", "."),
104
"CLAZZ");
105
try (FileInputStream fin = new FileInputStream(f);
106
FileChannel fc = fin.getChannel()) {
107
return fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
108
}
109
} catch (FileNotFoundException e) {
110
throw new RuntimeException("Can't open file: " + name, e);
111
} catch (IOException e) {
112
throw new RuntimeException("Can't open file: " + name, e);
113
}
114
}
115
116
static {
117
/* create a bunch of different ByteBuffers, starting with a mapped
118
buffer from a class file, and create various duplicate and wrapped
119
buffers. */
120
buffers[MAPPED_BUFFER] = readClassFile(CLASS_NAME + ".class");
121
byte[] array = new byte[buffers[MAPPED_BUFFER].limit()];
122
buffers[MAPPED_BUFFER].get(array);
123
buffers[MAPPED_BUFFER].flip();
124
125
buffers[DIRECT_BUFFER] = ByteBuffer.allocateDirect(array.length);
126
buffers[DIRECT_BUFFER].put(array);
127
buffers[DIRECT_BUFFER].flip();
128
129
buffers[ARRAY_BUFFER] = ByteBuffer.allocate(array.length);
130
buffers[ARRAY_BUFFER].put(array);
131
buffers[ARRAY_BUFFER].flip();
132
133
buffers[WRAPPED_BUFFER] = ByteBuffer.wrap(array);
134
135
buffers[READ_ONLY_ARRAY_BUFFER] = buffers[ARRAY_BUFFER].asReadOnlyBuffer();
136
137
buffers[READ_ONLY_DIRECT_BUFFER] = buffers[DIRECT_BUFFER].asReadOnlyBuffer();
138
139
buffers[DUP_ARRAY_BUFFER] = buffers[ARRAY_BUFFER].duplicate();
140
141
buffers[DUP_DIRECT_BUFFER] = buffers[DIRECT_BUFFER].duplicate();
142
}
143
144
public Class findClass(String name) {
145
CodeSource cs = null;
146
count++;
147
return defineClass(name, buffers[loaderType], cs);
148
}
149
} /* DummyClassLoader */
150
151
} /* DefineClassByteBuffer */
152
153